diff --git a/packages/compat/webpack/src/createCompiler.ts b/packages/compat/webpack/src/createCompiler.ts index 7e3fbe5280..1b971d3b0c 100644 --- a/packages/compat/webpack/src/createCompiler.ts +++ b/packages/compat/webpack/src/createCompiler.ts @@ -46,7 +46,7 @@ export async function createCompiler(options: InitConfigsOptions) { done(stats as Rspack.Stats); }); - if (context.command === 'dev') { + if (context.action === 'dev') { helpers.registerDevHook({ compiler, context, diff --git a/packages/core/src/cli/init.ts b/packages/core/src/cli/init.ts index 15aeb09f92..e10bc26c29 100644 --- a/packages/core/src/cli/init.ts +++ b/packages/core/src/cli/init.ts @@ -99,37 +99,39 @@ export async function init({ }); rsbuild.onBeforeCreateCompiler(() => { - const command = process.argv[2]; - if (command === 'dev' || isBuildWatch) { - const files: string[] = []; - const config = rsbuild.getNormalizedConfig(); - - if (config.dev?.watchFiles) { - for (const watchFilesConfig of castArray(config.dev.watchFiles)) { - if (watchFilesConfig.type !== 'reload-server') { - continue; - } - - const paths = castArray(watchFilesConfig.paths); - if (watchFilesConfig.options) { - watchFilesForRestart({ - files: paths, - rsbuild, - isBuildWatch, - watchOptions: watchFilesConfig.options, - }); - } else { - files.push(...paths); - } + // Skip watching files when not in dev mode or not in build watch mode + if (rsbuild.context.action !== 'dev' && !isBuildWatch) { + return; + } + + const files: string[] = []; + const config = rsbuild.getNormalizedConfig(); + + if (config.dev?.watchFiles) { + for (const watchFilesConfig of castArray(config.dev.watchFiles)) { + if (watchFilesConfig.type !== 'reload-server') { + continue; } - } - watchFilesForRestart({ - files, - rsbuild, - isBuildWatch, - }); + const paths = castArray(watchFilesConfig.paths); + if (watchFilesConfig.options) { + watchFilesForRestart({ + files: paths, + rsbuild, + isBuildWatch, + watchOptions: watchFilesConfig.options, + }); + } else { + files.push(...paths); + } + } } + + watchFilesForRestart({ + files, + rsbuild, + isBuildWatch, + }); }); return rsbuild; diff --git a/packages/core/src/createContext.ts b/packages/core/src/createContext.ts index 36248152e1..fb3500c0ed 100644 --- a/packages/core/src/createContext.ts +++ b/packages/core/src/createContext.ts @@ -150,6 +150,7 @@ export function createPublicContext( context: RsbuildContext, ): Readonly { const exposedKeys: Array = [ + 'action', 'version', 'rootPath', 'distPath', diff --git a/packages/core/src/createRsbuild.ts b/packages/core/src/createRsbuild.ts index c4cd959118..531e55a804 100644 --- a/packages/core/src/createRsbuild.ts +++ b/packages/core/src/createRsbuild.ts @@ -205,7 +205,7 @@ export async function createRsbuild( }); const preview = async (options: PreviewOptions = {}) => { - context.command = 'preview'; + context.action = 'preview'; if (!getNodeEnv()) { setNodeEnv('production'); @@ -237,7 +237,7 @@ export async function createRsbuild( }; const build: Build = async (...args) => { - context.command = 'build'; + context.action = 'build'; if (!getNodeEnv()) { setNodeEnv('production'); @@ -254,7 +254,7 @@ export async function createRsbuild( }; const startDevServer: StartDevServer = (...args) => { - context.command = 'dev'; + context.action = 'dev'; if (!getNodeEnv()) { setNodeEnv('development'); @@ -264,7 +264,7 @@ export async function createRsbuild( }; const createDevServer: CreateDevServer = (...args) => { - context.command = 'dev'; + context.action = 'dev'; if (!getNodeEnv()) { setNodeEnv('development'); @@ -274,8 +274,8 @@ export async function createRsbuild( }; const createCompiler: CreateCompiler = (...args) => { - if (!context.command) { - context.command = getNodeEnv() === 'development' ? 'dev' : 'build'; + if (!context.action) { + context.action = getNodeEnv() === 'development' ? 'dev' : 'build'; } return providerInstance.createCompiler(...args); }; diff --git a/packages/core/src/provider/createCompiler.ts b/packages/core/src/provider/createCompiler.ts index 8e73520271..11b627456c 100644 --- a/packages/core/src/provider/createCompiler.ts +++ b/packages/core/src/provider/createCompiler.ts @@ -123,7 +123,7 @@ export async function createCompiler(options: InitConfigsOptions): Promise<{ isCompiling = true; }); - if (context.command === 'build') { + if (context.action === 'build') { compiler.hooks.run.tap('rsbuild:run', logRspackVersion); } @@ -181,7 +181,7 @@ export async function createCompiler(options: InitConfigsOptions): Promise<{ }, ); - if (context.command === 'dev') { + if (context.action === 'dev') { registerDevHook({ context, compiler, diff --git a/packages/core/src/types/context.ts b/packages/core/src/types/context.ts index 7c6ec05536..a6e295c483 100644 --- a/packages/core/src/types/context.ts +++ b/packages/core/src/types/context.ts @@ -21,6 +21,16 @@ export type RsbuildContext = { port: number; https: boolean; }; + /** + * The current action type. + * - dev: will be set when running `rsbuild dev` or `rsbuild.startDevServer()` + * - build: will be set when running `rsbuild build` or `rsbuild.build()` + * - preview: will be set when running `rsbuild preview` or `rsbuild.preview()` + */ + action?: 'dev' | 'build' | 'preview'; + /** + * The bundler type, can be `rspack` or `webpack`. + */ bundlerType: BundlerType; }; @@ -44,12 +54,4 @@ export type InternalContext = RsbuildContext & { environments: Record; /** Only build specified environment. */ specifiedEnvironments?: string[]; - /** - * The command type. - * - * - dev: `rsbuild dev` or `rsbuild.startDevServer()` - * - build: `rsbuild build` or `rsbuild.build()` - * - preview: `rsbuild preview` or `rsbuild.preview()` - */ - command?: 'dev' | 'build' | 'preview'; }; diff --git a/website/docs/en/api/javascript-api/instance.mdx b/website/docs/en/api/javascript-api/instance.mdx index 1b63227f53..46736764f4 100644 --- a/website/docs/en/api/javascript-api/instance.mdx +++ b/website/docs/en/api/javascript-api/instance.mdx @@ -63,6 +63,30 @@ type DevServer = { }; ``` +### context.action + +The current action type. + +- **Type:** + +```ts +type Action = 'dev' | 'build' | 'preview' | undefined; +``` + +`context.action` is set when running CLI commands or calling Rsbuild instance methods: + +- `dev`: will be set when running [rsbuild dev](/guide/basic/cli#rsbuild-dev) or [rsbuild.startDevServer()](/api/javascript-api/instance#rsbuildstartdevserver) +- `build`: will be set when running [rsbuild build](/guide/basic/cli#rsbuild-build) or [rsbuild.build()](/api/javascript-api/instance#rsbuildbuild) +- `preview`: will be set when running [rsbuild preview](/guide/basic/cli#rsbuild-preview) or [rsbuild.preview()](/api/javascript-api/instance#rsbuildpreview) + +For example: + +```ts +if (rsbuild.context.action === 'dev') { + // do something +} +``` + ### context.bundlerType The bundler type of current build. diff --git a/website/docs/zh/api/javascript-api/instance.mdx b/website/docs/zh/api/javascript-api/instance.mdx index 0cb152e397..7298237131 100644 --- a/website/docs/zh/api/javascript-api/instance.mdx +++ b/website/docs/zh/api/javascript-api/instance.mdx @@ -63,6 +63,30 @@ type DevServer = { }; ``` +### context.action + +当前的动作类型。 + +- **类型:** + +```ts +type Action = 'dev' | 'build' | 'preview' | undefined; +``` + +`context.action` 在运行 CLI 命令或调用 Rsbuild 实例方法时设置: + +- `dev`: 当运行 [rsbuild dev](/guide/basic/cli#rsbuild-dev) 或 [rsbuild.startDevServer()](/api/javascript-api/instance#rsbuildstartdevserver) 时设置。 +- `build`: 当运行 [rsbuild build](/guide/basic/cli#rsbuild-build) 或 [rsbuild.build()](/api/javascript-api/instance#rsbuildbuild) 时设置。 +- `preview`: 当运行 [rsbuild preview](/guide/basic/cli#rsbuild-preview) 或 [rsbuild.preview()](/api/javascript-api/instance#rsbuildpreview) 时设置。 + +示例: + +```ts +if (rsbuild.context.action === 'dev') { + // do something +} +``` + ### context.bundlerType 当前执行构建的构建工具类型。