Skip to content

Commit

Permalink
feat(cli): rspack preview support for nodeEnv flag and default env (
Browse files Browse the repository at this point in the history
#9543)

* feat(cli): `rspack preview` support for nodeEnv flag and default env

* fix
  • Loading branch information
chenjiahan authored Mar 4, 2025
1 parent 672a375 commit c8bcb8f
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 38 deletions.
14 changes: 2 additions & 12 deletions packages/rspack-cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,7 @@ export class RspackCLI {
process.env.RSPACK_CONFIG_VALIDATE ??= "loose";
process.env.WATCHPACK_WATCHER_LIMIT =
process.env.WATCHPACK_WATCHER_LIMIT || "20";
const nodeEnv = process?.env?.NODE_ENV;
const rspackCommandDefaultEnv =
rspackCommand === "build" ? "production" : "development";
if (typeof options.nodeEnv === "string") {
process.env.NODE_ENV = nodeEnv || options.nodeEnv;
} else {
process.env.NODE_ENV = nodeEnv || rspackCommandDefaultEnv;
}

let config = await this.loadConfig(options);
config = await this.buildConfig(config, options, rspackCommand);

Expand Down Expand Up @@ -124,9 +117,6 @@ export class RspackCLI {
): Promise<RspackOptions | MultiRspackOptions> {
const isBuild = command === "build";
const isServe = command === "serve";
const commandDefaultEnv: "production" | "development" = isBuild
? "production"
: "development";

const internalBuildConfig = async (item: RspackOptions) => {
if (options.entry) {
Expand Down Expand Up @@ -165,7 +155,7 @@ export class RspackCLI {
}
// auto set default mode if user config don't set it
if (!item.mode) {
item.mode = commandDefaultEnv ?? "none";
item.mode = isBuild ? "production" : "development";
}
// user parameters always has highest priority than default mode and config mode
if (options.mode) {
Expand Down
6 changes: 5 additions & 1 deletion packages/rspack-cli/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
commonOptions,
commonOptionsForBuildAndServe,
ensureEnvObject,
setBuiltinEnvArg
setBuiltinEnvArg,
setDefaultNodeEnv
} from "../utils/options";

export class BuildCommand implements RspackCommand {
Expand All @@ -33,13 +34,16 @@ export class BuildCommand implements RspackCommand {
});
},
async options => {
setDefaultNodeEnv(options, "production");
const env = ensureEnvObject(options);

if (options.watch) {
setBuiltinEnvArg(env, "WATCH", true);
} else {
setBuiltinEnvArg(env, "BUNDLE", true);
setBuiltinEnvArg(env, "BUILD", true);
}

const logger = cli.getLogger();
let createJsonStringifyStream: typeof import("@discoveryjs/json-ext").stringifyStream;
if (options.json) {
Expand Down
4 changes: 3 additions & 1 deletion packages/rspack-cli/src/commands/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type yargs from "yargs";

import type { RspackCLI } from "../cli";
import type { RspackCommand, RspackPreviewCLIOptions } from "../types";
import { commonOptions } from "../utils/options";
import { commonOptions, setDefaultNodeEnv } from "../utils/options";

const previewOptions = (yargs: yargs.Argv) => {
yargs.positional("dir", {
Expand Down Expand Up @@ -49,6 +49,8 @@ export class PreviewCommand implements RspackCommand {
"run the rspack server for build output",
previewOptions,
async options => {
setDefaultNodeEnv(options, "production");

const rspackOptions = { ...options, argv: { ...options } };
const { RspackDevServer } = await import("@rspack/dev-server");

Expand Down
5 changes: 4 additions & 1 deletion packages/rspack-cli/src/commands/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
commonOptions,
commonOptionsForBuildAndServe,
ensureEnvObject,
setBuiltinEnvArg
setBuiltinEnvArg,
setDefaultNodeEnv
} from "../utils/options";

export class ServeCommand implements RspackCommand {
Expand Down Expand Up @@ -40,7 +41,9 @@ export class ServeCommand implements RspackCommand {
}
}),
async options => {
setDefaultNodeEnv(options, "development");
setBuiltinEnvArg(ensureEnvObject(options), "SERVE", true);

const rspackOptions = {
...options,
argv: {
Expand Down
2 changes: 1 addition & 1 deletion packages/rspack-cli/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface RspackCLIOptions {
argv?: Record<string, any>;
configName?: string[];
configLoader?: string;
nodeEnv?: string;
}

export interface RspackBuildCLIOptions extends RspackCLIOptions {
Expand All @@ -35,7 +36,6 @@ export interface RspackBuildCLIOptions extends RspackCLIOptions {
analyze?: boolean;
profile?: boolean;
env?: Record<string, any>;
nodeEnv?: string;
outputPath?: string;
}

Expand Down
20 changes: 16 additions & 4 deletions packages/rspack-cli/src/utils/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export const commonOptions = (yargs: yargs.Argv) => {
default: "register",
describe:
"Specify the loader to load the config file, can be `native` or `register`."
},
nodeEnv: {
string: true,
describe: "sets `process.env.NODE_ENV` to be specified value"
}
});
};
Expand Down Expand Up @@ -53,10 +57,6 @@ export const commonOptionsForBuildAndServe = (yargs: yargs.Argv) => {
string: true,
describe: "env passed to config function"
},
nodeEnv: {
string: true,
describe: "sets process.env.NODE_ENV to be specified value"
},
devtool: {
type: "boolean",
default: false,
Expand Down Expand Up @@ -144,3 +144,15 @@ export function ensureEnvObject<T extends Record<string, unknown>>(
options.env = options.env || {};
return options.env as T;
}

export function setDefaultNodeEnv(
options: yargs.Arguments,
defaultEnv: string
) {
if (process.env.NODE_ENV !== undefined) {
return;
}

process.env.NODE_ENV =
typeof options.nodeEnv === "string" ? options.nodeEnv : defaultEnv;
}
17 changes: 8 additions & 9 deletions website/docs/en/api/cli.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ npx rspack -h

Rspack CLI provides several common flags that can be used with all commands:

| Flag | Description |
| -------------------- | ------------------------------------------------------------------------------------------------------------------------------ |
| -c, --config [value] | Specify the path to the configuration file, see [Specify the configuration file](/config/index#specify-the-configuration-file) |
| --configLoader | Specify the loader to load the config file, can be `native` or `register`, defaults to `register` |
| --configName | Specify the name of the configuration to use. |
| -h, --help | Show help information |
| -v, --version | Show version number |
| Flag | Description |
| -------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
| -c, --config [value] | Specify the path to the configuration file, see [Specify the configuration file](/config/index#specify-the-configuration-file) |
| --configLoader | Specify the loader to load the config file, can be `native` or `register`, defaults to `register` |
| --configName | Specify the name of the configuration to use. |
| --nodeEnv | Set the value of `process.env.NODE_ENV`, defaults to `development` for `rspack dev`, and `production` for `rspack build` and `rspack preview` |
| -h, --help | Show help information |
| -v, --version | Show version number |

:::tip
All flags in Rspack CLI support the `camelCase` and `kebab-case`, for example, both `--configLoader` and `--config-loader` are valid.
Expand Down Expand Up @@ -64,7 +65,6 @@ Options:
-m, --mode mode [string]
-w, --watch watch [boolean] [default: false]
--env env passed to config function [array]
--nodeEnv sets process.env.NODE_ENV to be specified value [string]
-d, --devtool devtool [boolean] [default: false]
--analyze analyze [boolean] [default: false]
--json emit stats json
Expand Down Expand Up @@ -104,7 +104,6 @@ Options:
-m, --mode mode [string]
-w, --watch watch [boolean] [default: false]
--env env passed to config function [array]
--nodeEnv sets process.env.NODE_ENV to be specified value [string]
-d, --devtool devtool [boolean] [default: false]
--hot enables hot module replacement
--port allows to specify a port to use [number]
Expand Down
17 changes: 8 additions & 9 deletions website/docs/zh/api/cli.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ npx rspack -h

Rspack CLI 提供了一些公共选项,可以用于所有命令:

| 选项 | 描述 |
| -------------------- | -------------------------------------------------------------------- |
| -c, --config [value] | 指定配置文件路径,详见 [指定配置文件](/config/index#指定配置文件) |
| --configLoader | 指定配置文件加载器,可以是 `native``register`,默认是 `register` |
| --configName | 指定配置文件名称 |
| -h, --help | 显示帮助信息 |
| -v, --version | 显示版本号 |
| 选项 | 描述 |
| -------------------- | --------------------------------------------------------------------------------------------------------------------------- |
| -c, --config [value] | 指定配置文件路径,详见 [指定配置文件](/config/index#指定配置文件) |
| --configLoader | 指定配置文件加载器,可以是 `native``register`,默认是 `register` |
| --configName | 指定配置文件名称 |
| --nodeEnv | 设置 `process.env.NODE_ENV` 的值,`rspack dev` 默认是 `development``rspack build``rspack preview` 默认是 `production` |
| -h, --help | 显示帮助信息 |
| -v, --version | 显示版本号 |

:::tip
Rspack CLI 的所有选项都支持使用 `camelCase`(驼峰命名)和 `kebab-case`(短横线命名),例如 `--configLoader``--config-loader` 都是有效的。
Expand Down Expand Up @@ -64,7 +65,6 @@ Options:
-m, --mode mode [string]
-w, --watch watch [boolean] [default: false]
--env env passed to config function [array]
--nodeEnv sets process.env.NODE_ENV to be specified value [string]
-d, --devtool devtool [boolean] [default: false]
--analyze analyze [boolean] [default: false]
--json emit stats json
Expand Down Expand Up @@ -104,7 +104,6 @@ Options:
-m, --mode mode [string]
-w, --watch watch [boolean] [default: false]
--env env passed to config function [array]
--nodeEnv sets process.env.NODE_ENV to be specified value [string]
-d, --devtool devtool [boolean] [default: false]
--hot enables hot module replacement
--port allows to specify a port to use [number]
Expand Down

2 comments on commit c8bcb8f

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented on c8bcb8f Mar 4, 2025

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 (2025-03-04 ec71bd0) Current Change
10000_big_production-mode_disable-minimize + exec 36.7 s ± 403 ms 36.7 s ± 875 ms +0.04 %
10000_development-mode + exec 1.71 s ± 20 ms 1.65 s ± 14 ms -3.60 %
10000_development-mode_hmr + exec 681 ms ± 18 ms 676 ms ± 31 ms -0.81 %
10000_production-mode + exec 2.21 s ± 117 ms 2.11 s ± 54 ms -4.91 %
10000_production-mode_persistent-cold + exec 2.33 s ± 50 ms 2.21 s ± 24 ms -5.12 %
10000_production-mode_persistent-hot + exec 1.64 s ± 26 ms 1.58 s ± 146 ms -3.90 %
arco-pro_development-mode + exec 1.73 s ± 128 ms 1.73 s ± 115 ms +0.38 %
arco-pro_development-mode_hmr + exec 375 ms ± 1.9 ms 374 ms ± 1.6 ms -0.27 %
arco-pro_production-mode + exec 3.54 s ± 113 ms 3.45 s ± 85 ms -2.55 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.58 s ± 191 ms 3.54 s ± 91 ms -1.31 %
arco-pro_production-mode_persistent-cold + exec 3.56 s ± 148 ms 3.55 s ± 35 ms -0.36 %
arco-pro_production-mode_persistent-hot + exec 2.25 s ± 115 ms 2.28 s ± 72 ms +1.24 %
arco-pro_production-mode_traverse-chunk-modules + exec 3.53 s ± 177 ms 3.48 s ± 86 ms -1.23 %
large-dyn-imports_development-mode + exec 1.95 s ± 58 ms 1.91 s ± 39 ms -2.38 %
large-dyn-imports_production-mode + exec 2 s ± 57 ms 1.96 s ± 28 ms -1.73 %
threejs_development-mode_10x + exec 1.43 s ± 35 ms 1.44 s ± 84 ms +0.56 %
threejs_development-mode_10x_hmr + exec 786 ms ± 10 ms 778 ms ± 10 ms -0.96 %
threejs_production-mode_10x + exec 5.03 s ± 77 ms 4.95 s ± 100 ms -1.65 %
threejs_production-mode_10x_persistent-cold + exec 5.13 s ± 357 ms 5.02 s ± 383 ms -2.14 %
threejs_production-mode_10x_persistent-hot + exec 4.47 s ± 335 ms 4.35 s ± 330 ms -2.63 %
10000_big_production-mode_disable-minimize + rss memory 8698 MiB ± 78.5 MiB 9496 MiB ± 429 MiB +9.17 %
10000_development-mode + rss memory 678 MiB ± 11.8 MiB 684 MiB ± 18.5 MiB +0.96 %
10000_development-mode_hmr + rss memory 1247 MiB ± 285 MiB 1310 MiB ± 482 MiB +5.04 %
10000_production-mode + rss memory 650 MiB ± 26.5 MiB 706 MiB ± 48.2 MiB +8.73 %
10000_production-mode_persistent-cold + rss memory 762 MiB ± 20 MiB 788 MiB ± 63.3 MiB +3.36 %
10000_production-mode_persistent-hot + rss memory 735 MiB ± 11.5 MiB 747 MiB ± 29 MiB +1.55 %
arco-pro_development-mode + rss memory 636 MiB ± 21.9 MiB 593 MiB ± 42.4 MiB -6.69 %
arco-pro_development-mode_hmr + rss memory 709 MiB ± 21.4 MiB 610 MiB ± 74.9 MiB -13.95 %
arco-pro_production-mode + rss memory 776 MiB ± 38.8 MiB 727 MiB ± 80.8 MiB -6.31 %
arco-pro_production-mode_generate-package-json-webpack-plugin + rss memory 792 MiB ± 25.6 MiB 738 MiB ± 75.6 MiB -6.83 %
arco-pro_production-mode_persistent-cold + rss memory 863 MiB ± 20.6 MiB 815 MiB ± 106 MiB -5.55 %
arco-pro_production-mode_persistent-hot + rss memory 832 MiB ± 32 MiB 778 MiB ± 63.6 MiB -6.43 %
arco-pro_production-mode_traverse-chunk-modules + rss memory 800 MiB ± 35.8 MiB 758 MiB ± 73.3 MiB -5.31 %
large-dyn-imports_development-mode + rss memory 697 MiB ± 17 MiB 671 MiB ± 11 MiB -3.82 %
large-dyn-imports_production-mode + rss memory 576 MiB ± 3.62 MiB 561 MiB ± 3.45 MiB -2.61 %
threejs_development-mode_10x + rss memory 651 MiB ± 59.5 MiB 667 MiB ± 16.7 MiB +2.55 %
threejs_development-mode_10x_hmr + rss memory 974 MiB ± 301 MiB 1023 MiB ± 116 MiB +5.04 %
threejs_production-mode_10x + rss memory 919 MiB ± 56.1 MiB 984 MiB ± 104 MiB +6.99 %
threejs_production-mode_10x_persistent-cold + rss memory 1050 MiB ± 71.5 MiB 1071 MiB ± 27.6 MiB +2.05 %
threejs_production-mode_10x_persistent-hot + rss memory 916 MiB ± 60.7 MiB 904 MiB ± 51.2 MiB -1.30 %

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented on c8bcb8f Mar 4, 2025

Choose a reason for hiding this comment

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

📝 Ecosystem CI detail: Open

suite result
modernjs ✅ success
rspress ✅ success
rslib ❌ failure
rsbuild ✅ success
rsdoctor ❌ failure
examples ✅ success
devserver ✅ success
nuxt ✅ success

Please sign in to comment.