Skip to content

Commit

Permalink
fix!(cli): rename env option of inspect command to mode
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan committed Aug 6, 2024
1 parent d96bf9b commit 02e8c5d
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 12 deletions.
34 changes: 32 additions & 2 deletions e2e/cases/cli/inspect/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@ import { execSync } from 'node:child_process';
import path from 'node:path';
import { globContentJSON } from '@e2e/helper';
import { expect, test } from '@playwright/test';
import { removeSync } from 'fs-extra';

test('should run inspect command correctly', async () => {
const clean = () => {
removeSync(path.join(__dirname, 'dist'));
delete process.env.NODE_ENV;
};

test('should run inspect command correctly', async () => {
clean();

execSync('npx rsbuild inspect', {
cwd: __dirname,
});
Expand All @@ -26,8 +33,31 @@ test('should run inspect command correctly', async () => {
expect(files[rspackConfig!]).toContain("mode: 'development'");
});

test('should run inspect command with mode option correctly', async () => {
clean();

execSync('npx rsbuild inspect --mode production', {
cwd: __dirname,
});

const files = await globContentJSON(path.join(__dirname, 'dist'));
const fileNames = Object.keys(files);

const rsbuildConfig = fileNames.find((item) =>
item.includes('rsbuild.config.mjs'),
);
expect(rsbuildConfig).toBeTruthy();

const rspackConfig = fileNames.find((item) =>
item.includes('rspack.config.web.mjs'),
);
expect(rspackConfig).toBeTruthy();
expect(files[rspackConfig!]).toContain("mode: 'production'");
});

test('should run inspect command with output option correctly', async () => {
delete process.env.NODE_ENV;
clean();

execSync('npx rsbuild inspect --output foo', {
cwd: __dirname,
});
Expand Down
4 changes: 2 additions & 2 deletions packages/compat/webpack/src/inspectConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ export async function inspectConfig({
inspectOptions?: InspectConfigOptions;
bundlerConfigs?: WebpackConfig[];
}): Promise<InspectConfigResult<'webpack'>> {
if (inspectOptions.env) {
process.env.NODE_ENV = inspectOptions.env;
if (inspectOptions.mode) {
process.env.NODE_ENV = inspectOptions.mode;
} else if (!process.env.NODE_ENV) {
process.env.NODE_ENV = 'development';
}
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/cli/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export type BuildOptions = CommonOptions & {
};

export type InspectOptions = CommonOptions & {
env: RsbuildMode;
mode: RsbuildMode;
output: string;
verbose?: boolean;
};
Expand Down Expand Up @@ -135,14 +135,14 @@ export function runCli(): void {

inspectCommand
.description('inspect the Rspack and Rsbuild configs')
.option('--env <env>', 'specify env mode', 'development')
.option('--mode <mode>', 'specify the mode for Rsbuild', 'development')
.option('--output <output>', 'specify inspect content output path')
.option('--verbose', 'show full function definitions in output')
.action(async (options: InspectOptions) => {
try {
const rsbuild = await init({ cliOptions: options });
await rsbuild?.inspectConfig({
env: options.env,
mode: options.mode,
verbose: options.verbose,
outputPath: options.output,
writeToDisk: true,
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/provider/inspectConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export async function inspectConfig({
inspectOptions?: InspectConfigOptions;
bundlerConfigs?: RspackConfig[];
}): Promise<InspectConfigResult<'rspack'>> {
if (inspectOptions.env) {
setNodeEnv(inspectOptions.env);
if (inspectOptions.mode) {
setNodeEnv(inspectOptions.mode);
} else if (!getNodeEnv()) {
setNodeEnv('development');
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/types/rsbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export type BuildOptions = {
};

export type InspectConfigOptions = {
env?: RsbuildMode;
mode?: RsbuildMode;
verbose?: boolean;
outputPath?: string;
writeToDisk?: boolean;
Expand Down
2 changes: 1 addition & 1 deletion website/docs/en/api/javascript-api/instance.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ If you need to view the Rsbuild and Rspack configurations during the build proce
type InspectConfigOptions = {
// View the config in the specified environment
// defaults to "development", can be set to "production"
env?: RsbuildMode;
mode?: RsbuildMode;
// Whether to enable verbose mode, display the complete content of the function in the config
// defaults to `false`
verbose?: boolean;
Expand Down
2 changes: 1 addition & 1 deletion website/docs/zh/api/javascript-api/instance.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ inspectConfig 方法通常用于调试 Rsbuild 内部的配置,它会返回 Rs
type InspectConfigOptions = {
// 查看指定环境下的配置
// 默认为 "development",可以设置为 "production"
env?: RsbuildMode;
mode?: RsbuildMode;
// 是否开启冗余模式,展示配置中函数的完整内容
// 默认为 `false`
verbose?: boolean;
Expand Down

0 comments on commit 02e8c5d

Please sign in to comment.