Skip to content

Commit de107f8

Browse files
authored
fix(VirtualUrlPlugin): set resourceData.context to avoid invalid fallback (#20390)
1 parent a656ab1 commit de107f8

File tree

6 files changed

+47
-0
lines changed

6 files changed

+47
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Fix VirtualUrlPlugin to set resourceData.context for proper module resolution. Previously, when context was not set, it would fallback to the virtual scheme path (e.g., `virtual:routes`), which is not a valid filesystem path, causing subsequent resolve operations to fail.

lib/schemes/VirtualUrlPlugin.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ class VirtualUrlPlugin {
139139
const type = virtualConfig.type;
140140
resourceData.path = path + type;
141141
resourceData.resource = path;
142+
resourceData.context = compiler.context;
142143

143144
if (virtualConfig.version) {
144145
const cacheKey = toCacheKey(resourceData.resource, scheme);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import routes from "virtual:routes";
2+
3+
it("should correctly load virtual modules with the js type.", () => {
4+
expect(routes[0]).toBe("react");
5+
});

test/configCases/plugins/virtual-url-plugin-mf/node_modules/package.json

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/configCases/plugins/virtual-url-plugin-mf/node_modules/react.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"use strict";
2+
3+
const webpack = require("../../../../");
4+
5+
/** @type {import("webpack").Configuration} */
6+
const config = {
7+
plugins: [
8+
new webpack.container.ModuleFederationPlugin({
9+
name: "host",
10+
shared: {
11+
react: {
12+
import: "react",
13+
eager: true,
14+
singleton: false
15+
}
16+
}
17+
}),
18+
new webpack.experiments.schemes.VirtualUrlPlugin({
19+
type: ".js",
20+
routes: {
21+
source() {
22+
return `
23+
import React from 'react';
24+
export default [React];
25+
`;
26+
}
27+
}
28+
})
29+
]
30+
};
31+
32+
module.exports = config;

0 commit comments

Comments
 (0)