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 build for the browser #2277

Closed
wants to merge 5 commits into from
Closed

Conversation

lukejacksonn
Copy link

@lukejacksonn lukejacksonn commented Dec 6, 2019

Hey 👋 I'm a big advocate of buildstep-less architectures on the web, taking advantage of ES modules to eschew the need for bundling (es-react for example). This PR addresses #1819.

💭 Rationale

Recently I wanted to use urql in one of my projects that falls into this build free category. So I set about picking apart the dependencies of the package to see how far it was from being ESM ready. Probably unsurprisingly this package is the biggest dependency of urql so I started with fixing it up and this is what I ended up working with.

👨‍🔧 Changes

  • 316a69e Created a new build esm with a custom babel extension that appends .es.js to import/export statement sources. The differences between this and mjs build are subtle but this decision was made as not to disturb any existing builds. Explicit extensions are required according to the browser es module spec (also the most recent node es module spec).
  • 8e7cc02 Checks typeof process in instanceOf.js to prevent Cannot find property .env of undefined error when running the browser; without altering the node behaviour.
  • d27e528 Vendor'd the iterall package to eradicate the only bare specifier import and making the whole codebase dependency free!
  • d7210ec Makes paths in /index.js resolvable by extension only (due to /depencendy.js and /dependency/index.js inconsistency usually handled by nodes resolution algorithm)

📦 Output

This PR itself is a POC that I made for myself, I'm not precious about the implementation details but I am quite optimistic about the results.

Here is a published version of this PR: https://www.runpkg.com/?es-graphql@1.0.3/dist/index.es.js
Here is a demo of this build working with urql: https://es-urql.lukejacksonn.now.sh

Running yarn build && yarn testonly currently succeeds with no errors but type checking fails due to no definitions for the functions within iterall.js. If anyone has suggestions of how to achieve the same result with a different approach to any of the steps outlined by specific commits here, then I am all ears (I am no babel aficionado)!

🤔 Conclusion

This library required very few changes in order to make it just work in the browser. I'm almost certain that this would trivialise and encourage gql client library authors to generate es module builds themselves which will lower the barrier to entry for people getting started with GraphQL in general (example projects could now just be an index.html with no install or build process to explain).

IvanGoncharov added a commit to IvanGoncharov/graphql-js that referenced this pull request Jan 20, 2020
We used only couple functions from 'iterall' and it's the only
dependency we have at the moment.
Having no dependencies significantly simplify ESM builds and Deno
support in future
Partially resolves: graphql#2277

Bonus: All supported Node versions already natively support Array.from
so it should improve perfomance
IvanGoncharov added a commit that referenced this pull request Jan 20, 2020
We used only couple functions from 'iterall' and it's the only
dependency we have at the moment.
Having no dependencies significantly simplify ESM builds and Deno
support in future
Partially resolves: #2277

Bonus: All supported Node versions already natively support Array.from
so it should improve perfomance
@IvanGoncharov IvanGoncharov reopened this Jan 20, 2020
IvanGoncharov added a commit to IvanGoncharov/graphql-js that referenced this pull request Jan 20, 2020
IvanGoncharov added a commit that referenced this pull request Jan 20, 2020
IvanGoncharov added a commit to IvanGoncharov/graphql-js that referenced this pull request Jan 20, 2020
@@ -11,7 +11,8 @@ declare function instanceOf(

// See: https://expressjs.com/en/advanced/best-practice-performance.html#set-node_env-to-production
// See: https://webpack.js.org/guides/production/
export default process.env.NODE_ENV === 'production'
export default typeof process !== 'undefined' &&
process.env.NODE_ENV === 'production'
Copy link
Member

Choose a reason for hiding this comment

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

@lukejacksonn Can you please extract it into separate PR?
Also, please test that Webpack is able to recognize this pattern and switch to the production code.

Is it the last change we need to support ESM?

Copy link
Author

Choose a reason for hiding this comment

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

Yeh I can do that.. this is the last source modification required for ESM support (assuming that the mjs build now adds extensions to imports paths).

Copy link
Member

Choose a reason for hiding this comment

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

Yeh I can do that..

Great 👍 Thanks a lot!

assuming that the mjs build now adds extensions to imports paths

You can see preview build here:
https://github.com/graphql/graphql-js/blob/npm/graphql.mjs
If you have time to try it here is the instruction:
https://github.com/graphql/graphql-js#want-to-ride-the-bleeding-edge

Copy link
Member

Choose a reason for hiding this comment

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

Hi @lukejacksonn,
Did you have a time to test this change with webpack?
I planning to release new RC so it would be great to also include this change.

Copy link
Author

Choose a reason for hiding this comment

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

Hey @IvanGoncharov sorry this task completely slipped my mind. I have made a PR #2409 but am not so sure about what webpack specific config needs to be checked

@IvanGoncharov
Copy link
Member

Superseded by #2409

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.

2 participants