Skip to content
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

fix: not enable rspack-plugin-react-refresh when mode set to 'product… #5260

Merged
merged 3 commits into from
Jan 8, 2024
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
24 changes: 22 additions & 2 deletions packages/rspack-dev-server/tests/normalizeOptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,44 @@ describe("normalize options snapshot", () => {
).toMatchSnapshot();
});

it("shouldn't have reactRefreshEntry.js by default when rspackFuture.disableReactRefreshByDefault is enabled", async () => {
it("shouldn't have reactRefreshEntry.js by default when in production mode", async () => {
const reactRefreshEntry =
"<prefix>/rspack-plugin-react-refresh/client/reactRefreshEntry.js";
const entries1 = await getAdditionEntries(
{},
{
mode: "production",
entry: ["something"]
}
);
expect(entries1["undefined"]).not.toContain(reactRefreshEntry);
const entries2 = await getAdditionEntries(
{},
{
mode: "production",
entry: ["something"],
plugins: [new ReactRefreshPlugin()]
plugins: [new ReactRefreshPlugin({ forceEnable: true })]
}
);
expect(entries2["undefined"]).toContain(reactRefreshEntry);
const entries3 = await getAdditionEntries(
{},
{
mode: "development",
entry: ["something"],
plugins: [new ReactRefreshPlugin()]
}
);
expect(entries3["undefined"]).toContain(reactRefreshEntry);
const entries4 = await getAdditionEntries(
{},
{
mode: "production",
entry: ["something"],
plugins: [new ReactRefreshPlugin()]
}
);
expect(entries4["undefined"]).not.toContain(reactRefreshEntry);
});

it("should apply HMR plugin by default", async () => {
Expand Down
11 changes: 11 additions & 0 deletions packages/rspack-plugin-react-refresh/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ class ReactRefreshRspackPlugin {
}

apply(compiler: Compiler) {
if (
// Webpack do not set process.env.NODE_ENV, so we need to check for mode.
// Ref: https://github.com/webpack/webpack/issues/7074
(compiler.options.mode !== "development" ||
// We also check for production process.env.NODE_ENV,
// in case it was set and mode is non-development (e.g. 'none')
(process.env.NODE_ENV && process.env.NODE_ENV === "production")) &&
!this.options.forceEnable
) {
return;
}
new compiler.webpack.EntryPlugin(compiler.context, reactRefreshEntryPath, {
name: undefined
}).apply(compiler);
Expand Down
2 changes: 2 additions & 0 deletions packages/rspack-plugin-react-refresh/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export type PluginOptions = {
include?: string | RegExp | (string | RegExp)[] | null;
exclude?: string | RegExp | (string | RegExp)[] | null;
library?: string;
forceEnable?: boolean;
};

const d = <K extends keyof PluginOptions>(
Expand All @@ -22,5 +23,6 @@ export function normalizeOptions(options: PluginOptions) {
d(options, "exclude", /node_modules/i);
d(options, "include", /\.([cm]js|[jt]sx?|flow)$/i);
d(options, "library");
d(options, "forceEnable", false);
return options;
}
Loading