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

chore: remove define plugin in rspack-cli #6219

Merged
merged 2 commits into from
May 15, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 0 additions & 16 deletions packages/rspack-cli/src/rspack-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,22 +186,6 @@ export class RspackCLI {
}
}

// Tells webpack to set process.env.NODE_ENV to a given string value.
// optimization.nodeEnv uses DefinePlugin unless set to false.
// optimization.nodeEnv defaults to mode if set, else falls back to 'production'.
// See doc: https://webpack.js.org/configuration/optimization/#optimizationnodeenv
// See source: https://github.com/webpack/webpack/blob/8241da7f1e75c5581ba535d127fa66aeb9eb2ac8/lib/WebpackOptionsApply.js#L563

// When mode is set to 'none', optimization.nodeEnv defaults to false.
if (item.mode !== "none") {
(item.plugins ||= []).push(
new rspackCore.DefinePlugin({
// User defined `process.env.NODE_ENV` always has highest priority than default define
"process.env.NODE_ENV": JSON.stringify(item.mode)
})
);
}

if (typeof item.stats === "undefined") {
item.stats = { preset: "errors-warnings", timings: true };
} else if (typeof item.stats === "boolean") {
Expand Down
22 changes: 4 additions & 18 deletions packages/rspack-cli/tests/build/basic/public/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/rspack-cli/tests/build/issue-6214/entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const long_name_should_be_minified = process.env.NODE_ENV;
console.log(long_name_should_be_minified);
9 changes: 9 additions & 0 deletions packages/rspack-cli/tests/build/issue-6214/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { readFile, run } from "../../utils/test-utils";
import { resolve } from "path";

it("should not have `process.env.NODE_ENV` when optimization.nodeEnv has been set", async () => {
await run(__dirname, ["--mode", "production"]);
const mainJs = await readFile(resolve(__dirname, "dist/main.js"), "utf-8");
expect(mainJs).toContain("process.env.NODE_ENV");
expect(mainJs).not.toContain("long_name_should_be_minified");
});
14 changes: 14 additions & 0 deletions packages/rspack-cli/tests/build/issue-6214/rspack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const path = require("path");

/** @type {import('@rspack/cli').Configuration} */
module.exports = {
mode: "development", // will be override to "production" by "--mode"
entry: "./entry.js",
output: {
clean: true,
path: path.resolve(__dirname, "dist")
},
optimization: {
nodeEnv: false
}
};