1
0
mirror of https://github.com/lancedikson/bowser synced 2026-10-28 14:29:25 +00:00

fix: keep ESM consumers on ES5, enable tree-shaking, trim bundled.js

Found by a differential test of the packed candidate against the published
2.14.1 across Node 12-22, Bun, TypeScript 4.7-7.0, seven bundlers and a
real browser.

- bowser.mjs is now lowered to ES5 syntax (keeping `export`). Before the
  exports map, webpack, Rollup and esbuild resolved `import 'bowser'` to
  es5.js via the `browser` field; the ES2015 bowser.mjs broke esbuild
  `--target=es5` and shipped classes into webpack ES5 builds. The ESM build
  sets `minify: false` explicitly, because rolldown's default DCE-only pass
  re-prints the chunk after renderChunk and restores shorthand properties.
- `"sideEffects": ["./bundled.js"]` lets esbuild, Vite and webpack 4 drop an
  unused `import 'bowser'`; bundled.js stays listed so polyfill-only imports
  are never tree-shaken away.
- `./package` export restores extensionless `require('bowser/package')`,
  which resolved on 2.14.1.
- bundled.js imports `core-js/es` plus the timers, immediate and
  DOM-collection web polyfills instead of `core-js/stable`, dropping URL,
  URLSearchParams and queueMicrotask, which no released bundled.js ever
  shipped: 176.9 kB -> 156.1 kB (56.6 -> 49.3 kB gzip).

Tests: bowser.mjs must parse as ES5 apart from its export; bundled.js must
install the @babel/polyfill surface 2.14.1 shipped (passes on 2.14.1's
bundled.js, fails on unpolyfilled es5.js); package smoke test covers
`bowser/package` and `sideEffects`.
This commit is contained in:
naorpeled
2026-09-27 01:04:05 +03:00
parent c1208b4a56
commit 7b144d8247
6 changed files with 119 additions and 23 deletions

View File

@@ -2,14 +2,23 @@
* UMD entry point for `bundled.js` — same as `es5.js`, but with the polyfills
* needed by the browser targets baked in.
*
* `core-js/stable` + `regenerator-runtime/runtime` is the core-js@3 equivalent
* of the deprecated `@babel/polyfill` that this bundle used to be built from.
* `@babel/preset-env`'s `useBuiltIns: 'entry'` rewrites the `core-js/stable`
* import below into just the polyfills the configured targets actually need.
* This is the core-js@3 equivalent of the deprecated `@babel/polyfill` that
* this bundle used to be built from, which was: all of ECMAScript, the
* `timers`, `immediate` and DOM-collection iterator web polyfills, and
* `regenerator-runtime`. `@babel/preset-env`'s `useBuiltIns: 'entry'` rewrites
* the `core-js/*` imports below into the modules the configured targets need.
*
* `core-js/es` rather than `core-js/stable`: `stable` is `es` plus every web
* polyfill, and the extra ones — `URL`, `URLSearchParams`, `queueMicrotask` —
* were never in any released `bundled.js`, yet cost ~20 kB (~7 kB gzipped).
* `test/acceptance/test-es5-runtime.js` pins the `@babel/polyfill` surface.
*
* See `./es5.js` for why this only re-exports the default.
*/
import 'core-js/stable';
import 'core-js/es';
import 'core-js/web/dom-collections';
import 'core-js/web/immediate';
import 'core-js/web/timers';
import 'regenerator-runtime/runtime';
import Bowser from '../../src/bowser.js';