Skip to content

Commit c1a6810

Browse files
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
1 parent ae09563 commit c1a6810

4 files changed

Lines changed: 60 additions & 2 deletions

File tree

‎lib/config/defaults.js‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,9 @@ const applyWebpackOptionsDefaults = (options, compilerIndex) => {
578578
css:
579579
/** @type {boolean} */
580580
(options.experiments.css),
581+
library:
582+
/** @type {string[]} */
583+
(options.output.enabledLibraryTypes).length > 0,
581584
records: Boolean(options.recordsInputPath || options.recordsOutputPath)
582585
});
583586

@@ -2444,12 +2447,13 @@ const applyPerformanceDefaults = (performance, { production }) => {
24442447
* @param {boolean} options.production is production
24452448
* @param {boolean} options.development is development
24462449
* @param {boolean} options.css is css enabled
2450+
* @param {boolean} options.library whether the output is consumed as a library
24472451
* @param {boolean} options.records using records
24482452
* @returns {void}
24492453
*/
24502454
const applyOptimizationDefaults = (
24512455
optimization,
2452-
{ production, development, css, records }
2456+
{ production, development, css, library, records }
24532457
) => {
24542458
D(optimization, "removeAvailableModules", false);
24552459
D(optimization, "removeEmptyChunks", true);
@@ -2498,7 +2502,17 @@ const applyOptimizationDefaults = (
24982502
if (optimization.minimize) {
24992503
// Canonicalize the per-asset-type options: the JS minimizer's defaults
25002504
// become visible in the resolved config rather than hidden in the wiring.
2501-
F(minimizeOptions, "javascript", () => ({ compress: { passes: 2 } }));
2505+
// A library is read by another build, so the annotations carrying what the
2506+
// modules do to it have to survive minification; terser drops them otherwise.
2507+
F(minimizeOptions, "javascript", () =>
2508+
library
2509+
? {
2510+
compress: { passes: 2 },
2511+
// eslint-disable-next-line camelcase
2512+
format: { preserve_annotations: true }
2513+
}
2514+
: { compress: { passes: 2 } }
2515+
);
25022516
F(minimizeOptions, "css", () => ({}));
25032517
F(minimizeOptions, "html", () => ({}));
25042518
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { mul } from "./pure-cjs";
2+
3+
export function times(a, b) {
4+
return mul(a, b);
5+
}
6+
7+
it("should keep the annotation through minification", () => {
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 annotation = ["/*#", "__PURE__", "*/"].join("");
12+
13+
// Terser strips annotations unless the library defaults ask it not to, so the
14+
// one in front of the side-effect-free module's instantiation must still be here.
15+
expect(source).toMatch(
16+
new RegExp(`${annotation.replace(/[*/]/g, "\\$&")}\\w+\\(`)
17+
);
18+
expect(times(6, 7)).toBe(42);
19+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
exports.mul = function (a, b) {
2+
return a * b;
3+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../types").Configuration} */
4+
module.exports = {
5+
target: "node",
6+
mode: "production",
7+
output: {
8+
filename: "bundle0.mjs",
9+
library: { type: "module" },
10+
module: true
11+
},
12+
module: {
13+
rules: [{ test: /[\\/]pure-cjs\.js$/, sideEffects: false }]
14+
},
15+
optimization: {
16+
minimize: true,
17+
// `"..."` keeps webpack's own default minimizer, so the case runs the
18+
// terser options `defaults.js` picks for a library.
19+
minimizer: ["..."]
20+
},
21+
experiments: { outputModule: true }
22+
};

0 commit comments

Comments
 (0)