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.

bundle size worse than preset-env with esmodules:true ?

See original GitHub issue

I was looking into migrating a project that I maintain to preset-modules. However, I’ve noticed that the resulting bundle size is actually bigger than when I use preset-env with esmodules:true.

I’m not entirely sure that this is the fault of preset-modules, but I can’t seem to figure out why it’s behaving the way that it is.

To reproduce:

If you clone single-spa, checkout the 5.0 branch, yarn install, and then run yarn build:analyze, here’s the output (which is using preset-env with targets:{esmodules:true} - focusing only on the esm bundle:

./src/single-spa.js → ./lib/esm/single-spa.dev.js...
-----------------------------
Rollup File Analysis
-----------------------------
bundle size:    42.974 KB
original size:  53.199 KB
code reduction: 19.22 %
module count:   23

Then, if you yarn add -D @babel/preset-modules, change the .babelrc file to use @babel/preset-modules (for the “esm” environment), and then run yarn build:analyze again, here’s the result:

./src/single-spa.js → ./lib/esm/single-spa.dev.js...
-----------------------------
Rollup File Analysis
-----------------------------
bundle size:    45.098 KB
original size:  96.12 KB
code reduction: 53.08 %
module count:   24

If I do a comparison between the two compiled files, it appears that the size difference comes down to two things: 1)there’s an additional function added:

function _typeof(obj) {
  if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
    _typeof = function (obj) {
      return typeof obj;
    };
  } else {
    _typeof = function (obj) {
      return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
    };
  }

  return _typeof(obj);
}

and 2) all the arrow functions were compiled back to normal function s.

I’m not sure why either of these things are happening, especially the transpilation of arrow functions to normal functions.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
frehnercommented, Apr 8, 2020

confirmed to work while using latest babel and "bugfixes": true

1reaction
frehnercommented, Feb 19, 2020

I finally got a minute to look at this more closely, and I realized what the problem is: you’re using both @babel/preset-env and @babel/preset-modules. Babel overrides, such as the "esm" one you’re using, do not replace plugins and presets wholesale - they get merged. Since @babel/preset-env and @babel/preset-modules are different preset names, they are both included in the resolved configuration.

This will sort itself out once babel/babel#11083 is merged - you’ll be able to simply have your "esm" override set { "target": { "esmodules": true }, "bugfixes": true } and it’ll apply preset-modules optimizations.

Ah, thanks, I wasn’t aware that babel behaved that way.

Thank you for looking into this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What you don't know about BabelJS preset-env - PerimeterX
What are you missing beneath the hood of BabelJs preset-env? Read about how to configure it to save bundle size and leverage the...
Read more >
Webpack 5 and ESM - Stack Overflow
I want: To use webpack to bundle my web app up; To use ES Modules within my source js and have them transpiled...
Read more >
babel/preset-env
babel/preset-env` is a smart preset that allows you to use the latest JavaScript without needing to micromanage which syntax transforms (and optionally, ...
Read more >
Understanding (all) JavaScript module formats and tools
Webpack module: bundle from CJS, AMD, ES modules ... defineProperty(exports, '__esModule', { value: true }); }; // create a fake namespace ...
Read more >
Everything you can do to reduce bundle size for webapps
Non-ESModule dependencies. One of the most common causes of bundle size bloat are modules that can't be tree-shaken, such as CommonJS modules.
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