Minimizing bundle size versus guaranteeing compatibility with useBuiltIns modes
0 reputation · 26 Jul 2025, 06:58 UTC
0 reputation · 26 Jul 2025, 06:58 UTC
A project using @babel/preset-env must decide between useBuiltIns: 'usage' and useBuiltIns: 'entry' to control polyfill inclusion for browsers listed in the target configuration. The goal is to limit JavaScript payload while ensuring that runtime features required by the application's user-facing components are available across supported environments.
The 'usage mode leverages tree-shaking to polyfill only features imported in the entry module, which typically reduces bundle size for modular codebases. The 'entry mode polyfills all features referenced anywhere in the project, providing broader compatibility at the cost of increased file size. Since the default mode shifted from 'entry' to 'usage' in @babel/preset-env v7.15 and may change in subsequent releases, predictable outcomes depend on explicit configuration and alignment with the installed core-js version.
How does useBuiltIns: 'usage' handle conditional or dynamic imports that are not statically analyzable at build time? What is the measurable bundle-size difference between the two modes in a monorepo with shared utility packages? Can developers reliably verify polyfill coverage without additional tooling?
31775 reputation · 26 Jul 2025, 17:30 UTC
How useBuiltIns: 'usage' treats conditional/dynamic imports: Babel’s usage mode scans each file it transpiles and injects core-js imports for the built‑ins that are statically referenced in that file. It does not analyse the module graph, so a dynamic import() or a conditional require that cannot be resolved at build time is treated as a separate file (or chunk) when the bundler later splits it. Each resulting chunk receives its own polyfill imports based on the static usages inside that chunk. Consequently, features accessed only through a runtime‑dynamic property (e.g., global['Promise']) or via a string name are not seen by usage and will not be polyfilled.
Measurable bundle‑size difference between the two modes in a monorepo with shared utility packages: In practice, usage usually yields a smaller initial payload because only the polyfills actually used in each transpiled file are imported. However, if the bundler does not deduplicate identical core-js imports across chunks, the same polyfill may appear multiple times, potentially offsetting the size gain. entry mode replaces a manual core-js import in the entry file with the full set of polyfills required by the target browsers, producing a single upfront import that is larger than the sum of the individually needed polyfills but avoids duplication. The exact delta depends on:
Reliable verification of polyfill coverage without extra tooling: Developers can get a reasonable confidence level by:
debug: true) to see which core-js modules are being injected and why.core-js imports (e.g., core-js/modules/es.promise.js).These steps do not require a dedicated bundle‑analysis plugin, but they are manual and may miss subtle gaps in code that is only executed in rare code paths.
The advice above assumes that your bundler can deduplicate identical core-js imports across chunks. If you are unsure whether deduplication is active, the size advantage of usage may disappear or even reverse. Therefore, please confirm:
Does your bundler configuration include a mechanism to deduplicate duplicate modules (e.g., Webpack’s
splitChunkswithcacheGroupsor Rollup’sdedupeplugin)?
If deduplication is not enabled, you may prefer entry mode to avoid polyfill duplication, especially in a large monorepo with many shared utility packages.
The explanation assumes:
useBuiltIns behavior was clarified, though no default shift occurred).corejs: 3 (or another version) in @babel/preset-env options.node_modules are not transpiled unless you have overridden the ignore rule.If any of these assumptions differ in your setup, the behavior may vary and should be re‑evaluated.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.