diff --git a/.changeset/enable-native-watcher.md b/.changeset/enable-native-watcher.md new file mode 100644 index 000000000000..2807a8aa7db2 --- /dev/null +++ b/.changeset/enable-native-watcher.md @@ -0,0 +1,7 @@ +--- +'@modern-js/builder': patch +--- + +feat(builder): enable Rspack `experiments.nativeWatcher` by default, and allow opting out via `tools.rspack` + +feat(builder): 默认开启 Rspack 的 `experiments.nativeWatcher`,可通过 `tools.rspack` 关闭 diff --git a/packages/cli/builder/src/plugins/nativeWatcher.ts b/packages/cli/builder/src/plugins/nativeWatcher.ts new file mode 100644 index 000000000000..b526b0a292ca --- /dev/null +++ b/packages/cli/builder/src/plugins/nativeWatcher.ts @@ -0,0 +1,16 @@ +import type { RsbuildPlugin } from '@rsbuild/core'; + +/** + * Enable Rspack's Rust native watcher by default. + * Users can opt out via `tools.rspack`, which is applied after this hook. + */ +export const pluginNativeWatcher = (): RsbuildPlugin => ({ + name: 'builder:native-watcher', + + setup(api) { + api.modifyRspackConfig(config => { + config.experiments ??= {}; + config.experiments.nativeWatcher ??= true; + }); + }, +}); diff --git a/packages/cli/builder/src/shared/parseCommonConfig.ts b/packages/cli/builder/src/shared/parseCommonConfig.ts index e5c5059cb5de..613a02634761 100644 --- a/packages/cli/builder/src/shared/parseCommonConfig.ts +++ b/packages/cli/builder/src/shared/parseCommonConfig.ts @@ -14,6 +14,7 @@ import { pluginEmitRouteFile } from '../plugins/emitRouteFile'; import { pluginEnvironmentDefaults } from '../plugins/environmentDefaults'; import { pluginGlobalVars } from '../plugins/globalVars'; import { pluginHtmlMinifierTerser } from '../plugins/htmlMinify'; +import { pluginNativeWatcher } from '../plugins/nativeWatcher'; import { pluginRuntimeChunk } from '../plugins/runtimeChunk'; import type { BuilderConfig, CreateBuilderCommonOptions } from '../types'; import { transformToRsbuildServerOptions } from './devServer'; @@ -230,6 +231,7 @@ export async function parseCommonConfig( }), pluginEnvironmentDefaults(distPath), pluginHtmlMinifierTerser(), + pluginNativeWatcher(), ]; if (checkSyntax) { diff --git a/packages/cli/builder/tests/__snapshots__/default.test.ts.snap b/packages/cli/builder/tests/__snapshots__/default.test.ts.snap index c88947cc2b7a..2780973dc9c5 100644 --- a/packages/cli/builder/tests/__snapshots__/default.test.ts.snap +++ b/packages/cli/builder/tests/__snapshots__/default.test.ts.snap @@ -23,6 +23,7 @@ exports[`builder rspack > should generator rspack config correctly 1`] = ` ], }, "experiments": { + "nativeWatcher": true, "sourceImport": true, }, "infrastructureLogging": { @@ -1491,6 +1492,7 @@ exports[`builder rspack > should generator rspack config correctly 2`] = ` "rsbuild:less", "builder:environment-defaults-plugin", "builder:plugin-html-minifier-terser", + "builder:native-watcher", "rsbuild:type-check", "builder:runtime-chunk", "rsbuild:react", @@ -1524,6 +1526,7 @@ exports[`builder rspack > should generator rspack config correctly when node 1`] ], }, "experiments": { + "nativeWatcher": true, "sourceImport": true, }, "infrastructureLogging": { @@ -2486,6 +2489,7 @@ exports[`builder rspack > should generator rspack config correctly when prod 1`] ], }, "experiments": { + "nativeWatcher": true, "sourceImport": true, }, "infrastructureLogging": { @@ -3977,6 +3981,7 @@ exports[`builder rspack > should generator rspack config correctly when service- ], }, "experiments": { + "nativeWatcher": true, "sourceImport": true, }, "infrastructureLogging": { diff --git a/packages/cli/builder/tests/__snapshots__/environment.test.ts.snap b/packages/cli/builder/tests/__snapshots__/environment.test.ts.snap index 1ea7e57688a2..01af8d715582 100644 --- a/packages/cli/builder/tests/__snapshots__/environment.test.ts.snap +++ b/packages/cli/builder/tests/__snapshots__/environment.test.ts.snap @@ -24,6 +24,7 @@ exports[`builder environment compat > should generator environment config correc ], }, "experiments": { + "nativeWatcher": true, "sourceImport": true, }, "infrastructureLogging": { @@ -1502,6 +1503,7 @@ exports[`builder environment compat > should generator environment config correc ], }, "experiments": { + "nativeWatcher": true, "sourceImport": true, }, "infrastructureLogging": { @@ -2440,6 +2442,7 @@ exports[`builder environment compat > should generator environment config correc ], }, "experiments": { + "nativeWatcher": true, "sourceImport": true, }, "infrastructureLogging": { diff --git a/packages/cli/builder/tests/nativeWatcher.test.ts b/packages/cli/builder/tests/nativeWatcher.test.ts new file mode 100644 index 000000000000..c85dd1a79402 --- /dev/null +++ b/packages/cli/builder/tests/nativeWatcher.test.ts @@ -0,0 +1,58 @@ +import { describe, expect, it } from '@rstest/core'; +import { createBuilder } from '../src'; +import { unwrapConfig } from './helper'; + +describe('experiments.nativeWatcher', () => { + it('should be enabled by default', async () => { + const rsbuild = await createBuilder({ + cwd: '', + bundlerType: 'rspack', + config: {}, + }); + + const config = await unwrapConfig(rsbuild); + + expect(config.experiments?.nativeWatcher).toBe(true); + }); + + it('should be overridable by tools.rspack', async () => { + const rsbuild = await createBuilder({ + cwd: '', + bundlerType: 'rspack', + config: { + tools: { + rspack: { + experiments: { + nativeWatcher: false, + }, + }, + }, + }, + }); + + const config = await unwrapConfig(rsbuild); + + expect(config.experiments?.nativeWatcher).toBe(false); + }); + + it('should be overridable by tools.bundlerChain', async () => { + const rsbuild = await createBuilder({ + cwd: '', + bundlerType: 'rspack', + config: { + tools: { + bundlerChain: chain => { + chain.experiments({ + ...chain.get('experiments'), + nativeWatcher: false, + }); + }, + }, + }, + }); + + const config = await unwrapConfig(rsbuild); + + expect(config.experiments?.nativeWatcher).toBe(false); + }); +});