Skip to content

Commit b7840ef

Browse files
authored
feat: support library for instance unique name (#4628)
* feat: support library for instance unique name * feat: add template for build
1 parent 47b828c commit b7840ef

File tree

5 files changed

+43
-4
lines changed

5 files changed

+43
-4
lines changed

packages/rspack-plugin-react-refresh/client/reactRefreshEntry.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ var safeThis = (function () {
3737
if (process.env.NODE_ENV !== "production") {
3838
if (typeof safeThis !== "undefined") {
3939
var $RefreshInjected$ = "__reactRefreshInjected";
40+
// Namespace the injected flag (if necessary) for monorepo compatibility
41+
if (
42+
typeof __react_refresh_library__ !== "undefined" &&
43+
__react_refresh_library__
44+
) {
45+
$RefreshInjected$ += "_" + __react_refresh_library__;
46+
}
47+
4048
// Only inject the runtime if it hasn't been injected
4149
if (!safeThis[$RefreshInjected$]) {
4250
RefreshRuntime.injectIntoGlobalHook(safeThis);

packages/rspack-plugin-react-refresh/src/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import path from "path";
22
import type { Compiler } from "@rspack/core";
3+
import { DefinePlugin } from "@rspack/core";
34
import { normalizeOptions, type PluginOptions } from "./options";
45

56
export type { PluginOptions };
@@ -55,6 +56,18 @@ class ReactRefreshRspackPlugin {
5556
use: "builtin:react-refresh-loader"
5657
});
5758

59+
const definedModules = {
60+
// For Mutiple Instance Mode
61+
__react_refresh_library__: JSON.stringify(
62+
compiler.webpack.Template.toIdentifier(
63+
this.options.library ||
64+
compiler.options.output.uniqueName ||
65+
compiler.options.output.library
66+
)
67+
)
68+
};
69+
new DefinePlugin(definedModules).apply(compiler);
70+
5871
const refreshPath = path.dirname(require.resolve("react-refresh"));
5972
compiler.options.resolve.alias = {
6073
"react-refresh": refreshPath,

packages/rspack-plugin-react-refresh/src/options.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
export type PluginOptions = {
22
include?: string | RegExp | (string | RegExp)[] | null;
33
exclude?: string | RegExp | (string | RegExp)[] | null;
4+
library?: string;
45
};
56

6-
const d = (
7+
const d = <K extends keyof PluginOptions>(
78
object: PluginOptions,
8-
property: keyof PluginOptions,
9-
defaultValue: PluginOptions[keyof PluginOptions]
9+
property: K,
10+
defaultValue?: PluginOptions[K]
1011
) => {
1112
if (
1213
typeof object[property] === "undefined" &&
@@ -20,5 +21,6 @@ const d = (
2021
export function normalizeOptions(options: PluginOptions) {
2122
d(options, "exclude", /node_modules/i);
2223
d(options, "include", /\.([cm]js|[jt]sx?|flow)$/i);
24+
d(options, "library");
2325
return options;
2426
}

packages/rspack-plugin-react-refresh/tests/test.spec.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const path = require("path");
33
const { rspack } = require("@rspack/core");
44
const ReactRefreshPlugin = require("@rspack/plugin-react-refresh");
55

6+
const uniqueName = "ReactRefreshLibrary";
67
const compileWithReactRefresh = (fixturePath, refreshOptions, callback) => {
78
let dist = path.join(fixturePath, "dist");
89
rspack(
@@ -13,7 +14,8 @@ const compileWithReactRefresh = (fixturePath, refreshOptions, callback) => {
1314
fixture: path.join(fixturePath, "index.js")
1415
},
1516
output: {
16-
path: dist
17+
path: dist,
18+
uniqueName
1719
},
1820
plugins: [new ReactRefreshPlugin(refreshOptions)],
1921
optimization: {
@@ -89,6 +91,17 @@ describe("react-refresh-rspack-plugin", () => {
8991
);
9092
});
9193

94+
it("should add library to make sure work in Micro-Frontend", done => {
95+
compileWithReactRefresh(
96+
path.join(__dirname, "fixtures/default"),
97+
{},
98+
(_, __, { reactRefresh, fixture, runtime, vendor }) => {
99+
expect(reactRefresh).toContain(uniqueName);
100+
done();
101+
}
102+
);
103+
});
104+
92105
it("should include selected file when compiling", done => {
93106
compileWithReactRefresh(
94107
path.join(__dirname, "fixtures/custom"),

packages/rspack/src/Compiler.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,9 @@ class Compiler {
227227
get CopyRspackPlugin() {
228228
return require("./builtin-plugin").CopyRspackPlugin;
229229
},
230+
get Template() {
231+
return require("./Template");
232+
},
230233
optimize: {
231234
get LimitChunkCountPlugin() {
232235
return require("./builtin-plugin").LimitChunkCountPlugin;

0 commit comments

Comments
 (0)