Skip to content

Commit 2b20c1c

Browse files
committed
fix(module-federation): encode runtime data URI and handle named wrappers
1 parent aed1ab4 commit 2b20c1c

File tree

6 files changed

+75
-2
lines changed

6 files changed

+75
-2
lines changed

packages/rspack/src/Template.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ const DELTA_A_TO_Z = 'z'.charCodeAt(0) - START_LOWERCASE_ALPHABET_CODE + 1;
1414
const NUMBER_OF_IDENTIFIER_START_CHARS = DELTA_A_TO_Z * 2 + 2; // a-z A-Z _ $
1515
const NUMBER_OF_IDENTIFIER_CONTINUATION_CHARS =
1616
NUMBER_OF_IDENTIFIER_START_CHARS + 10; // a-z A-Z _ $ 0-9
17-
const FUNCTION_CONTENT_REGEX = /^function\s?\(\)\s?\{\r?\n?|\r?\n?\}$/g;
17+
const FUNCTION_CONTENT_REGEX =
18+
/^function(?:\s+[\w$]+)?\s?\(\)\s?\{\r?\n?|\r?\n?\}$/g;
1819
const INDENT_MULTILINE_REGEX = /^\t/gm;
1920
const LINE_SEPARATOR_REGEX = /\r?\n/g;
2021
const IDENTIFIER_NAME_REPLACE_REGEX = /^([^a-zA-Z$_])/;

packages/rspack/src/container/ModuleFederationPlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,5 +346,5 @@ function getDefaultEntryRuntime(
346346
require('./moduleFederationDefaultRuntime.js').default,
347347
),
348348
].join(';');
349-
return `@module-federation/runtime/rspack.js!=!data:text/javascript,${content}`;
349+
return `@module-federation/runtime/rspack.js!=!data:text/javascript,${encodeURIComponent(content)}`;
350350
}

tests/rspack-test/Template.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const { Template } = require("@rspack/core");
2+
3+
describe("Template.getFunctionContent", () => {
4+
it("should strip named function wrappers", () => {
5+
const content = Template.getFunctionContent(function moduleFederationDefaultRuntime_default() {
6+
return "ok";
7+
});
8+
9+
expect(content).toContain('return "ok";');
10+
expect(content).not.toContain("function moduleFederationDefaultRuntime_default");
11+
});
12+
13+
it("should strip anonymous function wrappers", () => {
14+
const content = Template.getFunctionContent(function () {
15+
return "ok";
16+
});
17+
18+
expect(content).toContain('return "ok";');
19+
expect(content).not.toContain("function ()");
20+
});
21+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
it("should expose module with encoded mf runtime data uri entry", async () => {
2+
await __webpack_init_sharing__("default");
3+
const container = __non_webpack_require__("./container.js");
4+
container.init(__webpack_share_scopes__.default);
5+
const moduleFactory = await container.get("./module");
6+
expect(moduleFactory().ok).toBe(true);
7+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const ok = true;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const { ModuleFederationPlugin } = require("@rspack/core").container;
2+
3+
class AssertEncodedMFRuntimeDataUriPlugin {
4+
apply(compiler) {
5+
compiler.hooks.compilation.tap(
6+
"AssertEncodedMFRuntimeDataUriPlugin",
7+
(_, { normalModuleFactory }) => {
8+
normalModuleFactory.hooks.beforeResolve.tap(
9+
"AssertEncodedMFRuntimeDataUriPlugin",
10+
(resolveData) => {
11+
const request = resolveData?.request;
12+
const prefix =
13+
"@module-federation/runtime/rspack.js!=!data:text/javascript,";
14+
15+
if (!request || !request.startsWith(prefix)) {
16+
return;
17+
}
18+
19+
const dataUriContent = request.slice(prefix.length);
20+
21+
expect(dataUriContent).toMatch(/%[0-9A-Fa-f]{2}/);
22+
expect(dataUriContent).not.toContain(
23+
"import __module_federation_bundler_runtime__ from",
24+
);
25+
},
26+
);
27+
},
28+
);
29+
}
30+
}
31+
32+
/** @type {import("@rspack/core").Configuration} */
33+
module.exports = {
34+
plugins: [
35+
new ModuleFederationPlugin({
36+
name: "container",
37+
filename: "container.js",
38+
library: { type: "commonjs-module" },
39+
exposes: ["./module"],
40+
}),
41+
new AssertEncodedMFRuntimeDataUriPlugin(),
42+
],
43+
};

0 commit comments

Comments
 (0)