-
Notifications
You must be signed in to change notification settings - Fork 196
Add CJS/ESM module exports #81
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
Add CJS/ESM module exports #81
Conversation
- rollup - @rollup/plugin-typescript - rollup-plugin-dts - rollup-plugin-esbuild - esbuild
Following changes: - update lib and target to es2018 - add esModuleInterop and set to true - add moduleResolution and set to node
The configuration is designe to build: - declarations - cjs module - esm module
Set the correct entrypoint for cjs and esm modules and update build script to perform a typecheck followed by a rollup build
Update typescript to latest version due to an issue that version 4.4.2 has with rollup. For reference here is the issue: [Builds hang with typescript 4.4.2](rollup/plugins#983)
Add 'sideEffects: false' to package.json to allow webpack (and possible other bundlers) to threeshake the code
|
Have you done testing to make sure that the resulting ESM target is tree shakable? Just because it's ESM-compatible doesn't guarantee that it's tree-shakable, so I just want to confirm. Honestly, I don't think bundling everything in a single index.js file is ideal, would be best to leave it as separate files, but as long as it's tree shakable and sourcemaps are generated correctly, it shouldn't matter much. |
I tried it using Vite (so Rollup + esbuild) and it was threeshaking perfectly. If you want to try it out wiht webpack, please feel free to do so. Easiest way to try this out is to clone the branch containing these changes, Regarding the single file vs multiple file, I opted for single file just to make the lib a bit smaller, but If you think multiple files is preferable then I will update it tomorrow toghether with the tsconfig options |
Okay, thank you. |
|
I can confirm that also with webpack it is three shaking correctly! I've tested it on vue-cli project I have and these are the results Code added import * as _ from "radash";
console.log(_.first([1, 2, 3]));Here are the differences
|
Update tsconfig to target 'esnext' instead of 'es2018'
Set option 'preserveModules' to 'true' to keep module structure intact instead of combining everything into a single file. This way it should be easier for bundlers to three shake unused code
|
|
Following the guidelines provided here: https://github.com/microsoft/TypeScript/wiki/Node-Target-Mapping, I set tsconfig target and lib to 'es2019' to support node 12
Add exports and specific .cjs and .mjs extensions to make sure node uses the correct module loader Following documentaion on the official nodejs website: https://nodejs.org/api/packages.html#dual-commonjses-module-packages
|
No idea why the pipeline failed, it seems to be related to something I did not actually touch (and btw, with same node version test pass locally...). If you can @rayepps, rerun the workflow. Anyway, I added something with respect to the previous commit, which I will specify here: "exports": {
"import": "./dist/esm/index.mjs",
"require": "./dist/cjs/index.cjs",
"types": "./dist/index.d.ts"
},I added the exports section and I used the .cjs and .mjs extensions to make sure that node uses the correct loader. |
It is dumb tests, based on time: |
|
@matteosacchetto I love this! I'm so excited to merge it. A bunch of PRs were merged recently so there is a new conflict but it's an easy one in the @gander I would love to know how to improve that test cus I agree, it's dumb 😃 |
|
@rayepps I have resolved the conflict with the |
|
@matteosacchetto can you run I just published |
I run |
|
Again, issue on the same test mentioned above by @gander |
|
One additional cool thing: now it threeshakes also if you do |
|
I'm glad this was merged. When is it going to be released? |
|
@gustavopch it will go out in 2 days (Monday) in v8.0.0. I'm working on a few other changes for the major release and I also happen to currently be traveling 😅 |
* add: rollup, esbuild and rollup plugins - rollup - @rollup/plugin-typescript - rollup-plugin-dts - rollup-plugin-esbuild - esbuild * update: tsconfig.json Following changes: - update lib and target to es2018 - add esModuleInterop and set to true - add moduleResolution and set to node * add: rollup config file The configuration is designe to build: - declarations - cjs module - esm module * update: package.json exports and scripts Set the correct entrypoint for cjs and esm modules and update build script to perform a typecheck followed by a rollup build * update: typescript 4.4.2 -> 4.8.3 Update typescript to latest version due to an issue that version 4.4.2 has with rollup. For reference here is the issue: [Builds hang with typescript 4.4.2](rollup/plugins#983) * add: sideEffects to package.json Add 'sideEffects: false' to package.json to allow webpack (and possible other bundlers) to threeshake the code * fix: tsconfig target set to 'esnext' Update tsconfig to target 'esnext' instead of 'es2018' * fix: set rollup to preserve module structure Set option 'preserveModules' to 'true' to keep module structure intact instead of combining everything into a single file. This way it should be easier for bundlers to three shake unused code * fix: tsconfig target and lib set to 'es2019' Following the guidelines provided here: https://github.com/microsoft/TypeScript/wiki/Node-Target-Mapping, I set tsconfig target and lib to 'es2019' to support node 12 * fix: set package.json exports and use .cjs and .mjs extensions Add exports and specific .cjs and .mjs extensions to make sure node uses the correct module loader Following documentaion on the official nodejs website: https://nodejs.org/api/packages.html#dual-commonjses-module-packages * build: bump rollup to 2.79.1 * build: regenerate yarn.lock

With this pull request I am adding the support for both CJS and ESM module export. This will allow for example bundlers like webpack, rollup, vite, ... to threeshake any unnecessary code when writing web apps
To achieve this I ended up using rollup (easy to understand and configure) in combination with esbuild (for its speed)
I also update caniuse-lite to latest version (solved a warning during build) and typescript to latest version (due to an issue of version 4.4.2 (rollup/plugins#983)
This pull request relates to discussion #49
Resolves #79