question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Deploy both ES5 and ES2015+ code in production

See original GitHub issue

Is this a bug report?

No

Everything described in this article.

But for lazy ones I’ll add some highlights here.

<!-- Browsers with ES module support load this file. -->
<script type="module" src="main.js"></script>

<!-- Older browsers load this file (and module-supporting -->
<!-- browsers know *not* to load this file). -->
<script nomodule src="main-legacy.js"></script>

Warning! The only gotcha here is Safari 10 doesn’t support the nomodule attribute, but you can solve this by inlining a JavaScript snippet in your HTML prior to using any <script nomodule> tags. (Note: this has been fixed in Safari Tech Preview, and should be released in Safari 11).

And here is the working example.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:15
  • Comments:20 (9 by maintainers)

github_iconTop GitHub Comments

6reactions
MidnightDesigncommented, Aug 16, 2018

…so this is dead? Has anyone built like a plugin or something? I would really love to have this feature for performance and debugging.

3reactions
geelencommented, Sep 14, 2017

Ok, after a few hours of hacking, this is what we’ve found:

  • It can’t be two entry points

This feels like the nicest idea, where you now have two entry points (in webpack.config.prod.js):

- entry: [require.resolve('./polyfills'), paths.appIndexJs],
+ entry: {
+   main: [require.resolve('./polyfills'), paths.appIndexJs],
+   esmodule: paths.appIndexJs,
+ }

But there’s no way to change babel-preset-env in the config, depending on the entry point.

  • Generating two webpack builds kinda sucks, but is maybe necessary

If you duplicate your production webpack config, you can handle things pretty well, but that has the weakness of… well… duplicating your production webpack config. @neeharv has gotten a build working pretty well using this so I might let him describe it.

One thing worth pointing out is that we had to use the beta version of UglifyJSPlugin to get the ecma config property, otherwise ES6 code would break. So CRA would probably need to bump to whatever version of webpack includes this, once it goes stable.

  • Adding a post-processing webpack plugin to generate an ES5 version could work

This is where my thinking is at atm. If your webpack plugin targets beautiful new ES6 code with no polyfills and very little transpilation, but then something comes along and transpiles it to ES5. I’ve played around with unminified-webpack-plugin a bit and tried to get something similar working, but don’t know webpack really well enough to ship this quickly. What it would need to do, though, is this:

  • Receive every JS chunk
  • Rerun babel on it with a preset-env of older browsers
  • Emit new chunks (with any dynamic imports rewritten)
  • Name them as .nomodule.js or something similar
  • Somehow instruct HTMLWebpackPlugin to inject the right script tags (type="module" for index.js, nomodule for nomodule.js)

This does feel like an achievable goal, and makes sense to be done as a webpack plugin. There are still questions around serviceworker, given that it reads from the manifest, but we haven’t got that far yet.


So that’s where we got to in a couple of hours hacking. I think this is a really cool idea and has the potential to dramatically cut down bundle sizes for modern browsers, so would love to see this progress!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deploying ES2015+ Code in Production Today - Philip Walton
When run, these two configs will output two, production-ready JavaScript files: main.mjs (the syntax will be ES2015+); main.es5.js (the ...
Read more >
Why does Angular build create files with 'es5' and 'es2015' but ...
Angular doesn't bundle the JavaScript files into a single file. You can add a build step to concat the files together. concat-build.js:
Read more >
Deploy Angular apps with ES2015, today - Medium
Our approach is to generate “two apps” from the same source code, ... we have a default app with ES2015 and a “legacy”...
Read more >
Deployment - Angular
For the simplest deployment, create a production build and copy the output directory to a ... ES5 support enabled, es2015, Differential loading (two...
Read more >
Understanding ES5, ES2015 and TypeScript - John Papa
What is the difference between ES5, ES2015 (formerly known as ES6), ... is a lot of ES5 code and it's good to understand...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found