Skip to content

Commit 0d07d65

Browse files
fix: error with contenthash and css experiment
2 parents 10638c0 + 8efac43 commit 0d07d65

7 files changed

Lines changed: 117 additions & 17 deletions

File tree

lib/css/CssModulesPlugin.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -368,24 +368,24 @@ class CssModulesPlugin {
368368

369369
/** @type {CssModule[] | undefined} */
370370
const modules = orderedCssModulesPerChunk.get(chunk);
371-
const { path: filename, info } = compilation.getPathWithInfo(
372-
CssModulesPlugin.getChunkFilenameTemplate(
373-
chunk,
374-
compilation.outputOptions
375-
),
376-
{
377-
hash,
378-
runtime: chunk.runtime,
379-
chunk,
380-
contentHashType: "css"
381-
}
382-
);
383-
const undoPath = getUndoPath(
384-
filename,
385-
compilation.outputOptions.path,
386-
false
387-
);
388371
if (modules !== undefined) {
372+
const { path: filename, info } = compilation.getPathWithInfo(
373+
CssModulesPlugin.getChunkFilenameTemplate(
374+
chunk,
375+
compilation.outputOptions
376+
),
377+
{
378+
hash,
379+
runtime: chunk.runtime,
380+
chunk,
381+
contentHashType: "css"
382+
}
383+
);
384+
const undoPath = getUndoPath(
385+
filename,
386+
compilation.outputOptions.path,
387+
false
388+
);
389389
result.push({
390390
render: () =>
391391
this.renderChunk({
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
body {
2+
background: yellow;
3+
color: green;
4+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const name = 'async';
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import * as style from "./style.css";
2+
3+
it("should work with js", done => {
4+
import('./async.js').then(x => {
5+
expect(x.name).toBe("async")
6+
done();
7+
}, done);
8+
});
9+
10+
it("should work with css", done => {
11+
expect(style).toEqual(nsObj({}));
12+
13+
const computedStyle = getComputedStyle(document.body);
14+
15+
expect(computedStyle.getPropertyValue("background")).toBe(" green");
16+
expect(computedStyle.getPropertyValue("color")).toBe(" yellow");
17+
18+
import("./async.css").then(x => {
19+
expect(x).toEqual(nsObj({}));
20+
21+
const style = getComputedStyle(document.body);
22+
23+
expect(style.getPropertyValue("background")).toBe(" yellow");
24+
expect(style.getPropertyValue("color")).toBe(" green");
25+
26+
done();
27+
}, done);
28+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
body {
2+
background: green;
3+
color: yellow;
4+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const fs = require("fs");
2+
3+
let cssBundle;
4+
5+
module.exports = {
6+
findBundle: function (_, options) {
7+
const jsBundleRegex = new RegExp(/^bundle\..+\.js$/, "i");
8+
const cssBundleRegex = new RegExp(/^bundle\..+\.css$/, "i");
9+
const asyncRegex = new RegExp(/^async\..+\.js$/, "i");
10+
const files = fs.readdirSync(options.output.path);
11+
const jsBundle = files.find(file => jsBundleRegex.test(file));
12+
13+
if (!jsBundle) {
14+
throw new Error(
15+
`No file found with correct name (regex: ${
16+
jsBundleRegex.source
17+
}, files: ${files.join(", ")})`
18+
);
19+
}
20+
21+
const async = files.find(file => asyncRegex.test(file));
22+
23+
if (!async) {
24+
throw new Error(
25+
`No file found with correct name (regex: ${
26+
asyncRegex.source
27+
}, files: ${files.join(", ")})`
28+
);
29+
}
30+
31+
cssBundle = files.find(file => cssBundleRegex.test(file));
32+
33+
if (!cssBundle) {
34+
throw new Error(
35+
`No file found with correct name (regex: ${
36+
cssBundleRegex.source
37+
}, files: ${files.join(", ")})`
38+
);
39+
}
40+
41+
return [jsBundle, async];
42+
},
43+
moduleScope(scope) {
44+
const link = scope.window.document.createElement("link");
45+
link.rel = "stylesheet";
46+
link.href = cssBundle;
47+
scope.window.document.head.appendChild(link);
48+
}
49+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/** @type {import("../../../../").Configuration} */
2+
module.exports = {
3+
target: "web",
4+
mode: "development",
5+
output: {
6+
filename: "bundle.[name].[contenthash].js",
7+
cssFilename: "bundle.[name].[contenthash].css",
8+
chunkFilename: "async.[name].[contenthash].js",
9+
cssChunkFilename: "async.[name].[contenthash].css"
10+
},
11+
experiments: {
12+
css: true
13+
}
14+
};

0 commit comments

Comments
 (0)