Repository navigation
Commit a1f0fb1
authored
feat: keep module side-effect information in the build output (#21789)
* feat: keep module side-effect information in the build output
Annotate the instantiation of a module webpack knows to be side-effect free
with `/*#__PURE__*/`, so a bundler consuming the emitted bundle can drop it
when nothing reads its exports.
Refs #17121
* feat: annotate every module instantiation webpack emits as a statement
Carry the side-effect information into the deferred import, the eager slot of
a wrapped concatenation member and the three entry-startup calls as well.
Annotating an expression a caller may go on to invoke is not safe: a minifier
binds the annotation to the outermost call, so `f(id)(x)` loses `(x)` with it.
The entry-startup calls skip it where the module id is assigned inside the
argument, and the entry IIFE keeps it for writing into `__webpack_exports__`
rather than returning it.
Refs #17121
* test: cover the deferred module instantiations
Drop the annotation from the eager slot of a wrapped concatenation member: the
root is the only member ever reached that way, and every shape that wraps it
routes a `require()` through its own imports, so it is never side-effect free
there and the annotation could not be emitted.
Refs #17121
* chore: regenerate types for the annotation helper
Refs #17121
* fix: keep the side-effect annotations in a minified library
Terser drops `/*#__PURE__*/` unless asked not to, so a production library build
erased the information it had just been given. Preserve annotations in the
default JavaScript minimizer options whenever the output is consumed as a
library.
Refs #17121
* feat: report an entry whose exports a consumer cannot drop
An entry other modules reference is not inlined, so its exports are read off the
exports object and no bundler consuming the library can drop them. Report that
through the analyzable-ESM bailout channel, where the answer is actionable.
Refs #17121
* chore: fold the changeset into one entry
Refs #17121
* test: build the annotation matcher without hand-rolled escaping
CodeQL flagged the escape set for omitting backslashes. Spell the pieces out
already escaped for a regexp instead, which drops the replace entirely.
Refs #17121
* test: snapshot the deferred bundles and shorten the changeset
Review feedback: the emitted code is what these two assertions check, so let the
whole bundle be the snapshot rather than pinning one substring of it, and bring
the changeset summary back under 80 characters.
Refs #17121
* docs: tighten the annotation comments
Review feedback: keep both to the two short lines the guide asks for, without
losing the minifier binding rule a new call site has to know.
Refs #17121
* test: name the deferred bundle snapshots
The two compilers run concurrently, so an unnamed snapshot keys on call order and
the two bundles swapped places between runs. Key each on its bundle name instead.
Refs #17121
* test: shorten the snapshot helper's JSDoc
Review feedback: comments in test/ are at most two short lines, so keep the type
annotation and drop the prose.
Refs #17121
* fix: do not annotate the call that starts an entry
An entry runs by contract, not because something imports it, so a minifier that
drops the startup call leaves the entry unexecuted — `all-combined` built nine
cases whose bundles then registered no tests at all. Side-effect freedom says an
importer may skip a module; it does not say the entry may never run.
Refs #17121
* test: keep the deferred snapshots off the running node version
The case pinned its target to the node running it, so the emitted runtime tracked
that version — Object.hasOwn on a new one, the hasOwnProperty fallback on Node 14
— and the snapshot could not hold across the matrix. Use the conservative target
the harness defaults to, as the repo's other bundle-snapshot case does.
Refs #17121
* test: assert the surviving annotation on the asset, not at runtime
Reading the bundle back with `require("fs")` made the ESM output import
`createRequire` from `node:module`, which Node 10 does not have.
* test: refresh the deferred snapshots for the leaner define-getters runtime
The rebase picks up #21803, which shortens `__webpack_require__.d`. The
annotations under test are unchanged.
* fix: pass the answer `_analyzableBailout` now requires
Main gave the helper a third argument it hands back; the public wrapper
only reports, so it answers with `null`.
* feat: write the side-effect annotations only into ESM output
Output another build reads back is the only output the annotation can pay
for. Over configCases the gate is -1.51 KiB raw / -635 B gzip: 73 assets
lose an annotation nothing consumed, 21 defer-import ones grow because
their own minifier pass was using it.
* test: drop the annotations from a non-ESM snapshot
`static-export-bindings` builds a script bundle, so the ESM gate no longer
writes them there.
* docs: shorten a test comment to the two-line limit
* docs: shorten a comment to the three-line limit
* fix: preserve the annotations only for an ESM library
A script-loaded output — a federation container above all — carries no
annotations of its own, so telling terser to keep them only kept the ones
webpack has always emitted and terser used to drop.1 parent 3d2bec0 commit a1f0fb1
37 files changed
Lines changed: 2609 additions & 8 deletions
File tree
- .changeset
- lib
- config
- javascript
- optimize
- template
- test/configCases/side-effects
- 0-pure-annotation-library
- 1-pure-annotation-consumer
- pure-annotation-minified
- pure-deferred-import
- __snapshots__
- pure-entry-not-inlined-bailout
- pure-harmony-import
- __snapshots__
- pure-module-instantiation
- __snapshots__
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
578 | 578 | | |
579 | 579 | | |
580 | 580 | | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
581 | 585 | | |
582 | 586 | | |
583 | 587 | | |
| |||
2444 | 2448 | | |
2445 | 2449 | | |
2446 | 2450 | | |
| 2451 | + | |
2447 | 2452 | | |
2448 | 2453 | | |
2449 | 2454 | | |
2450 | 2455 | | |
2451 | 2456 | | |
2452 | | - | |
| 2457 | + | |
2453 | 2458 | | |
2454 | 2459 | | |
2455 | 2460 | | |
| |||
2496 | 2501 | | |
2497 | 2502 | | |
2498 | 2503 | | |
2499 | | - | |
2500 | | - | |
2501 | | - | |
| 2504 | + | |
| 2505 | + | |
| 2506 | + | |
| 2507 | + | |
| 2508 | + | |
| 2509 | + | |
| 2510 | + | |
| 2511 | + | |
| 2512 | + | |
| 2513 | + | |
| 2514 | + | |
| 2515 | + | |
2502 | 2516 | | |
2503 | 2517 | | |
2504 | 2518 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1715 | 1715 | | |
1716 | 1716 | | |
1717 | 1717 | | |
| 1718 | + | |
| 1719 | + | |
| 1720 | + | |
| 1721 | + | |
| 1722 | + | |
| 1723 | + | |
1718 | 1724 | | |
1719 | 1725 | | |
1720 | 1726 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2820 | 2820 | | |
2821 | 2821 | | |
2822 | 2822 | | |
2823 | | - | |
| 2823 | + | |
2824 | 2824 | | |
2825 | 2825 | | |
2826 | 2826 | | |
| |||
2934 | 2934 | | |
2935 | 2935 | | |
2936 | 2936 | | |
2937 | | - | |
| 2937 | + | |
| 2938 | + | |
| 2939 | + | |
| 2940 | + | |
2938 | 2941 | | |
2939 | 2942 | | |
2940 | 2943 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
835 | 835 | | |
836 | 836 | | |
837 | 837 | | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
| 844 | + | |
| 845 | + | |
| 846 | + | |
| 847 | + | |
838 | 848 | | |
839 | 849 | | |
840 | 850 | | |
| |||
1855 | 1865 | | |
1856 | 1866 | | |
1857 | 1867 | | |
| 1868 | + | |
| 1869 | + | |
| 1870 | + | |
| 1871 | + | |
| 1872 | + | |
| 1873 | + | |
| 1874 | + | |
| 1875 | + | |
| 1876 | + | |
| 1877 | + | |
| 1878 | + | |
| 1879 | + | |
| 1880 | + | |
| 1881 | + | |
| 1882 | + | |
| 1883 | + | |
| 1884 | + | |
| 1885 | + | |
| 1886 | + | |
| 1887 | + | |
| 1888 | + | |
| 1889 | + | |
1858 | 1890 | | |
1859 | 1891 | | |
1860 | 1892 | | |
| |||
2383 | 2415 | | |
2384 | 2416 | | |
2385 | 2417 | | |
2386 | | - | |
| 2418 | + | |
2387 | 2419 | | |
2388 | 2420 | | |
2389 | 2421 | | |
| |||
2396 | 2428 | | |
2397 | 2429 | | |
2398 | 2430 | | |
2399 | | - | |
| 2431 | + | |
| 2432 | + | |
| 2433 | + | |
| 2434 | + | |
2400 | 2435 | | |
2401 | 2436 | | |
2402 | 2437 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
0 commit comments