Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: ESM and CJS transpilation #1067

Draft
wants to merge 84 commits into
base: master
Choose a base branch
from

Conversation

andy-haynes
Copy link
Collaborator

Pre-flight checklist

  • I have read the Contributing Guidelines on pull requests.
  • Commit messages follow the conventional commits spec
  • If this is a code change: I have written unit tests.
  • If this is a new API or substantial change: the PR has an accompanying issue (closes #0000) and the maintainers have approved on my working plan.

Motivation

This PR updates the new packages to support ESM and CJS transpilation from the TypeScript source. By supporting ESM modules we remove hurdles for working with new versions of NodeJS and ESM-based frameworks like Vite, addressing longstanding issues for developers.

Worth noting in this PR:

  • No bundling has been introduced, the reasoning being that consumers would import modules directly and rely on bundling and/or tree shaking to produce the dependency graph.
  • New package.json files now include exports and browser. exports points the consumer to lib/cjs for require imports and lib/esm for native import. browser references lib/cjs and is currently required for near-api-js to build its bundle with browserify.
  • JS files in package scope (e.g. tests) are expected to be ESM-compatible, enabling use of import/export and requiring that relative paths to JS modules include .js file extensions.
  • Only the modules at lib/esm are tested currently. It may be overkill to also test the CJS output 🤔

Test Plan

This is entirely a packaging change and so only affects consumers, not any specific runtime functionality. The new example-esm and example-vite packages have been added to demonstrate usage with native modules. Local testing has been done by running near-wallet locally after yarn linking the new near-api-js package, and current bundles have been compared to those produced by the changes in this PR.

Related issues/PRs

This is the most directly-relevant one I found at a glance, but the cascading goodness stemming from this change (on top of breaking NAJ into packages) will likely resolve other issues related to polyfills, bundling, and other usage issues.

@changeset-bot
Copy link

changeset-bot bot commented Feb 7, 2023

⚠️ No Changeset found

Latest commit: 5960b51

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Base automatically changed from refactor/packages to master March 30, 2023 18:45
@andy-haynes andy-haynes marked this pull request as draft June 26, 2023 18:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Backlog 🥶
Development

Successfully merging this pull request may close these issues.

None yet

1 participant