Skip to content

Commit 39f67ec

Browse files
fix: require minimizer-webpack-plugin 5.12 for inline scripts in HTML
Before 5.10 the plugin's `terserMinify` handed terser the `as` webpack names an embedded body's production with, so every inline `<script>` and event handler failed with "`as` is not a supported option". The new case drives each of those productions through the plugin's terser and fails on 5.8.0. Claude-Session: https://claude.ai/code/session_01Vszf1WJhCqatsMQ2xZLF9x
1 parent 951d0c6 commit 39f67ec

9 files changed

Lines changed: 109 additions & 5 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+
Require `minimizer-webpack-plugin` 5.12, so inline scripts in HTML minify again.

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@
131131
"events": "^3.2.0",
132132
"graceful-fs": "^4.2.11",
133133
"mime-db": "^1.54.0",
134-
"minimizer-webpack-plugin": "^5.7.0",
134+
"minimizer-webpack-plugin": "^5.12.0",
135135
"schema-utils": "^4.5.0",
136136
"tapable": "^2.3.0",
137137
"terser": "^5.51.0",
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`ConfigCacheTestCases html minimize-embedded-script-productions minimize-embedded-script-productions should compile 1`] = `
4+
"<!doctype html><html lang=en><head><title>x</title><script>(()=>{if(localStorage&&window.matchMedia){const e=window.matchMedia(\\"(prefers-color-scheme: dark)\\").matches;document.documentElement.dataset.theme=e?\\"dark\\":\\"light\\"}})();</script><script type=module>window.moduleRan=!0;</script></head><body>
5+
<button onclick=if(a)return!1;b() style=color:red;margin:0>x</button>
6+
7+
8+
</body></html>"
9+
`;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`ConfigTestCases html minimize-embedded-script-productions minimize-embedded-script-productions should compile 1`] = `
4+
"<!doctype html><html lang=en><head><title>x</title><script>(()=>{if(localStorage&&window.matchMedia){const e=window.matchMedia(\\"(prefers-color-scheme: dark)\\").matches;document.documentElement.dataset.theme=e?\\"dark\\":\\"light\\"}})();</script><script type=module>window.moduleRan=!0;</script></head><body>
5+
<button onclick=if(a)return!1;b() style=color:red;margin:0>x</button>
6+
7+
8+
</body></html>"
9+
`;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
it("should emit the document whose scripts the plugin's terser minified", () => {
2+
// The assertions on the emitted file live in test.config.js (afterExecute).
3+
expect(new URL("./page.html", import.meta.url)).toBeDefined();
4+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<title>x</title>
5+
<script>
6+
;(() => {
7+
if (localStorage && window.matchMedia) {
8+
const dark = window.matchMedia("(prefers-color-scheme: dark)").matches ;
9+
document.documentElement.dataset.theme = dark ? "dark" : "light" ;
10+
}
11+
})() ;
12+
</script>
13+
<script type="module">
14+
const unused = 1 ;
15+
window.moduleRan = true ;
16+
</script>
17+
</head>
18+
<body>
19+
<button onclick=" if ( a ) return false ; b ( ) " style=" color : red ; margin : 0px ">x</button>
20+
</body>
21+
</html>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"use strict";
2+
3+
const fs = require("fs");
4+
const path = require("path");
5+
6+
module.exports = {
7+
afterExecute(options) {
8+
const page = fs.readFileSync(
9+
path.join(options.output.path, "page.html"),
10+
"utf8"
11+
);
12+
13+
expect(page).toMatchSnapshot();
14+
// Each production terser was handed: a classic script, a module (whose
15+
// unused binding it may drop) and an event handler's function body.
16+
expect(page).toContain(
17+
'<script>(()=>{if(localStorage&&window.matchMedia){const e=window.matchMedia("(prefers-color-scheme: dark)").matches;document.documentElement.dataset.theme=e?"dark":"light"}})();</script>'
18+
);
19+
expect(page).toContain("<script type=module>window.moduleRan=!0;</script>");
20+
expect(page).toContain("onclick=if(a)return!1;b()");
21+
expect(page).toContain("style=color:red;margin:0");
22+
}
23+
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"use strict";
2+
3+
const MinimizerPlugin = require("minimizer-webpack-plugin");
4+
const cssMinify = require("../../../../lib/css/cssMinify");
5+
const htmlMinify = require("../../../../lib/html/htmlMinify");
6+
7+
/** @type {import("../../../../").Configuration} */
8+
module.exports = {
9+
target: "web",
10+
mode: "production",
11+
output: { pathinfo: false, assetModuleFilename: "[name][ext]" },
12+
module: {
13+
rules: [{ test: /\.html$/, type: "asset/resource" }]
14+
},
15+
optimization: {
16+
minimize: true,
17+
minimizer: [
18+
{
19+
apply: (compiler) => {
20+
// The plugin's own `terserMinify`, as webpack 5.111.1's default minimizer
21+
// used it: each `<script>` and handler reaches it with an `as` naming
22+
// its production, which a plugin before 5.10.0 handed to terser as is.
23+
new MinimizerPlugin({
24+
test: /\.(?:[cm]?js|css|html)(\?.*)?$/i,
25+
minify: [MinimizerPlugin.terserMinify, cssMinify, htmlMinify],
26+
minimizerOptions: [{ compress: { passes: 2 } }, {}, {}]
27+
}).apply(/** @type {EXPECTED_ANY} */ (compiler));
28+
}
29+
}
30+
]
31+
},
32+
experiments: { css: true, html: true }
33+
};

‎yarn.lock‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7607,10 +7607,10 @@ minimist@^1.2.0, minimist@^1.2.5, minimist@^1.2.6, minimist@^1.2.8:
76077607
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.8.tgz#c1a464e7693302e082a075cee0c057741ac4772c"
76087608
integrity sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==
76097609

7610-
minimizer-webpack-plugin@^5.7.0:
7611-
version "5.11.0"
7612-
resolved "https://registry.yarnpkg.com/minimizer-webpack-plugin/-/minimizer-webpack-plugin-5.11.0.tgz#ffc2934e639c4bdc88dd2391b39342525c59e1bc"
7613-
integrity sha512-2JVy2DY+iBtfRyrQB5cRM7lfurnJIi6jFTgLip/7FxDu0WxswrIRzr4fBySQ0X7HqM7BcwE28zm+HfQ5V6y4Cg==
7610+
minimizer-webpack-plugin@^5.12.0:
7611+
version "5.12.0"
7612+
resolved "https://registry.yarnpkg.com/minimizer-webpack-plugin/-/minimizer-webpack-plugin-5.12.0.tgz#acef4a0bd3c5ee405f767a9d90e0173b55b16458"
7613+
integrity sha512-PCN1x1OzFeFB2GlP91s46q7Gz8Jf16Z7M45bzpoqrYZlh12QeY6tYVM5h299wbwy1DxnQ69VFmmh8Hb2Ff8mbw==
76147614
dependencies:
76157615
"@jridgewell/trace-mapping" "^0.3.31"
76167616
jest-worker "^27.4.5"

0 commit comments

Comments
 (0)