Skip to content

Commit

Permalink
fix: error with contenthash and css experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Jun 19, 2024
2 parents 10638c0 + 8efac43 commit 0d07d65
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 17 deletions.
34 changes: 17 additions & 17 deletions lib/css/CssModulesPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,24 +368,24 @@ class CssModulesPlugin {

/** @type {CssModule[] | undefined} */
const modules = orderedCssModulesPerChunk.get(chunk);
const { path: filename, info } = compilation.getPathWithInfo(
CssModulesPlugin.getChunkFilenameTemplate(
chunk,
compilation.outputOptions
),
{
hash,
runtime: chunk.runtime,
chunk,
contentHashType: "css"
}
);
const undoPath = getUndoPath(
filename,
compilation.outputOptions.path,
false
);
if (modules !== undefined) {
const { path: filename, info } = compilation.getPathWithInfo(
CssModulesPlugin.getChunkFilenameTemplate(
chunk,
compilation.outputOptions
),
{
hash,
runtime: chunk.runtime,
chunk,
contentHashType: "css"
}
);
const undoPath = getUndoPath(
filename,
compilation.outputOptions.path,
false
);
result.push({
render: () =>
this.renderChunk({
Expand Down
4 changes: 4 additions & 0 deletions test/configCases/css/contenthash/async.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
body {
background: yellow;
color: green;
}
1 change: 1 addition & 0 deletions test/configCases/css/contenthash/async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const name = 'async';
28 changes: 28 additions & 0 deletions test/configCases/css/contenthash/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as style from "./style.css";

it("should work with js", done => {
import('./async.js').then(x => {
expect(x.name).toBe("async")
done();
}, done);
});

it("should work with css", done => {
expect(style).toEqual(nsObj({}));

const computedStyle = getComputedStyle(document.body);

expect(computedStyle.getPropertyValue("background")).toBe(" green");
expect(computedStyle.getPropertyValue("color")).toBe(" yellow");

import("./async.css").then(x => {
expect(x).toEqual(nsObj({}));

const style = getComputedStyle(document.body);

expect(style.getPropertyValue("background")).toBe(" yellow");
expect(style.getPropertyValue("color")).toBe(" green");

done();
}, done);
});
4 changes: 4 additions & 0 deletions test/configCases/css/contenthash/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
body {
background: green;
color: yellow;
}
49 changes: 49 additions & 0 deletions test/configCases/css/contenthash/test.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const fs = require("fs");

let cssBundle;

module.exports = {
findBundle: function (_, options) {
const jsBundleRegex = new RegExp(/^bundle\..+\.js$/, "i");
const cssBundleRegex = new RegExp(/^bundle\..+\.css$/, "i");
const asyncRegex = new RegExp(/^async\..+\.js$/, "i");
const files = fs.readdirSync(options.output.path);
const jsBundle = files.find(file => jsBundleRegex.test(file));

if (!jsBundle) {
throw new Error(
`No file found with correct name (regex: ${
jsBundleRegex.source
}, files: ${files.join(", ")})`
);
}

const async = files.find(file => asyncRegex.test(file));

if (!async) {
throw new Error(
`No file found with correct name (regex: ${
asyncRegex.source
}, files: ${files.join(", ")})`
);
}

cssBundle = files.find(file => cssBundleRegex.test(file));

if (!cssBundle) {
throw new Error(
`No file found with correct name (regex: ${
cssBundleRegex.source
}, files: ${files.join(", ")})`
);
}

return [jsBundle, async];
},
moduleScope(scope) {
const link = scope.window.document.createElement("link");
link.rel = "stylesheet";
link.href = cssBundle;
scope.window.document.head.appendChild(link);
}
};
14 changes: 14 additions & 0 deletions test/configCases/css/contenthash/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/** @type {import("../../../../").Configuration} */
module.exports = {
target: "web",
mode: "development",
output: {
filename: "bundle.[name].[contenthash].js",
cssFilename: "bundle.[name].[contenthash].css",
chunkFilename: "async.[name].[contenthash].js",
cssChunkFilename: "async.[name].[contenthash].css"
},
experiments: {
css: true
}
};

0 comments on commit 0d07d65

Please sign in to comment.