fix: sourceTypes of asset module when lazy compilation (#19539)

This commit is contained in:
hai-x 2025-05-20 19:40:49 +08:00 committed by GitHub
parent 6b3e1c5845
commit 2c4f967fa6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 64 additions and 11 deletions

View File

@ -657,6 +657,7 @@ class AssetGenerator extends Generator {
* @returns {SourceTypes} available types (do not mutate)
*/
getTypes(module) {
/** @type {Set<string>} */
const sourceTypes = new Set();
const connections = this._moduleGraph.getIncomingConnections(module);
@ -669,27 +670,25 @@ class AssetGenerator extends Generator {
}
if ((module.buildInfo && module.buildInfo.dataUrl) || this.emit === false) {
if (sourceTypes) {
if (sourceTypes.size > 0) {
if (sourceTypes.has("javascript") && sourceTypes.has("css")) {
return JS_AND_CSS_URL_TYPES;
} else if (sourceTypes.has("javascript")) {
return JS_TYPES;
} else if (sourceTypes.has("css")) {
return CSS_URL_TYPES;
}
return JS_TYPES;
}
return NO_TYPES;
}
if (sourceTypes) {
if (sourceTypes.size > 0) {
if (sourceTypes.has("javascript") && sourceTypes.has("css")) {
return ASSET_AND_JS_AND_CSS_URL_TYPES;
} else if (sourceTypes.has("javascript")) {
return ASSET_AND_JS_TYPES;
} else if (sourceTypes.has("css")) {
return ASSET_AND_CSS_URL_TYPES;
}
return ASSET_AND_JS_TYPES;
}
return ASSET_TYPES;

View File

@ -122,6 +122,7 @@ class AssetSourceGenerator extends Generator {
* @returns {SourceTypes} available types (do not mutate)
*/
getTypes(module) {
/** @type {Set<string>} */
const sourceTypes = new Set();
const connections = this._moduleGraph.getIncomingConnections(module);
@ -133,12 +134,13 @@ class AssetSourceGenerator extends Generator {
sourceTypes.add(connection.originModule.type.split("/")[0]);
}
if (sourceTypes.has("javascript") && sourceTypes.has("css")) {
return JS_AND_CSS_URL_TYPES;
} else if (sourceTypes.has("javascript")) {
if (sourceTypes.size > 0) {
if (sourceTypes.has("javascript") && sourceTypes.has("css")) {
return JS_AND_CSS_URL_TYPES;
} else if (sourceTypes.has("css")) {
return CSS_URL_TYPES;
}
return JS_TYPES;
} else if (sourceTypes.has("css")) {
return CSS_URL_TYPES;
}
return NO_TYPES;

View File

@ -0,0 +1,6 @@
We use `NEXT()` to trigger one compilation to simulate lazy compilation behavior.
So this initial content will be ignored.
---
A
---
B

View File

@ -0,0 +1,21 @@
const getFile = name =>
__non_webpack_require__("fs").readFileSync(
__non_webpack_require__("path").join(__dirname, name),
"utf-8"
);
it("should work", async function (done) {
let promise = import("./file.text");
NEXT(
require("../../update")(done, true, () => {
promise.then(() => {
expect(getFile("./assets/file.text")).toContain("A");
module.hot.accept("./file.text", () => {
expect(getFile("./assets/file.text")).toContain("B");
done();
});
NEXT(require("../../update")(done));
});
})
);
});

View File

@ -0,0 +1,25 @@
/** @type {import("../../../../").Configuration} */
module.exports = {
experiments: {
lazyCompilation: {
entries: false,
imports: true
}
},
node: {
__dirname: false
},
module: {
generator: {
asset: {
filename: "assets/[name][ext]"
}
},
rules: [
{
test: /file\.text$/,
type: "asset/resource"
}
]
}
};