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

Node ESM configuration w/ CJS & ESM build outputs #81

Merged
merged 6 commits into from
Apr 3, 2024

Conversation

coren-frankel
Copy link
Contributor

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() with import within the src directory. The jest.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

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
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.
@danomatic
Copy link
Owner

This looks great! I don't have any experience with converting to an ES module but I'll merge and play with it!

@danomatic danomatic merged commit f61fc53 into danomatic:main Apr 3, 2024
@danomatic
Copy link
Owner

@coren-frankel I think I got this all working in v1.3.2!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants