fix: error with contenthash and css experiment

This commit is contained in:
Alexander Akait 2024-06-19 19:17:27 +03:00 committed by GitHub
commit 0d07d65b01
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 117 additions and 17 deletions

View File

@ -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({

View File

@ -0,0 +1,4 @@
body {
background: yellow;
color: green;
}

View File

@ -0,0 +1 @@
export const name = 'async';

View File

@ -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);
});

View File

@ -0,0 +1,4 @@
body {
background: green;
color: yellow;
}

View File

@ -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);
}
};

View File

@ -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
}
};