Skip to content

Commit 50426fa

Browse files
committed
fix(container): align remote fallback index with registered externals
The factorize hook built the RemoteModule's external requests from the raw array index, while registration skips `internal ` entries without advancing its counter. When an `internal ` external preceded a real one the RemoteModule referenced an unregistered `webpack/container/reference/<key>/fallback-N` request and the build failed to resolve it. Use one compacted counter that mirrors the registration loop so the names line up. Signed-off-by: Eshiv Pandey <[email protected]>
1 parent 045d2ab commit 50426fa

7 files changed

Lines changed: 69 additions & 7 deletions

File tree

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+
Keep the remote fallback index aligned with registered container externals when an `internal ` external precedes a real one.

‎lib/container/ContainerReferencePlugin.js‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,23 @@ class ContainerReferencePlugin {
173173
(data.request.length === key.length ||
174174
data.request.charCodeAt(key.length) === slashCode)
175175
) {
176+
// Mirror the registration loop above: the fallback index only
177+
// counts non-internal externals, so `internal ` entries must
178+
// not advance it or the names drift out of remoteExternals.
179+
let i = 0;
180+
const externalRequests = config.external.map((external) => {
181+
if (external.startsWith("internal ")) {
182+
return external.slice(9);
183+
}
184+
const request = `webpack/container/reference/${key}${
185+
i ? `/fallback-${i}` : ""
186+
}`;
187+
i++;
188+
return request;
189+
});
176190
return new RemoteModule(
177191
data.request,
178-
config.external.map((external, i) =>
179-
external.startsWith("internal ")
180-
? external.slice(9)
181-
: `webpack/container/reference/${key}${
182-
i ? `/fallback-${i}` : ""
183-
}`
184-
),
192+
externalRequests,
185193
`.${data.request.slice(key.length)}`,
186194
config.shareScope
187195
);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use strict";
2+
3+
module.exports = {
4+
get(module) {
5+
return Promise.resolve(() => `fallback ${module}`);
6+
}
7+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
it("should resolve a remote when an internal fallback precedes a real external", () => {
2+
return import("./module").then(({ test }) => test());
3+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import abc from "abc/hello-world";
2+
3+
export function test() {
4+
// The real external "ABC" resolves through the registered request; before the
5+
// fix its request name was shifted by the leading `internal ` entry and the
6+
// build failed to resolve it.
7+
expect(abc).toBe("abc ./hello-world");
8+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"use strict";
2+
3+
module.exports = {
4+
moduleScope(scope) {
5+
scope.ABC = {
6+
get(module) {
7+
return new Promise((resolve) => {
8+
resolve(() => `abc ${module}`);
9+
});
10+
}
11+
};
12+
}
13+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"use strict";
2+
3+
const { ContainerReferencePlugin } = require("../../../../").container;
4+
5+
/** @type {import("../../../../").Configuration} */
6+
module.exports = {
7+
plugins: [
8+
new ContainerReferencePlugin({
9+
remoteType: "var",
10+
remotes: {
11+
// An `internal ` fallback before a real external must not shift the
12+
// fallback index, or the real external references an unregistered
13+
// container request and the build fails.
14+
abc: ["internal ./fallback-abc", "ABC"]
15+
}
16+
})
17+
]
18+
};

0 commit comments

Comments
 (0)