Skip to content

Commit

Permalink
chore: remove define plugin in rspack-cli (#6219)
Browse files Browse the repository at this point in the history
* chore: remove define plugin in rspack-cli

* test: add test
  • Loading branch information
LingyuCoder committed May 15, 2024
1 parent 2fda4a2 commit 2456d69
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 34 deletions.
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
}
};

2 comments on commit 2456d69

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

Name Base (2024-05-15 3ee5df7) Current Change
10000_development-mode + exec 2.7 s ± 29 ms 2.77 s ± 29 ms +2.84 %
10000_development-mode_hmr + exec 709 ms ± 5.9 ms 749 ms ± 2.7 ms +5.76 %
10000_production-mode + exec 2.48 s ± 16 ms 2.62 s ± 26 ms +5.66 %
arco-pro_development-mode + exec 2.48 s ± 59 ms 2.52 s ± 78 ms +1.37 %
arco-pro_development-mode_hmr + exec 433 ms ± 2.3 ms 440 ms ± 2.4 ms +1.61 %
arco-pro_development-mode_hmr_intercept-plugin + exec 441 ms ± 1.7 ms 449 ms ± 2 ms +1.86 %
arco-pro_development-mode_intercept-plugin + exec 3.28 s ± 88 ms 3.39 s ± 76 ms +3.42 %
arco-pro_production-mode + exec 4.05 s ± 133 ms 4.19 s ± 61 ms +3.25 %
arco-pro_production-mode_intercept-plugin + exec 4.89 s ± 80 ms 5.04 s ± 64 ms +3.11 %
threejs_development-mode_10x + exec 1.98 s ± 16 ms 2.02 s ± 17 ms +2.09 %
threejs_development-mode_10x_hmr + exec 775 ms ± 3.3 ms 792 ms ± 3.8 ms +2.20 %
threejs_production-mode_10x + exec 5.19 s ± 33 ms 5.35 s ± 29 ms +2.98 %

Threshold exceeded: ["10000_development-mode_hmr + exec","10000_production-mode + exec"]

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result
modernjs, ubuntu-latest ❌ failure
_selftest, ubuntu-latest ✅ success
nx, ubuntu-latest ✅ success
rspress, ubuntu-latest ✅ success
rsbuild, ubuntu-latest ❌ failure
compat, ubuntu-latest ✅ success
examples, ubuntu-latest ✅ success

Please sign in to comment.