-
Notifications
You must be signed in to change notification settings - Fork 538
Support native ESM via .mjs. #433
base: main
Are you sure you want to change the base?
Conversation
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.
Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need the corporate CLA signed. If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks! |
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks! |
Don't merge yet, I'm noticing that |
This one needs careful consideration.
Looks great 🎉
Please ping me when you solve this issue or if you need help with it.
If you have time you can help to migrate
Maybe it also make sense to add
No need to support |
This is a nesesary breaking change. This is consistent with the graphql package, which only has named exports.
…nto mjs # Conflicts: # src/__tests__/usage-test.js
As a breaking change, I updated the exports to be only named for ESM/CJS cross compatibility and consistency. For a native ESM environment: import { graphqlHTTP, getGraphQLParams } from 'express-graphql' For a legacy CJS environment: - const graphqlHTTP = require('express-graphql')
- const getGraphQLParams = graphqlHTTP.getGraphQLParams
+ const { graphqlHTTP, getGraphQLParams } = require('express-graphql') The old way the additional There is no concept of mixed default/named exports in CJS, only a babel convention of a // ESM:
import graphqlHTTP, { getGraphQLParams } from 'express-graphql'
// CJS:
const { default: graphqlHTTP, getGraphQLParams } = require('express-graphql') |
- Configured Travis to test on the latest stable Node.js version (v9-10+ were not being tested!), and the lowest supported version which is now v6. Removed unessesary versions in between. - Updated package.json engines field, which in turn updates transpilation via dynamic Babel preset-env config.
@@ -2,10 +2,8 @@ language: node_js | |||
|
|||
# https://github.com/nodejs/LTS | |||
node_js: | |||
- "8" |
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.
8
is still in Active LTS
and we should definitely test it + we need to test 10
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.
v10 is tested as it is the current stable release used via 'node'
.
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 prefer to use explicit version, especially for consistency with graphql-js
and other graphql/*
repos.
{ | ||
modules: process.env.ESM ? false : 'commonjs', | ||
targets: { | ||
node: require('./package.json').engines.node.substring(2), // Strip `>=` |
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.
Until babel/babel#7277 is implemented I think it's better to use explicit string.
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.
Why? Then you would not have a single source of truth.
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.
Because this construct assumes >= <version>
and it's very brittle approach.
I personally think engines
should contain only node version that we run tests against. See my PR for graphql-js
: https://github.com/graphql/graphql-js/pull/1338/files#diff-b9cfc7f2cdf78a7f4b91a753d10865a2R21
@jaydenseric I would prefer not make such breaking changes especially since the absolute majority of users don't use |
For starters, that plugin does not, and likely will not support babel v7. The best way forward is to tweak the API to properly support both environments. As a best practice, no npm package should have mixed named/default exports. Only default, or only named. Only default is probably an inferior option if you expect your API to grow, as you can't export more stuff without a breaking change to named exports. The new API would be consistent with This issue is a dealbreaker for me and others I know. Because Apollo does not support native ESM either I've begun work on a new GraphQL server package. |
Hi 👋 ! Node core and Node Module WG member here. There is no set date for when Adopting The |
Things were moving too slow so I published graphql-api-koa, and have been enjoying writing resolvers in native ESM for a month now without issue. |
We're about a month away from Node 12 going into LTS. What happened to this feature? |
Node.js natively supports ESM via
.mjs
(currently behind the--experimental-modules
flag, soon to be unflagged).At the moment, consumers using native ESM,
graphql
andexpress-graphql
are in a conundrum (see #425). When they usegraphql
the ESM.mjs
modules run. Whenexpress-graphql
internally usesgraphql
, the CJS.js
files run.graphql
thinks there are multiple versions being used and throws a tantrum. This issue will be mitigated by supporting native ESM across both packages.babel-preset-es2015
forbabel-preset-env
.package.json
engines
field.babel-preset-env
to target the Node.js version defined inpackage.json
engines
field..babelrc.js
file for dynamic config via env.package.json
scripts, added an ESM build step that generates.mjs
dist files. Follows the lead of Use .mjs for module code graphql-js#1244, although I do things quite different for my own projects with Babel v7.package.json
main
field for ESM/CJS cross compatibility.package.json
module
field for tree-shaking bundlers.I didn't upgrade Babel to v7 to limit the scope of this PR. It would be a good idea though.