Skip to content

Commit 8a42a2a

Browse files
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
1 parent 659b93b commit 8a42a2a

7 files changed

Lines changed: 15 additions & 7 deletions

File tree

‎lib/config/defaults.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2565,7 +2565,7 @@ const applyOptimizationDefaults = (
25652565
* @returns {RegExp} matcher for asset names with those extensions
25662566
*/
25672567
const assetNameTest = (extensions) =>
2568-
new RegExp(`\\.(?:${extensions.join("|")})(?:[?#].*)?$`, "i");
2568+
new RegExp(`^[^?#]*\\.(?:${extensions.join("|")})(?:[?#].*)?$`, "i");
25692569
if (minifyJs && !minifyCss && !minifyHtml) {
25702570
new TerserPlugin({
25712571
test: assetNameTest(["[cm]?js"]),

‎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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
not css {
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
const urls = [new URL("./files/script.js#frag", import.meta.url)];
1+
const urls = [
2+
new URL("./files/script.js#frag", import.meta.url),
3+
new URL("./files/data.txt#a.css", import.meta.url)
4+
];
25
if (CSS_AND_HTML) {
36
urls.push(
47
new URL("./files/style.css#dark", import.meta.url),
@@ -8,8 +11,9 @@ if (CSS_AND_HTML) {
811

912
it("should keep the fragment in the emitted asset URLs", () => {
1013
expect(urls[0].href).toMatch(/script\.js#frag$/);
14+
expect(urls[1].href).toMatch(/data\.txt#a\.css$/);
1115
if (CSS_AND_HTML) {
12-
expect(urls[1].href).toMatch(/style\.css#dark$/);
13-
expect(urls[2].href).toMatch(/page\.html#top$/);
16+
expect(urls[2].href).toMatch(/style\.css#dark$/);
17+
expect(urls[3].href).toMatch(/page\.html#top$/);
1418
}
1519
});

‎test/configCases/asset-modules/minimize-fragment-names/test.config.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ module.exports = {
2020
);
2121
}
2222

23+
// The extension is read before the fragment, so `#a.css` claims nothing.
24+
expect(read("1/data.txt")).toBe("not css {\n");
25+
2326
// Only the second build enables native CSS and HTML.
2427
expect(read("1/style.css")).toBe(".fragment{color:red}");
2528
expect(read("1/page.html")).toBe(

0 commit comments

Comments
 (0)