-
Notifications
You must be signed in to change notification settings - Fork 61
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
[WIP] expose ESM browser build and remove fetch and Promise polyfills - fixes #57 and #59 #60
Conversation
Hey @nsivertsen thanks a lot for this PR! I came across it while searching for the best way to support multiple environments and it was very helpful. I blatantly copied some parts of the rollup config for one of my projects. 😄 I would recommend adding a Thanks again! edit, just noticed you're in Berlin - happy to buy you a coffee sometime 😄 |
Hey @nsivertsen, thank you so much for your work, I'm sorry that our team did not take care of this earlier. I'll merge this and write the documentation in the coming days! |
@hypervillain oh, I didn't expect this to still be merged. That's great news! I see you've also removed Ramda from |
hi @nsivertsen we do have some questions about a few of the todo's left in the readme. |
hi @MarcMcIntosh I don't have much time before next week, unfortunately, but perhaps it's an easy question? Are you referring to "add section on bundling for node and browsers to README"? I saw that you're requiring let fetch = window && window.fetch;
if (SERVER_SIDE) {
fetch = require('node-fetch');
}
export default fetch; And then import the above wherever Note that I haven't tried the above, but I think it should work. |
Hi @nsivertsen that's exactly what I meant :) |
@MarcMcIntosh Promise and fetch are the only two features that were polyfilled in the previous version of the library, so it seems that is the case |
@nsivertsen That's good to know, |
@MarcMcIntosh As mentioned above, I wouldn't require anything special in Node (and rather include node-fetch by default, since it will always be needed). For browsers, it should be stated that polyfills for fetch and Promise are required if the user is targeting older browsers. Also, for people not using standard bundlers such as webpack or rollup, it might be helpful to know that module resolution for browsers relies on the {
"main": "dist/prismic-javascript.js",
"module": "dist/prismic-javascript.mjs",
"browser": {
"./dist/prismic-javascript.js": "dist/prismic-javascript.browser.js",
"./dist/prismic-javascript.mjs": "dist/prismic-javascript.browser.mjs"
}
} How this works is that Node code that uses CommonJs gets the When bundling for the browser, we want the code without Webpack will automatically check the Rollup, on the other hand, won't. It will need to be configured in I hope that helps! |
@nsivertsen Thanks for clearing that up, we will need to update the read me so users can have that information available to them :) |
Background
As mentioned in #59,
prismic-javascript
currently only exports a UMD build that is not tree-shakeable by bundlers and therefore forces consumers of the library to include everything in their bundles, instead of only the functions they actually use.In addition, as mentioned in #57,
prismic-javascript
currently forces down polyfills forfetch
andPromise
. By now, everyone know that JS is the most expensive [1], [2] resource on most websites. Since browser support for fetch (87.1%) and Promise (88.85%) is actually quite good, and most real-world projects already have some polyfilling strategy (such as polyfill.io) in place (meaning that in the worst case, users are forced to download the polyfills twice), it feels a little unnecessary to include them here.I've therefore removed
promise-polyfill
and replacedcross-fetch
withnode-fetch
(which is used internally bycross-fetch
when run on Node), which useswindow.fetch
when bundled for the browser. Even before tree-shaking, this already reduces the bundle size ofprismic-javascript.min.js
from 31.9KB to 13.5KB.Description
Aside from the existing
dist/prismic-javascript.min.js
UMD build, this PR adds the following builds:dist/prismic-javascript.js
: CJS build for Node.jsdist/prismic-javascript.mjs
: ESM build for Node.jsdist/prismic-javascript.browser.js
: UMD build for browsersdist/prismic-javascript.browser.mjs
: ESM build for browsersIt uses the
package.browser
field, which is understood by all major bundlers (Webpack, Rollup, Browserify), to provide the browser bundles.Since Webpack still doesn't support outputting ES modules, I was forced to switch to Rollup, but I think you'll find the change quite pleasant as builds are faster and the configuration file is simpler (especially when considering it now outputs five bundles rather than one).
Unfortunately, because polyfills are no longer shipped with the bundle, this is potentially a breaking change for some people (but Semver protects us from that), but one I hope you'll agree is favourable in the long-term and might consider including in the upcoming 2.0 release. 🤞
There are some things left to do (see below), but I thought I'd leave this here already for discussion.
Thank you!
Todo
dist/prismic-javascript.esm.js
todist/prismic-javascript.mjs
for Node compatibilityFixes #57, fixes #59