Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/enable-native-watcher.md
Original file line number Diff line number Diff line change
@@ -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` 关闭
16 changes: 16 additions & 0 deletions packages/cli/builder/src/plugins/nativeWatcher.ts
Original file line number Diff line number Diff line change
@@ -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;
});
},
});
2 changes: 2 additions & 0 deletions packages/cli/builder/src/shared/parseCommonConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -230,6 +231,7 @@ export async function parseCommonConfig(
}),
pluginEnvironmentDefaults(distPath),
pluginHtmlMinifierTerser(),
pluginNativeWatcher(),
];

if (checkSyntax) {
Expand Down
5 changes: 5 additions & 0 deletions packages/cli/builder/tests/__snapshots__/default.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ exports[`builder rspack > should generator rspack config correctly 1`] = `
],
},
"experiments": {
"nativeWatcher": true,
"sourceImport": true,
},
"infrastructureLogging": {
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -1524,6 +1526,7 @@ exports[`builder rspack > should generator rspack config correctly when node 1`]
],
},
"experiments": {
"nativeWatcher": true,
"sourceImport": true,
},
"infrastructureLogging": {
Expand Down Expand Up @@ -2486,6 +2489,7 @@ exports[`builder rspack > should generator rspack config correctly when prod 1`]
],
},
"experiments": {
"nativeWatcher": true,
"sourceImport": true,
},
"infrastructureLogging": {
Expand Down Expand Up @@ -3977,6 +3981,7 @@ exports[`builder rspack > should generator rspack config correctly when service-
],
},
"experiments": {
"nativeWatcher": true,
"sourceImport": true,
},
"infrastructureLogging": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ exports[`builder environment compat > should generator environment config correc
],
},
"experiments": {
"nativeWatcher": true,
"sourceImport": true,
},
"infrastructureLogging": {
Expand Down Expand Up @@ -1502,6 +1503,7 @@ exports[`builder environment compat > should generator environment config correc
],
},
"experiments": {
"nativeWatcher": true,
"sourceImport": true,
},
"infrastructureLogging": {
Expand Down Expand Up @@ -2440,6 +2442,7 @@ exports[`builder environment compat > should generator environment config correc
],
},
"experiments": {
"nativeWatcher": true,
"sourceImport": true,
},
"infrastructureLogging": {
Expand Down
58 changes: 58 additions & 0 deletions packages/cli/builder/tests/nativeWatcher.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
Loading