Skip to content

Commit 951d0c6

Browse files
Support CSS asset names with fragment/query suffixes in minifier (#22277)
* fix(css): minify assets with fragment suffixes * fix: minify JS, CSS and HTML assets whose names carry a fragment webpack itself emits such names: asset modules default to `[hash][ext][query][fragment]`, and `output.filename` may contain `#`. Widen the `jsMinify`/`htmlMinify` filters and the default minimizer's `test` (both branches) to accept a `#` suffix, and cover it with a config case that emits these names through webpack rather than by writing to `compilation.assets`. Claude-Session: https://claude.ai/code/session_01EpeANYvMKyZwSDTRVE5LoG * fix: read a minified asset's extension before its query or fragment `/\.css(?:[?#].*)?$/` also claimed `data.txt#a.css`, and the CSS minifier then emptied it. Anchor the three filters and the default `test` so the extension is the one before the first `?` or `#`. Claude-Session: https://claude.ai/code/session_01EpeANYvMKyZwSDTRVE5LoG * test: snapshot the minified fragment-named assets Claude-Session: https://claude.ai/code/session_01EpeANYvMKyZwSDTRVE5LoG * test: compare the unminified asset to its source, not an LF literal Claude-Session: https://claude.ai/code/session_01EpeANYvMKyZwSDTRVE5LoG
1 parent a924765 commit 951d0c6

14 files changed

Lines changed: 148 additions & 4 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+
Minify JavaScript, CSS and HTML assets whose names carry a `#fragment`.

‎lib/config/defaults.js‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2558,8 +2558,17 @@ const applyOptimizationDefaults = (
25582558
!claimedByOtherMinimizer.html &&
25592559
minimizeByType.html !== false;
25602560
if (!minifyJs && !minifyCss && !minifyHtml) return;
2561+
// An asset name keeps the query and the fragment its filename template
2562+
// wrote (`[query][fragment]`, `main.js#[contenthash]`), so both may follow.
2563+
/**
2564+
* @param {string[]} extensions extension patterns
2565+
* @returns {RegExp} matcher for asset names with those extensions
2566+
*/
2567+
const assetNameTest = (extensions) =>
2568+
new RegExp(`^[^?#]*\\.(?:${extensions.join("|")})(?:[?#].*)?$`, "i");
25612569
if (minifyJs && !minifyCss && !minifyHtml) {
25622570
new TerserPlugin({
2571+
test: assetNameTest(["[cm]?js"]),
25632572
minify: require("../javascript/jsMinify"),
25642573
minimizerOptions: terserOptions
25652574
}).apply(/** @type {EXPECTED_ANY} */ (compiler));
@@ -2622,7 +2631,7 @@ const applyOptimizationDefaults = (
26222631
extensions.push("html");
26232632
}
26242633
new TerserPlugin({
2625-
test: new RegExp(`\\.(?:${extensions.join("|")})(\\?.*)?$`, "i"),
2634+
test: assetNameTest(extensions),
26262635
minify,
26272636
minimizerOptions
26282637
}).apply(/** @type {EXPECTED_ANY} */ (compiler));

‎lib/css/cssMinify.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,6 @@ cssMinify.getEmbeddedTypes = () => [
173173
* @param {string} name asset filename
174174
* @returns {boolean} true for CSS assets
175175
*/
176-
cssMinify.filter = (name) => /\.css(\?.*)?$/i.test(name);
176+
cssMinify.filter = (name) => /^[^?#]*\.css(?:[?#].*)?$/i.test(name);
177177

178178
module.exports = cssMinify;

‎lib/html/htmlMinify.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,6 @@ htmlMinify.getEmbeddedTypes = () => {
252252
* @param {string} name asset filename
253253
* @returns {boolean} true for HTML assets
254254
*/
255-
htmlMinify.filter = (name) => /\.html(\?.*)?$/i.test(name);
255+
htmlMinify.filter = (name) => /^[^?#]*\.html(?:[?#].*)?$/i.test(name);
256256

257257
module.exports = htmlMinify;

‎lib/javascript/jsMinify.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,6 @@ jsMinify.getTypes = () => ["javascript"];
267267
* @param {string} name asset filename
268268
* @returns {boolean} true for JavaScript assets
269269
*/
270-
jsMinify.filter = (name) => /\.[cm]?js(\?.*)?$/i.test(name);
270+
jsMinify.filter = (name) => /^[^?#]*\.[cm]?js(?:[?#].*)?$/i.test(name);
271271

272272
module.exports = jsMinify;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`ConfigCacheTestCases asset-modules minimize-fragment-names minimize-fragment-names should compile 1`] = `"export const answer=function(){return 3};"`;
4+
5+
exports[`ConfigCacheTestCases asset-modules minimize-fragment-names minimize-fragment-names should compile 2`] = `"export const answer=function(){return 3};"`;
6+
7+
exports[`ConfigCacheTestCases asset-modules minimize-fragment-names minimize-fragment-names should compile 3`] = `".fragment{color:red}"`;
8+
9+
exports[`ConfigCacheTestCases asset-modules minimize-fragment-names minimize-fragment-names should compile 4`] = `
10+
"<!doctype html><head><title>Fragment</title></head><body><p class=a>hi</p>
11+
12+
</body></html>"
13+
`;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`ConfigTestCases asset-modules minimize-fragment-names minimize-fragment-names should compile 1`] = `"export const answer=function(){return 3};"`;
4+
5+
exports[`ConfigTestCases asset-modules minimize-fragment-names minimize-fragment-names should compile 2`] = `"export const answer=function(){return 3};"`;
6+
7+
exports[`ConfigTestCases asset-modules minimize-fragment-names minimize-fragment-names should compile 3`] = `".fragment{color:red}"`;
8+
9+
exports[`ConfigTestCases asset-modules minimize-fragment-names minimize-fragment-names should compile 4`] = `
10+
"<!doctype html><head><title>Fragment</title></head><body><p class=a>hi</p>
11+
12+
</body></html>"
13+
`;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
not css {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head><title>Fragment</title></head>
4+
<body><p class=" a ">hi</p></body>
5+
</html>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const answer = function () {
2+
return 1 + 2;
3+
};

0 commit comments

Comments
 (0)