Consider generating multiple bundles in favor of modern browsers #2114
Replies: 43 comments 10 replies
-
Huh super interesting! Would love to see someone set this up on gatsby! Should be doable as a plugin. Btw, the main benefit appears to be that you don't have to polyfill for newer browsers so the code loaded is significantly smaller. |
Beta Was this translation helpful? Give feedback.
-
That article seems confused (or I am)...the main jist is to not compile modules via babel. The problem with that tho is that you don't get a bundle out that has uncompiled es modules, you get a compiled webpack bundle with no modules. Babel won't compile the modules but webpack will. So you can use the "modern" babel config with any browser, in fact the v2 gatsby branch (which uses webpack2) already does this. |
Beta Was this translation helpful? Give feedback.
-
ahhhh I get it sorry i'm the dense one. The point is just that you can simplify feature detection by checking |
Beta Was this translation helpful? Give feedback.
-
@davidtheclark over at Batfish is working on a PR for a similar feature mapbox/batfish#188 This technique seems really promising. We'd need to see how much time it adds to the build but if it's not that much more, I'd definitely consider turning it on by default. If the extra build time hurts enough, we could make it optional. |
Beta Was this translation helpful? Give feedback.
-
@jquense I'm really into this approach, and have been applying parts of it in prod code. To summarize my understanding:
|
Beta Was this translation helpful? Give feedback.
-
Yup! And per our parallel polyfill discussion, the |
Beta Was this translation helpful? Give feedback.
-
I ended up holding off on that Batfish PR but implemented the plan on another build system tool — with the idea of bringing it into Batfish after Babel 7 is released. It worked well overall, but there were two hiccups:
|
Beta Was this translation helpful? Give feedback.
-
Yeah, the build twice is a pain. Webpack persistant caching might hep that, depending on how it’s implemented. Babel-minify worked well for me too. Not quite as small as uglify es branch, but not as many edge cases either. |
Beta Was this translation helpful? Give feedback.
-
For reference Meteor 1.7 introduced the ability to generate two different JavaScript bundles, one for legacy browsers that includes JS polyfills for features that aren't natively implemented by the browser, and one for modern browsers without these polyfills. The rationale for the Meteor design is exposed in meteor/meteor#9439 and https://blog.meteor.com/meteor-1-7-and-the-evergreen-dream-a8c1270b0901. |
Beta Was this translation helpful? Give feedback.
-
Cool! Super nice to see Meteor is doing this. This is a very high priority to ship with v2. |
Beta Was this translation helpful? Give feedback.
-
@KyleAMathews I'd be happy to discuss how this might work in Gatsby! It's a nice selling point for Meteor right now, but I think it's safe to say Gatsby's users have different needs, and are not about to switch to Meteor. Most of my thoughts are in the blog post that @mquandalle linked above, but here's a slightly shorter explanation that I recently typed in a FB comment:
|
Beta Was this translation helpful? Give feedback.
-
One quick question: does Gatsby have any mechanism for delivering different HTML to different browsers, or is the |
Beta Was this translation helpful? Give feedback.
-
Gatsby probably needs to rely on |
Beta Was this translation helpful? Give feedback.
-
I think that's a perfectly reasonable strategy, though I just finished tweeting some thoughts on the limits of that approach: https://twitter.com/benjamn/status/1005104014944538624 |
Beta Was this translation helpful? Give feedback.
-
Vue is shipping with this as an optional mode for builds https://cli.vuejs.org/guide/browser-compatibility.html#modern-mode |
Beta Was this translation helpful? Give feedback.
-
This is how vue-cli does modern builds https://github.com/vuejs/vue-cli/blob/38efc032e5c0d5ab8dd74602c1fb100fef62d766/packages/%40vue/cli-service/lib/webpack/ModernModePlugin.js |
Beta Was this translation helpful? Give feedback.
-
This is the article that pulled me into this topic: https://nystudio107.com/blog/an-annotated-webpack-4-config-for-frontend-web-development It doesn't use HtmlWebpackPlugin (at least not for that) but is still a great example of a complete use of multi-target compilations: https://github.com/nystudio107/annotated-webpack-4-config/ |
Beta Was this translation helpful? Give feedback.
-
@KyleAMathews and @DSchau you were working on this recently I think? Could you post an update of where you got to - was there some issues with handling multiple webpack configs? |
Beta Was this translation helpful? Give feedback.
-
@m-allanson thanks for tagging us in! We did. Here's where we ended up (with more work on this probably coming this week): The current state of the work is here. What we'll need to do is do two webpack builds (ideally in parallel, utilizing something like jest-worker), one with a modern config, and one with the legacy config. Our approach is tentatively going to be opt-out, e.g. Current blockers/issues/things to solve:
Additional things that should probably be solved:
I think this is a decent intro on the current state of things. More to come soon! |
Beta Was this translation helpful? Give feedback.
-
Hiya! This issue has gone quiet. Spooky quiet. 👻 We get a lot of issues, so we currently close issues after 30 days of inactivity. It’s been at least 20 days since the last update here. If we missed this issue or if you want to keep it open, please reply here. You can also add the label "not stale" to keep this issue open! Thanks for being a part of the Gatsby community! 💪💜 |
Beta Was this translation helpful? Give feedback.
-
Hey, sorry about the radio silence. A bunch of stuff came up resulting in me not being to continue working on this feature. Still really hoping to see this get done, please let me know if there's anything I can help with! |
Beta Was this translation helpful? Give feedback.
-
See: #12761 for a WIP PR @wardpeet and I worked on to use microbundle to publish our browser dependencies (e.g. gatsby-image, gatsby-link) as es and cjs bundles. We're waiting for some upstream changes to microbundle, then we can merge those in! This will get us some benefits as far as file size shipped to the browser to transpile less code. More to come! |
Beta Was this translation helpful? Give feedback.
-
slowly making microbundle ready for modern builds |
Beta Was this translation helpful? Give feedback.
-
Is there a status update on this? |
Beta Was this translation helpful? Give feedback.
-
more links |
Beta Was this translation helpful? Give feedback.
-
Has anyone identified even the correct pattern for doing this in webpack? I've wanted to set this up multiple times but just completely unsure waht the Right Way to even integrate it into webpack is |
Beta Was this translation helpful? Give feedback.
-
@jquense If you're already running a MultiCompiler, the best solution is to add an additional top-level compiler pass. Otherwise, babel-esm-plugin performs the The problem with all current approaches is that they don't extend the benefits of modern JS to code obtained from node_modules. Thankfully, there has recently been some solid movement towards a standardized mechanism for exposing modern JS in npm packages, and I'm hoping to have a demonstration of this soon. It will take a while for packages to adopt the approach, but shipping it as a default in microbundle and tsdx can serve as a nice head-start. |
Beta Was this translation helpful? Give feedback.
-
I ended up here while searching how to get rid of 80kb of polyfill.js from the bundle. :) I guess there is not an easy solution for newbies yet. |
Beta Was this translation helpful? Give feedback.
-
any updates regarding this, I'm using is there a way to Differential Serving in Gatsby? |
Beta Was this translation helpful? Give feedback.
-
Any updates on this? I'm using |
Beta Was this translation helpful? Give feedback.
-
According to this article, modern browsers may profit from bundles loaded with support for the ES2015 module syntax. Using the methods described in the article may also reduce server load because of the reduced bundle sizes.
Beta Was this translation helpful? Give feedback.
All reactions