-
Notifications
You must be signed in to change notification settings - Fork 185
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
Convert source code to using ESM instead of CommonJS #191
Open
aron
wants to merge
11
commits into
typescript-definitions
Choose a base branch
from
default-esm
base: typescript-definitions
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This makes the library very quick to get up and running. We use a proxy instance to ensure that the library keeps working in its existing state though with a @deprecated type annotation which will show up in editors.
6a33a89
to
1195d8a
Compare
1195d8a
to
cac159f
Compare
43678dd
to
6b6eb15
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #187
Most ecosystems are moving towards ECMAScript modules as the primary way of writing code (see browser, , TypeScript, bun, deno etc). Even the node docs describe ESM as:
Our commonjs source code can be consumed by nodejs programs as a first class module system and most of the above as a secondary (with the exception of the browser which would need transforming/bundling).
So this PR explores converting the codebase to use ESM modules instead of CommonJS (and providing a transpiled version of commonjs). I think this makes sense long term as we'll be able to use the ESM version in the browser, deno etc. and it supports default exports which allows us to cleanly extend the surface area of the library to export additional objects (like error types and helpers).
The code change here is pretty vast but it can be summarized:
build:commonjs
script which will use the TypeScript compiler to output commonjs into thedist/commonjs
directory. We then add a newexports
field to the package.json to wire this up. A couple of caveats here:{"type": "commonjs"}
into the dist/commonjs directory, otherwise it'll try and interpret the files as esm.index.cjs
file to do the initial require and take the default export, though we can generate this.This means that in most cases everything works as expected and TypeScript projects no longer need to set the
esModuleInterop
flag forreplicate
.