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

Build .mjs files (ES modules) alongside CommonJS #209

Closed
wants to merge 11 commits into from

Conversation

motiz88
Copy link
Contributor

@motiz88 motiz88 commented Apr 19, 2018

Closes #208, with the actual build changes lifted nearly verbatim from graphql-js (e.g. graphql/graphql-js#1244)

UPDATE: facebook/flow#6179 has landed and this branch incorporates the Flow 0.73 upgrade (which is also its own PR for good measure: #210)

@@ -7,6 +7,8 @@
* @flow
*/

import { Buffer } from './buffer';

Copy link
Member

Choose a reason for hiding this comment

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

As I understand facebook/flow#3723 was merged and will be available in the next release.
Does that mean you can just change this line to:

import Buffer from 'buffer';

and everything will work?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, sadly no. We'd be able to simplify buffer.js#L28-L37 to

export const Buffer = NodeBuffer.Buffer;

(which should now typecheck)

but we'd still have Node's actual implementation to deal with.

Under node --experimental-modules, import { Buffer } from 'buffer' fails because the buffer module doesn't export { Buffer } at all; it only has a default export with Buffer as a property. Hence, the rest of buffer.js is still needed for compatibility with both Node and Webpack, even once the Flow fix lands.

package.json Outdated
@@ -15,6 +15,8 @@
"url": "http://github.com/graphql/graphql-relay-js.git"
},
"main": "lib/index.js",
Copy link

Choose a reason for hiding this comment

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

This should be "main": "lib", so the .mjs files can be resolved automatically in an ESM environment.

@jaydenseric
Copy link

@motiz88 a similar effort graphql/express-graphql#433. You might like to adopt the Babel v7 ready approach to the dynamic config (.babelrc.js) here.

With .mjs emerging as the conventional file extension for ESM, it is better to rename the source files accordingly and use Babel's --keep-file-extension flag for the ESM build script, instead of a gross manual find and rename. Babel and other tools are beginning to leverage the source file .mjs extension in meaningful ways.

I didn't do it in my PR as I didn't want to rock the boat too much; I really need a speedy merge. Here are some published, working examples of the proposed approach:

* Flow (tested with 0.69.0) knows about global.Buffer but not about
* require('buffer').Buffer.
* https://github.com/facebook/flow/issues/3723
*/
Copy link
Member

Choose a reason for hiding this comment

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

@motiz88 Flow 0.72 released and it includes your fix 🎉 Can you please update this PR?

@motiz88 motiz88 mentioned this pull request May 26, 2018
@motiz88
Copy link
Contributor Author

motiz88 commented May 26, 2018

@IvanGoncharov Done, thanks for pinging me.

@jaydenseric re: .babelrc.js and switching to .mjs source files, that all looks super valid, but I intentionally took my cue here from the way graphql-js currently does things, e.g. graphql/graphql-js#1244. There's nothing stopping the maintainers of this package from changing the approach down the line if they deem it necessary.

@@ -9,7 +9,7 @@

import { expect } from 'chai';
import { describe, it } from 'mocha';
import { StarWarsSchema } from './starWarsSchema.js';
import { StarWarsSchema } from './starWarsSchema';
Copy link
Member

Choose a reason for hiding this comment

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

@motiz88 If you have time maybe it worth to split such changes into separate PR.
I think it would be easy to find someone from Facebook willing to merge such simple change
+ it will make the main PR smaller

@jaydenseric
Copy link

Guys, desperate for this. It seems there is nothing substantial blocking a merge?

I'm about to copy paste functions into my repo, write my own, or publish this PR as a disposable package to npm or something.

@WBad
Copy link

WBad commented Dec 24, 2018

Guys, desperate for this. It seems there is nothing substantial blocking a merge?

I'm about to copy paste functions into my repo, write my own, or publish this PR as a disposable package to npm or something.

@jaydenseric I did exactly that you can find the package on npm the code on gitlab

Base automatically changed from master to main February 10, 2021 15:10
@IvanGoncharov
Copy link
Member

@motiz88 Sorry for the long delay.
We plan to merge this lib into graphql-js to provide the better maintenance and attention it deserves.
This should happen over the next few months and so closing this since graphql-js already provides mjs files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Publish ESM (.mjs) entry point to npm
5 participants