Skip to content

Commit

Permalink
Support native ESM via .mjs.
Browse files Browse the repository at this point in the history
Relates to graphql#425.

- Swapped deprecated babel-preset-es2015 for babel-preset-env.
- Added package.json engines field.
- Configured babel-preset-env to target the Node.js version defined in package.json engines field.
- Kept Babel at v6, but moved config into a v7 ready .babelrc.js file for dynamic config via env.
- Tidied the package.json scripts, added an ESM build step that generates .mjs dist files. Follow the lead of graphql/graphql-js#1244, although I do things quite different for my own projects with Babel v7.
- Updated package.json main field for ESM/CJS cross compatibility.
- Added a package.json module field for tree-shaking bundlers.
  • Loading branch information
jaydenseric committed May 2, 2018
1 parent e8380dc commit 890fc03
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 54 deletions.
15 changes: 15 additions & 0 deletions .babelrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = {
presets: [
[
'env',
{
modules: process.env.ESM ? false : 'commonjs',
targets: {
node: require('./package.json').engines.node.substring(2), // Strip `>=`
},
},
],
],
plugins: ['transform-class-properties', 'transform-flow-strip-types'],
ignore: ['__tests__'],
};
21 changes: 12 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
"middleware",
"api"
],
"main": "dist/index.js",
"engines": {
"node": ">=4"
},
"main": "dist/index",
"module": "dist/index.mjs",
"directories": {
"lib": "./dist"
},
Expand All @@ -34,26 +38,25 @@
},
"babel": {
"presets": [
"es2015"
],
"plugins": [
"transform-class-properties",
"transform-flow-strip-types"
"./.babelrc.js"
]
},
"scripts": {
"prepublish": ". ./resources/prepublish.sh",
"test": "npm run lint && npm run check && npm run testonly",
"testonly": "mocha $npm_package_options_mocha",
"lint": "eslint src",
"prettier": "prettier --write 'src/**/*.js'",
"check": "flow check",
"build": "rm -rf dist/* && babel src --ignore __tests__ --out-dir dist && npm run build:flow",
"build": "npm run build:clean && npm run build:esm && npm run build:cjs && npm run build:flow",
"build:clean": "rm -rf dist/*",
"build:esm": "ESM=1 babel src -d dist/esm && for file in $(find dist/esm -name '*.js'); do mv \"$file\" `echo \"$file\" | sed 's/dist\\/esm/dist/g; s/.js$/.mjs/g'`; done && rm -rf dist/esm",
"build:cjs": "babel src -d dist",
"build:flow": "find ./src -name '*.js' -not -path '*/__tests__*' | while read filepath; do cp $filepath `echo $filepath | sed 's/\\/src\\//\\/dist\\//g'`.flow; done",
"watch": "node resources/watch.js",
"cover": "babel-node node_modules/.bin/isparta cover --root src --report html node_modules/.bin/_mocha -- $npm_package_options_mocha",
"cover:lcov": "babel-node node_modules/.bin/isparta cover --root src --report lcovonly node_modules/.bin/_mocha -- $npm_package_options_mocha",
"preversion": "npm test",
"prepublish": ". ./resources/prepublish.sh",
"start": "babel-node examples/index.js"
},
"dependencies": {
Expand All @@ -69,7 +72,7 @@
"babel-plugin-transform-class-properties": "6.24.1",
"babel-plugin-transform-flow-strip-types": "6.22.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "6.24.1",
"babel-preset-env": "^1.6.1",
"babel-register": "^6.26.0",
"babel-runtime": "^6.26.0",
"body-parser": "1.18.2",
Expand Down
Loading

0 comments on commit 890fc03

Please sign in to comment.