-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
esm module support #884
base: master
Are you sure you want to change the base?
esm module support #884
Conversation
fixes: auth0#655
any progress on this PR? |
This PR will cause issues with Rollup builds as Rollup does not support rollup/rollup#4597 @Trim if you are using ESM in the runtime or esbuild/vite, the best option for now is to use See auth0/node-auth0#832 For this library to support valid ESM, a different approach is probably needed, as Rollup does not appear it will support node.js |
This can be fixed by conditional exports https://nodejs.org/api/packages.html#conditional-exports nodejs import conditions are So just set this shim as https://github.com/rollup/plugins/blob/master/packages/node-resolve/README.md#exportconditions Of course rewrite in esm is a better option, but I think this shim still has its value and could work. |
@@ -3,6 +3,7 @@ | |||
"version": "9.0.0", | |||
"description": "JSON Web Token implementation (symmetric and asymmetric)", | |||
"main": "index.js", | |||
"module": "index.mjs", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"module": "index.mjs", | |
"exports": { | |
".": { | |
"module": "./index.js", | |
"import": { | |
"node": "./index.mjs" | |
}, | |
"require": "./index.js" | |
}, | |
"./package.json": "./package.json" | |
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested this locally with node and bundler rollup/webpack/esbuild, all works fine.
// index.mjs
import * as jwt from 'jsonwebtoken'
console.log(jwt)
rollup
rollup use condition ['default', 'module', 'import']
, so it will resolve to ./index.js
.
Previous rollup config with node-resolve and cjs plugins will still work. rollup without these plugins was broken and still be broken.
// rollup.config.mjs
import commonjs from '@rollup/plugin-commonjs';
import {nodeResolve} from '@rollup/plugin-node-resolve';
export default {
plugins: [nodeResolve(), commonjs()],
input: './index.mjs',
output: {
file: './build/bundle.min.js',
format: 'iife',
name: 'bundle'
}
}
webpack support this out-of-box
I'm not sure what's its condition, but at least it include module
and resolve to index.js
esbuild
a little bit complex here.
- if esbuild is called with
--platform=node
, it will use condition['default', 'import', 'node', 'module']
or['default', 'require', 'node', 'module']
(depend on source code), so the only important thing is to put"module": "./index.js",
in the top ofexports
object, otherwise esbuild will resolve to conditionimport.node
(index.mjs
).
(yes, exports
as json field are ordered, bundlers or runtime will try to resolve from top to end)
- if
--platform=node
is not given, condition won't includenode
, and it can only resolve tomodule
orrequire
, that'sindex.js
.
vite
vite doesn't use node
condition.
Summary
don't add package.json#module
, use exports
with conditions.
So no bundler's behavior will change, what's broken is still broken, and now nodejs can have a better esm support from this package
I'm encountering another problem. I'm using esbuild as esm loader in nodejs runtime with typescript, but cjs only exports make it impossible to write a valid type definition at For a cjs only package, the correct way to type it is But there is no way to use export assignment with esm exports in typescript. So have a (at least nodejs) esm would help this problem. |
@btakita I hope my comments explained the suggestion clearly. |
fixes: #655
By submitting a PR to this repository, you agree to the terms within the Auth0 Code of Conduct. Please see the contributing guidelines for how to create and submit a high-quality PR for this repo.
Description
References
Testing
Checklist