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

esm module support #884

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions index.mjs
@@ -0,0 +1,9 @@
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
export const verify = require('./verify.js');
export const sign = require('./sign.js');
export const JsonWebTokenError = require('./lib/JsonWebTokenError.js');
export const NotBeforeError = require('./lib/NotBeforeError.js');
export const TokenExpiredError = require('./lib/TokenExpiredError.js');
export const decode = require('./decode');
export default { verify, sign, JsonWebTokenError, NotBeforeError, TokenExpiredError, decode }
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -3,6 +3,7 @@
"version": "9.0.0",
"description": "JSON Web Token implementation (symmetric and asymmetric)",
"main": "index.js",
"module": "index.mjs",
Copy link

@trim21 trim21 May 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"module": "index.mjs",
"exports": {
".": {
"module": "./index.js",
"import": {
"node": "./index.mjs"
},
"require": "./index.js"
},
"./package.json": "./package.json"
},

Copy link

@trim21 trim21 May 10, 2023

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.

  1. 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 of exports object, otherwise esbuild will resolve to condition import.node (index.mjs).

(yes, exports as json field are ordered, bundlers or runtime will try to resolve from top to end)

  1. if --platform=node is not given, condition won't include node, and it can only resolve to module or require, that's index.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

"nyc": {
"check-coverage": true,
"lines": 95,
Expand Down