Node ESM configuration w/ CJS & ESM build outputs #81
+377
−47
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()
withimport
within thesrc
directory. Thejest.config.js
and.prettierrc.js
configuration files adopt the.cjs
extension 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