Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
TypeScript output es2015, esm (ES Modules), CJS, UMD, UMD + Min + Gzip. Assumes you install typescript (tsc), rollup, uglifyjs either globally or included as devDependencies
{
"scripts": {
"build": "npm run build:es2015 && npm run build:esm && npm run build:cjs && npm run build:umd && npm run build:umd:min",
"build:es2015": "tsc --module es2015 --target es2015 --outDir dist/es2015",
"build:esm": "tsc --module es2015 --target es5 --outDir dist/esm",
"build:cjs": "tsc --module commonjs --target es5 --outDir dist/cjs",
"build:umd": "rollup dist/esm/index.js --format umd --name YourLibrary --sourceMap --output dist/umd/yourlibrary.js",
"build:umd:min": "cd dist/umd && uglifyjs --compress --mangle --source-map --screw-ie8 --comments --o yourlibrary.min.js -- yourlibrary.js && gzip yourlibrary.min.js -c > yourlibrary.min.js.gz",
}
}
@jaydenseric

This comment has been minimized.

Copy link

jaydenseric commented Sep 26, 2018

For native ESM in Node.js to work via --experimental-modules, the output should be sibling .js (CJS) and .mjs (ESM) files. The package.json main field should then just be index - leave off the extension. That way the environment will resolve the right file (index.mjs or index.js) automatically, as in --experimental-modules mode .mjs resolves before .js.

For an example that works, see https://github.com/jaydenseric/graphql-api-koa: https://registry.npmjs.org/graphql-api-koa/-/graphql-api-koa-1.1.0.tgz

@jdalton

This comment has been minimized.

Copy link

jdalton commented Oct 6, 2018

👋 Node core and Node Module WG member here. There is no set date for when --experimental-modules will be unflagged. There's also no guarantee that what has shipped now is what will be in the future. For example, there are several discussions in the Node Module WG on how to approach implementation. One such discussion is to ship a maximally minimal implementation that would require package-name-maps for resolving bare specifiers (no longer using the Node module resolution algorithm).

Adopting .mjs with the idea that its current support is solid (for production use) at this stage is bit premature and may cause headaches for users. I've already seen .mjs cause issues for real projects like react-apollo, graphql, and create-react-app due to complications with babel, webpack, testing/mocks, or dual packages Also, if/when changes do happen to Node's --experimental-modules your ESM story could be various shades of broken.

The --experimental-modules flag really does mean experimental. It's something you should pause and consider at least before going all in as the route for your ESM support strategy today.

@dandv

This comment has been minimized.

Copy link

dandv commented May 4, 2019

The --experimental-modules flag really does mean experimental.

Just an update that Node v12 reworked the experimental modules support, and expects to drop the flag in October 2019.

Also, if anyone wants to reply here, please keep in mind that Gists still don't send notifications for comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.