Node ESM configuration w/ CJS & ESM build outputs#81
Merged
danomatic merged 6 commits intodanomatic:mainfrom Apr 3, 2024
Merged
Node ESM configuration w/ CJS & ESM build outputs#81danomatic merged 6 commits intodanomatic:mainfrom
danomatic merged 6 commits intodanomatic:mainfrom
Conversation
Extends the root tsconfig.json properties to build CJS, ESM, and types output. The build script and other package.json changes provide import/export flexibility by way of converting package to ES module.
ESM demands more types to shore up all implicit any from dependencies. ts-node is required for jest ts config. All require() replaced with import. prettier config was best left as commonjs, and received cjs extension.
Upgraded ts dependencies and added modern esm config for ts-jest.
The current tests are working without the .js extension usually required by es modules. Without adding more tests, I have withdrawn the added extensions until deemed necessary.
Owner
|
This looks great! I don't have any experience with converting to an ES module but I'll merge and play with it! |
Owner
|
@coren-frankel I think I got this all working in |
This file contains hidden or 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
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.
Inspired by my use case that led me to #57, I've made several TypeScript configuration changes to transition the package to an ES module, which builds CJS and ESM with types for greater compatibility across Node apps. This PR includes multiple new tsconfig.json files that extend the base and several package.json changes that provide entry points to the respective JS module types. Because there is no open issue for this change, I have avoided dependency upgrades and utilized the configuration options available with the current package.json versions of typescript, jest, & ts-jest.
Because the interoperability of both module types requires that the package become an ES module, I have replaced all
require()withimportwithin thesrcdirectory. Thejest.config.jsand.prettierrc.jsconfiguration files adopt the.cjsextension to maintain their syntax and current functionality.As it stands, I have not created any new tests and can only confirm that the previous test suite is still passing. I will continue to research best practices for transitioning a library from CJS to ESM/CJS compatibility, specifically to generate adequate tests demonstrating this PR is successful in its goal. Because the previous tests do not interact with the dist build (or any build for that matter), I would love any applicable guidance while I attempt to devise and draft relevant tests for each build output.
I understand these changes may also appear too broad in scope, so please feel free to request edits or ask for my justification in your review(s) for any specific choices. I appreciate your time and hope I can make meaningful contributions to this package, even if this PR is not the way.
For reference, my research took me through many sources and guides, but here are some of the most relevant:
tsconfig reference
microsoft/TypeScript: Dual ESM/CJS emit with tsc #54593
microsoft/TypeScript: Proposal: compute module format based on package.json visible to declarationDir/outDir #54546
blog-guide: Supporting CommonJS and ESM with Typescript and Node
ts-jest esm support