Skip to content

Commit c1be4e2

Browse files
authored
feat(config): use module paths for default minimizers (#22309)
* feat(config): use module paths for default minimizers * test(config): update minimizer module reference assertions --------- Co-authored-by: xiaoxiaojx <[email protected]>
1 parent 1d8600a commit c1be4e2

5 files changed

Lines changed: 135 additions & 10 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Use module paths for webpack's built-in minimizers so parallel minification can load them directly in workers instead of serializing their function sources.

‎lib/config/defaults.js‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2555,8 +2555,11 @@ const applyOptimizationDefaults = (
25552555
// proven on real builds it is a future default, and terser as the plugin
25562556
// publishes it minifies JS everywhere else.
25572557
const javascriptMinify = futureDefaults
2558-
? require("../javascript/jsMinify")
2559-
: MinimizerPlugin.terserMinify;
2558+
? require.resolve("../javascript/jsMinify")
2559+
: {
2560+
path: require.resolve("minimizer-webpack-plugin"),
2561+
export: "terserMinify"
2562+
};
25602563

25612564
const { css, html } = compiler.options.experiments;
25622565
const minifyJs = minimizeByType.javascript !== false;
@@ -2588,7 +2591,7 @@ const applyOptimizationDefaults = (
25882591
}).apply(/** @type {EXPECTED_ANY} */ (compiler));
25892592
return;
25902593
}
2591-
/** @type {(typeof javascriptMinify | typeof import("../css/cssMinify") | typeof import("../html/htmlMinify") | typeof import("../json/jsonMinify"))[]} */
2594+
/** @type {(typeof javascriptMinify | string)[]} */
25922595
const minify = [];
25932596
/** @type {EXPECTED_OBJECT[]} */
25942597
const minimizerOptions = [];
@@ -2625,15 +2628,15 @@ const applyOptimizationDefaults = (
26252628
extensions.push("[cm]?js");
26262629
}
26272630
if (minifyCss) {
2628-
minify.push(require("../css/cssMinify"));
2631+
minify.push(require.resolve("../css/cssMinify"));
26292632
minimizerOptions.push({
26302633
environment: cssEnvironment,
26312634
...cssOptions
26322635
});
26332636
extensions.push("css");
26342637
}
26352638
if (minifyHtml) {
2636-
minify.push(require("../html/htmlMinify"));
2639+
minify.push(require.resolve("../html/htmlMinify"));
26372640
// `minimize.css` rides under its own key rather than spread beside the
26382641
// HTML options: the two name some of the same switches, so one flat
26392642
// object would hand each language the other's answer.
@@ -2645,7 +2648,7 @@ const applyOptimizationDefaults = (
26452648
extensions.push("html");
26462649
}
26472650
if (minifyJson) {
2648-
minify.push(require("../json/jsonMinify"));
2651+
minify.push(require.resolve("../json/jsonMinify"));
26492652
minimizerOptions.push({});
26502653
// A web app manifest is JSON under its own extension.
26512654
extensions.push("json", "webmanifest");
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { createRequire } from "module";
2+
3+
const require = createRequire(import.meta.url);
4+
5+
const { minify, transform } = require("minimizer-webpack-plugin/dist/minify");
6+
const serialize = require("minimizer-webpack-plugin/dist/serialize-javascript");
7+
const { terserMinify } = require("minimizer-webpack-plugin/dist/utils");
8+
9+
const implementationPath = {
10+
path: require.resolve("minimizer-webpack-plugin/dist/utils"),
11+
export: "terserMinify"
12+
};
13+
const input = "function add(a, b) { return a + b; } console.log(add(1, 2));";
14+
const options = {
15+
name: "bundle.js",
16+
input,
17+
minimizer: {
18+
implementation: terserMinify,
19+
options: { compress: true, mangle: true }
20+
},
21+
extractComments: false
22+
};
23+
const pathOptions = {
24+
...options,
25+
minimizer: {
26+
...options.minimizer,
27+
implementation: implementationPath
28+
}
29+
};
30+
31+
/** @param {import("tinybench").Bench} bench benchmark suite */
32+
export default (bench) => {
33+
bench.add(
34+
'unit benchmark "minimizer-module-path-unit", minify entry by module path',
35+
async () => {
36+
await minify(pathOptions);
37+
}
38+
);
39+
bench.add(
40+
'unit benchmark "minimizer-module-path-unit", transform entry by function source',
41+
async () => {
42+
await transform(serialize(options));
43+
}
44+
);
45+
};

‎test/unitCases/Defaults.unittest.js‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6342,7 +6342,6 @@ describe("optimization.minimize", () => {
63426342

63436343
it("should minify JavaScript through webpack's printer under futureDefaults only", () => {
63446344
const MinimizerPlugin = require("minimizer-webpack-plugin");
6345-
const jsMinify = require("../../lib/javascript/jsMinify");
63466345
const { applyWebpackOptionsDefaults, getNormalizedWebpackOptions } =
63476346
require("../..").config;
63486347

@@ -6382,9 +6381,13 @@ describe("optimization.minimize", () => {
63826381
};
63836382

63846383
// terser as the plugin publishes it, until the printer is proven on real
6385-
// builds; the printer writes what it writes, so neither needs an option.
6386-
expect(wiredMinify(false)).toBe(MinimizerPlugin.terserMinify);
6387-
expect(wiredMinify(true)).toBe(jsMinify);
6384+
// builds; both implementations are passed as module references so workers
6385+
// can load them without serializing their function source.
6386+
expect(wiredMinify(false)).toEqual({
6387+
path: require.resolve("minimizer-webpack-plugin"),
6388+
export: "terserMinify"
6389+
});
6390+
expect(wiredMinify(true)).toBe(require.resolve("../../lib/javascript/jsMinify"));
63886391

63896392
/**
63906393
* @param {boolean} futureDefaults whether `experiments.futureDefaults` is on
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
"use strict";
2+
3+
const path = require("path");
4+
const MinimizerPlugin = require("minimizer-webpack-plugin");
5+
const { canMinifyByPath } = /** @type {EXPECTED_ANY} */ (
6+
// @ts-expect-error -- the plugin does not publish declarations for this test hook
7+
require("minimizer-webpack-plugin/dist/implementation")
8+
);
9+
const webpack = require("../..");
10+
11+
describe("default minimizer module paths", () => {
12+
/**
13+
* @param {import("../..").Configuration} config configuration
14+
* @returns {EXPECTED_ANY} raw minimizer options
15+
*/
16+
const getRawOptions = (config) => {
17+
const originalApply = MinimizerPlugin.prototype.apply;
18+
/** @type {EXPECTED_ANY} */
19+
let rawOptions;
20+
MinimizerPlugin.prototype.apply = function () {
21+
rawOptions = /** @type {EXPECTED_ANY} */ (this).rawOptions;
22+
};
23+
try {
24+
webpack({ ...config, mode: "production" });
25+
} finally {
26+
MinimizerPlugin.prototype.apply = originalApply;
27+
}
28+
return rawOptions;
29+
};
30+
31+
const expectModulePathDispatch = (/** @type {EXPECTED_ANY} */ rawOptions) => {
32+
expect(rawOptions).toBeDefined();
33+
const implementations = Array.isArray(rawOptions.minify)
34+
? rawOptions.minify
35+
: [rawOptions.minify];
36+
expect(
37+
implementations.every(
38+
(/** @type {EXPECTED_ANY} */ implementation) =>
39+
typeof implementation === "string" ||
40+
(implementation && typeof implementation.path === "string")
41+
)
42+
).toBe(true);
43+
// This is the plugin's exact guard for worker.minify. A function,
44+
// function-valued option, or mixed implementation would make it false.
45+
expect(canMinifyByPath({
46+
minimizer: {
47+
implementation: rawOptions.minify,
48+
options: rawOptions.minimizerOptions || {}
49+
}
50+
})).toBe(true);
51+
};
52+
53+
it("keeps the JavaScript task on the module-path dispatch", () => {
54+
expectModulePathDispatch(
55+
getRawOptions({
56+
entry: path.resolve(__dirname, "../../package.json")
57+
})
58+
);
59+
});
60+
61+
it("keeps the multi-language task on the module-path dispatch", () => {
62+
expectModulePathDispatch(
63+
getRawOptions({
64+
entry: path.resolve(__dirname, "../../package.json"),
65+
experiments: { css: true, html: true }
66+
})
67+
);
68+
});
69+
});

0 commit comments

Comments
 (0)