Skip to content

Commit 6ac75d4

Browse files
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
1 parent 0af51c4 commit 6ac75d4

20 files changed

Lines changed: 333 additions & 162 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": minor
3+
---
4+
5+
Annotate instantiations of side-effect-free modules with `/*#__PURE__*/`.

‎lib/optimize/ConcatenatedModule.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2924,7 +2924,10 @@ ${defineGetters}`
29242924
// External module bindings may be wrapped in
29252925
// runtime-condition `if` blocks but referenced outside,
29262926
// so they must remain function-scoped (`var`).
2927-
const required = `__webpack_require__(${moduleId})`;
2927+
const required = `${runtimeTemplate.moduleSideEffectsAnnotation(
2928+
info.module,
2929+
moduleGraph
2930+
)}__webpack_require__(${moduleId})`;
29282931

29292932
// The wrapping happens per reference, so only the helper has
29302933
// to be declared here.

‎lib/template/RuntimeTemplate.js‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1850,6 +1850,19 @@ class RuntimeTemplate {
18501850
return `${this.comment({ request })}${JSON.stringify(moduleId)}`;
18511851
}
18521852

1853+
/**
1854+
* The annotation for a module instantiation, carrying what webpack knows about
1855+
* the module's side effects on to whoever consumes the emitted bundle.
1856+
* @param {Module} module the instantiated module
1857+
* @param {ModuleGraph} moduleGraph the module graph
1858+
* @returns {string} the annotation, or an empty string when it may have side effects
1859+
*/
1860+
moduleSideEffectsAnnotation(module, moduleGraph) {
1861+
return module.getSideEffectsConnectionState(moduleGraph) === false
1862+
? "/*#__PURE__*/"
1863+
: "";
1864+
}
1865+
18531866
/**
18541867
* Returns the expression.
18551868
* @param {object} options options object
@@ -2391,7 +2404,10 @@ class RuntimeTemplate {
23912404

23922405
return [importContent, ""];
23932406
}
2394-
importContent = `/* harmony import */ ${optDeclaration}${importVar} = ${RuntimeGlobals.require}(${moduleId});\n`;
2407+
importContent = `/* harmony import */ ${optDeclaration}${importVar} = ${this.moduleSideEffectsAnnotation(
2408+
module,
2409+
moduleGraph
2410+
)}${RuntimeGlobals.require}(${moduleId});\n`;
23952411

23962412
if (exportsType === "dynamic") {
23972413
runtimeRequirements.add(RuntimeGlobals.compatGetDefaultExport);

‎test/configCases/optimization/static-export-bindings/__snapshots__/ConfigCacheTest.snap‎

Lines changed: 128 additions & 128 deletions
Large diffs are not rendered by default.

‎test/configCases/optimization/static-export-bindings/__snapshots__/ConfigTest.snap‎

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ __webpack_require__.r(__webpack_exports__);
6565
/* harmony export */ cyclicConst: () => (/* binding */ cyclicConst),
6666
/* harmony export */ readViaB: () => (/* binding */ readViaB)
6767
/* harmony export */ });
68-
/* harmony import */ var _cycle_b__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./cycle-b */ 100);
68+
/* harmony import */ var _cycle_b__WEBPACK_IMPORTED_MODULE_0__ = /*#__PURE__*/__webpack_require__(/*! ./cycle-b */ 100);
6969
7070
7171
const cyclicConst = \\"cyclic\\";
@@ -87,7 +87,7 @@ __webpack_require__.r(__webpack_exports__);
8787
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
8888
/* harmony export */ readFromB: () => (/* binding */ readFromB)
8989
/* harmony export */ });
90-
/* harmony import */ var _cycle_a__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./cycle-a */ 797);
90+
/* harmony import */ var _cycle_a__WEBPACK_IMPORTED_MODULE_0__ = /*#__PURE__*/__webpack_require__(/*! ./cycle-a */ 797);
9191
9292
9393
function readFromB() {
@@ -246,8 +246,8 @@ __webpack_require__.r(__webpack_exports__);
246246
/* harmony export */ chainedIncrement: () => (/* reexport safe */ _reexport__WEBPACK_IMPORTED_MODULE_1__.increment),
247247
/* harmony export */ chainedLiteral: () => (/* reexport safe */ _reexport__WEBPACK_IMPORTED_MODULE_0__.literal)
248248
/* harmony export */ });
249-
/* harmony import */ var _reexport__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./reexport */ 660);
250-
/* harmony import */ var _reexport__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./reexport */ 245);
249+
/* harmony import */ var _reexport__WEBPACK_IMPORTED_MODULE_0__ = /*#__PURE__*/__webpack_require__(/*! ./reexport */ 660);
250+
/* harmony import */ var _reexport__WEBPACK_IMPORTED_MODULE_1__ = /*#__PURE__*/__webpack_require__(/*! ./reexport */ 245);
251251
// Re-export chain: this file re-exports from reexport.js which re-exports from const-exports.js
252252
253253
// A mutable binding re-exported through two hops must stay live
@@ -266,7 +266,7 @@ __webpack_require__.r(__webpack_exports__);
266266
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
267267
/* harmony export */ reCircular: () => (/* reexport safe */ _cycle_a__WEBPACK_IMPORTED_MODULE_0__.cyclicConst)
268268
/* harmony export */ });
269-
/* harmony import */ var _cycle_a__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./cycle-a */ 797);
269+
/* harmony import */ var _cycle_a__WEBPACK_IMPORTED_MODULE_0__ = /*#__PURE__*/__webpack_require__(/*! ./cycle-a */ 797);
270270
// Re-export from a circular module — should use getter
271271
272272
@@ -286,9 +286,9 @@ __webpack_require__.r(__webpack_exports__);
286286
/* harmony export */ reFn: () => (/* reexport safe */ _function_exports__WEBPACK_IMPORTED_MODULE_1__.fn),
287287
/* harmony export */ reIncrement: () => (/* reexport safe */ _mutable_exports__WEBPACK_IMPORTED_MODULE_2__.increment)
288288
/* harmony export */ });
289-
/* harmony import */ var _const_exports__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./const-exports */ 660);
290-
/* harmony import */ var _function_exports__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./function-exports */ 103);
291-
/* harmony import */ var _mutable_exports__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./mutable-exports */ 245);
289+
/* harmony import */ var _const_exports__WEBPACK_IMPORTED_MODULE_0__ = /*#__PURE__*/__webpack_require__(/*! ./const-exports */ 660);
290+
/* harmony import */ var _function_exports__WEBPACK_IMPORTED_MODULE_1__ = /*#__PURE__*/__webpack_require__(/*! ./function-exports */ 103);
291+
/* harmony import */ var _mutable_exports__WEBPACK_IMPORTED_MODULE_2__ = /*#__PURE__*/__webpack_require__(/*! ./mutable-exports */ 245);
292292
// Mixed: direct const export + re-exports in the same module
293293
const ownConst = \\"own\\";
294294
@@ -314,8 +314,8 @@ __webpack_require__.r(__webpack_exports__);
314314
/* harmony export */ fn2: () => (/* reexport safe */ _function_exports__WEBPACK_IMPORTED_MODULE_1__.fn2),
315315
/* harmony export */ namedConst: () => (/* reexport safe */ _const_exports__WEBPACK_IMPORTED_MODULE_0__.literal)
316316
/* harmony export */ });
317-
/* harmony import */ var _const_exports__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./const-exports */ 660);
318-
/* harmony import */ var _function_exports__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./function-exports */ 103);
317+
/* harmony import */ var _const_exports__WEBPACK_IMPORTED_MODULE_0__ = /*#__PURE__*/__webpack_require__(/*! ./const-exports */ 660);
318+
/* harmony import */ var _function_exports__WEBPACK_IMPORTED_MODULE_1__ = /*#__PURE__*/__webpack_require__(/*! ./function-exports */ 103);
319319
// Star export mixed with named re-export — star export should prevent value binding
320320
321321
@@ -339,9 +339,9 @@ __webpack_require__.r(__webpack_exports__);
339339
/* harmony export */ literal: () => (/* reexport safe */ _const_exports__WEBPACK_IMPORTED_MODULE_0__.literal),
340340
/* harmony export */ renamed: () => (/* reexport safe */ _const_exports__WEBPACK_IMPORTED_MODULE_0__.renamed)
341341
/* harmony export */ });
342-
/* harmony import */ var _const_exports__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./const-exports */ 660);
343-
/* harmony import */ var _function_exports__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./function-exports */ 103);
344-
/* harmony import */ var _mutable_exports__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./mutable-exports */ 245);
342+
/* harmony import */ var _const_exports__WEBPACK_IMPORTED_MODULE_0__ = /*#__PURE__*/__webpack_require__(/*! ./const-exports */ 660);
343+
/* harmony import */ var _function_exports__WEBPACK_IMPORTED_MODULE_1__ = /*#__PURE__*/__webpack_require__(/*! ./function-exports */ 103);
344+
/* harmony import */ var _mutable_exports__WEBPACK_IMPORTED_MODULE_2__ = /*#__PURE__*/__webpack_require__(/*! ./mutable-exports */ 245);
345345
346346
347347
@@ -360,7 +360,7 @@ __webpack_require__.r(__webpack_exports__);
360360
/* harmony export */ readSelf: () => (/* binding */ readSelf),
361361
/* harmony export */ selfConst: () => (/* binding */ selfConst)
362362
/* harmony export */ });
363-
/* harmony import */ var _self_const__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./self-const */ 973);
363+
/* harmony import */ var _self_const__WEBPACK_IMPORTED_MODULE_0__ = /*#__PURE__*/__webpack_require__(/*! ./self-const */ 973);
364364
// Self-cycle: const must fall back to a getter so reading via the namespace works
365365
366366
@@ -384,7 +384,7 @@ __webpack_require__.r(__webpack_exports__);
384384
/* harmony export */ counter: () => (/* reexport safe */ _mutable_exports__WEBPACK_IMPORTED_MODULE_0__.counter),
385385
/* harmony export */ increment: () => (/* reexport safe */ _mutable_exports__WEBPACK_IMPORTED_MODULE_0__.increment)
386386
/* harmony export */ });
387-
/* harmony import */ var _mutable_exports__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./mutable-exports */ 245);
387+
/* harmony import */ var _mutable_exports__WEBPACK_IMPORTED_MODULE_0__ = /*#__PURE__*/__webpack_require__(/*! ./mutable-exports */ 245);
388388
// \`export *\` of a mutable binding must stay live
389389
390390
@@ -469,23 +469,23 @@ let __webpack_exports__ = {};
469469
!*** ./index.js ***!
470470
\\\\******************/
471471
__webpack_require__.r(__webpack_exports__);
472-
/* harmony import */ var _const_exports__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./reexport-star */ 660);
473-
/* harmony import */ var _function_exports__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./reexport-star */ 103);
474-
/* harmony import */ var _mutable_exports__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./star-mutable */ 245);
475-
/* harmony import */ var _cycle_a__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./reexport-circular */ 797);
476-
/* harmony import */ var _reexport__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ./reexport */ 618);
477-
/* harmony import */ var _reexport_chain__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! ./reexport-chain */ 670);
478-
/* harmony import */ var _reexport_mixed__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(/*! ./reexport-mixed */ 992);
479-
/* harmony import */ var _reexport_star__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__(/*! ./reexport-star */ 787);
480-
/* harmony import */ var _reexport_circular__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__(/*! ./reexport-circular */ 770);
481-
/* harmony import */ var _default_binding__WEBPACK_IMPORTED_MODULE_9__ = __webpack_require__(/*! ./default-binding */ 260);
482-
/* harmony import */ var _const_default__WEBPACK_IMPORTED_MODULE_10__ = __webpack_require__(/*! ./const-default */ 462);
483-
/* harmony import */ var _default_expr__WEBPACK_IMPORTED_MODULE_11__ = __webpack_require__(/*! ./default-expr */ 262);
484-
/* harmony import */ var _default_fn_decl__WEBPACK_IMPORTED_MODULE_12__ = __webpack_require__(/*! ./default-fn-decl */ 914);
485-
/* harmony import */ var _default_fn_anon__WEBPACK_IMPORTED_MODULE_13__ = __webpack_require__(/*! ./default-fn-anon */ 72);
486-
/* harmony import */ var _star_mutable__WEBPACK_IMPORTED_MODULE_14__ = __webpack_require__(/*! ./star-mutable */ 990);
487-
/* harmony import */ var _self_const__WEBPACK_IMPORTED_MODULE_15__ = __webpack_require__(/*! ./self-const */ 973);
488-
/* harmony import */ var _mutable_forms__WEBPACK_IMPORTED_MODULE_16__ = __webpack_require__(/*! ./mutable-forms */ 381);
472+
/* harmony import */ var _const_exports__WEBPACK_IMPORTED_MODULE_0__ = /*#__PURE__*/__webpack_require__(/*! ./reexport-star */ 660);
473+
/* harmony import */ var _function_exports__WEBPACK_IMPORTED_MODULE_1__ = /*#__PURE__*/__webpack_require__(/*! ./reexport-star */ 103);
474+
/* harmony import */ var _mutable_exports__WEBPACK_IMPORTED_MODULE_2__ = /*#__PURE__*/__webpack_require__(/*! ./star-mutable */ 245);
475+
/* harmony import */ var _cycle_a__WEBPACK_IMPORTED_MODULE_3__ = /*#__PURE__*/__webpack_require__(/*! ./reexport-circular */ 797);
476+
/* harmony import */ var _reexport__WEBPACK_IMPORTED_MODULE_4__ = /*#__PURE__*/__webpack_require__(/*! ./reexport */ 618);
477+
/* harmony import */ var _reexport_chain__WEBPACK_IMPORTED_MODULE_5__ = /*#__PURE__*/__webpack_require__(/*! ./reexport-chain */ 670);
478+
/* harmony import */ var _reexport_mixed__WEBPACK_IMPORTED_MODULE_6__ = /*#__PURE__*/__webpack_require__(/*! ./reexport-mixed */ 992);
479+
/* harmony import */ var _reexport_star__WEBPACK_IMPORTED_MODULE_7__ = /*#__PURE__*/__webpack_require__(/*! ./reexport-star */ 787);
480+
/* harmony import */ var _reexport_circular__WEBPACK_IMPORTED_MODULE_8__ = /*#__PURE__*/__webpack_require__(/*! ./reexport-circular */ 770);
481+
/* harmony import */ var _default_binding__WEBPACK_IMPORTED_MODULE_9__ = /*#__PURE__*/__webpack_require__(/*! ./default-binding */ 260);
482+
/* harmony import */ var _const_default__WEBPACK_IMPORTED_MODULE_10__ = /*#__PURE__*/__webpack_require__(/*! ./const-default */ 462);
483+
/* harmony import */ var _default_expr__WEBPACK_IMPORTED_MODULE_11__ = /*#__PURE__*/__webpack_require__(/*! ./default-expr */ 262);
484+
/* harmony import */ var _default_fn_decl__WEBPACK_IMPORTED_MODULE_12__ = /*#__PURE__*/__webpack_require__(/*! ./default-fn-decl */ 914);
485+
/* harmony import */ var _default_fn_anon__WEBPACK_IMPORTED_MODULE_13__ = /*#__PURE__*/__webpack_require__(/*! ./default-fn-anon */ 72);
486+
/* harmony import */ var _star_mutable__WEBPACK_IMPORTED_MODULE_14__ = /*#__PURE__*/__webpack_require__(/*! ./star-mutable */ 990);
487+
/* harmony import */ var _self_const__WEBPACK_IMPORTED_MODULE_15__ = /*#__PURE__*/__webpack_require__(/*! ./self-const */ 973);
488+
/* harmony import */ var _mutable_forms__WEBPACK_IMPORTED_MODULE_16__ = /*#__PURE__*/__webpack_require__(/*! ./mutable-forms */ 381);
489489
490490
491491
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { mul } from "./pure-cjs";
2+
3+
export function plus(a, b) {
4+
return a + b;
5+
}
6+
7+
export function times(a, b) {
8+
return mul(a, b);
9+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Not analyzable as side-effect free, so the case's `sideEffects: false` rule is
2+
// what tells webpack it is — the information the annotation carries into the output.
3+
exports.mul = function (a, b) {
4+
// Makes the module identifiable in the minified consumer bundle.
5+
if (a === "PURE_CJS_MODULE_MARKER") return a;
6+
return a * b;
7+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"use strict";
2+
3+
module.exports.noTests = true;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../types").Configuration} */
4+
module.exports = {
5+
target: "node",
6+
mode: "production",
7+
output: {
8+
filename: "lib.mjs",
9+
library: { type: "module" },
10+
module: true
11+
},
12+
module: {
13+
rules: [{ test: /[\\/]pure-cjs\.js$/, sideEffects: false }]
14+
},
15+
experiments: {
16+
outputModule: true
17+
}
18+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { plus } from "library";
2+
3+
it("should keep the export it uses", () => {
4+
expect(plus(1, 2)).toBe(3);
5+
});
6+
7+
it("should drop the side-effect-free module behind the unused export", () => {
8+
const fs = require("fs");
9+
const source = fs.readFileSync(__filename, "utf-8");
10+
// Spelled out in pieces, so this assertion is not itself a hit.
11+
const marker = ["PURE", "CJS", "MODULE", "MARKER"].join("_");
12+
13+
expect(source).not.toContain(marker);
14+
});

0 commit comments

Comments
 (0)