mirror of https://github.com/webpack/webpack.git
fix: sourceTypes of asset module when lazy compilation (#19539)
This commit is contained in:
parent
6b3e1c5845
commit
2c4f967fa6
|
|
@ -657,6 +657,7 @@ class AssetGenerator extends Generator {
|
||||||
* @returns {SourceTypes} available types (do not mutate)
|
* @returns {SourceTypes} available types (do not mutate)
|
||||||
*/
|
*/
|
||||||
getTypes(module) {
|
getTypes(module) {
|
||||||
|
/** @type {Set<string>} */
|
||||||
const sourceTypes = new Set();
|
const sourceTypes = new Set();
|
||||||
const connections = this._moduleGraph.getIncomingConnections(module);
|
const connections = this._moduleGraph.getIncomingConnections(module);
|
||||||
|
|
||||||
|
|
@ -669,27 +670,25 @@ class AssetGenerator extends Generator {
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((module.buildInfo && module.buildInfo.dataUrl) || this.emit === false) {
|
if ((module.buildInfo && module.buildInfo.dataUrl) || this.emit === false) {
|
||||||
if (sourceTypes) {
|
if (sourceTypes.size > 0) {
|
||||||
if (sourceTypes.has("javascript") && sourceTypes.has("css")) {
|
if (sourceTypes.has("javascript") && sourceTypes.has("css")) {
|
||||||
return JS_AND_CSS_URL_TYPES;
|
return JS_AND_CSS_URL_TYPES;
|
||||||
} else if (sourceTypes.has("javascript")) {
|
|
||||||
return JS_TYPES;
|
|
||||||
} else if (sourceTypes.has("css")) {
|
} else if (sourceTypes.has("css")) {
|
||||||
return CSS_URL_TYPES;
|
return CSS_URL_TYPES;
|
||||||
}
|
}
|
||||||
|
return JS_TYPES;
|
||||||
}
|
}
|
||||||
|
|
||||||
return NO_TYPES;
|
return NO_TYPES;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sourceTypes) {
|
if (sourceTypes.size > 0) {
|
||||||
if (sourceTypes.has("javascript") && sourceTypes.has("css")) {
|
if (sourceTypes.has("javascript") && sourceTypes.has("css")) {
|
||||||
return ASSET_AND_JS_AND_CSS_URL_TYPES;
|
return ASSET_AND_JS_AND_CSS_URL_TYPES;
|
||||||
} else if (sourceTypes.has("javascript")) {
|
|
||||||
return ASSET_AND_JS_TYPES;
|
|
||||||
} else if (sourceTypes.has("css")) {
|
} else if (sourceTypes.has("css")) {
|
||||||
return ASSET_AND_CSS_URL_TYPES;
|
return ASSET_AND_CSS_URL_TYPES;
|
||||||
}
|
}
|
||||||
|
return ASSET_AND_JS_TYPES;
|
||||||
}
|
}
|
||||||
|
|
||||||
return ASSET_TYPES;
|
return ASSET_TYPES;
|
||||||
|
|
|
||||||
|
|
@ -122,6 +122,7 @@ class AssetSourceGenerator extends Generator {
|
||||||
* @returns {SourceTypes} available types (do not mutate)
|
* @returns {SourceTypes} available types (do not mutate)
|
||||||
*/
|
*/
|
||||||
getTypes(module) {
|
getTypes(module) {
|
||||||
|
/** @type {Set<string>} */
|
||||||
const sourceTypes = new Set();
|
const sourceTypes = new Set();
|
||||||
const connections = this._moduleGraph.getIncomingConnections(module);
|
const connections = this._moduleGraph.getIncomingConnections(module);
|
||||||
|
|
||||||
|
|
@ -133,13 +134,14 @@ class AssetSourceGenerator extends Generator {
|
||||||
sourceTypes.add(connection.originModule.type.split("/")[0]);
|
sourceTypes.add(connection.originModule.type.split("/")[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (sourceTypes.size > 0) {
|
||||||
if (sourceTypes.has("javascript") && sourceTypes.has("css")) {
|
if (sourceTypes.has("javascript") && sourceTypes.has("css")) {
|
||||||
return JS_AND_CSS_URL_TYPES;
|
return JS_AND_CSS_URL_TYPES;
|
||||||
} else if (sourceTypes.has("javascript")) {
|
|
||||||
return JS_TYPES;
|
|
||||||
} else if (sourceTypes.has("css")) {
|
} else if (sourceTypes.has("css")) {
|
||||||
return CSS_URL_TYPES;
|
return CSS_URL_TYPES;
|
||||||
}
|
}
|
||||||
|
return JS_TYPES;
|
||||||
|
}
|
||||||
|
|
||||||
return NO_TYPES;
|
return NO_TYPES;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
@ -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));
|
||||||
|
});
|
||||||
|
})
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
@ -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"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
};
|
||||||
Loading…
Reference in New Issue