8000 fix: sourceTypes of asset module when lazy compilation by hai-x · Pull Request #19539 · webpack/webpack · GitHub
[go: up one dir, main page]

Skip to content

fix: sourceTypes of asset module when lazy compilation #19539

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions lib/asset/AssetGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;
Expand Down
12 changes: 7 additions & 5 deletions lib/asset/AssetSourceGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions test/hotCases/asset-modules-source/lazy-compilation/file.text
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions test/hotCases/asset-modules-source/lazy-compilation/index.js
Original file line number Diff line number Diff line change
@@ -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));
});
})
);
});
Original file line number Diff line number Diff line change
@@ -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
0