A fascinating technique that can greatly reduce your code

It's a common way in most web developers that write JavaScript with ES6+ and then bundle it to ES5, so that it can run in all browsers. However, the modern browsers support ES6 natively so it's unnecessary to shipping a lot of polyfills.

I'm really excited to share you a technique that you can compile and serve two separate JavaScript bundles:

  • One bundle you are definitely already generating, which serve for legacy browsers with polyfills.
  • Another bundle has less code with no polyfills, which serve for modern browsers.

That is

In modern browsers, script with type="module" will be loaded and executed, and script with nomodule will be ignored.

And in legacy browsers, script with type="module" will be ignored because they can't recognize this attrbute, script with nomodule has no effect, it will be treated as usual.

Note: There's something you should know about

你可能感兴趣的:(webpack)