From 0aee4792b2c27f0e6356b4c2f5d8b39d317daf86 Mon Sep 17 00:00:00 2001 From: kongjiacong Date: Tue, 1 Jul 2025 17:28:00 +0800 Subject: [PATCH 1/6] fix: preserve the default behavior of compilation scope to the current directory when using webpack --- packages/cli/uni-builder/src/webpack/index.ts | 3 +++ .../src/webpack/plugins/include.ts | 23 +++++++++++++++++++ .../runtime/plugin-router-v7/src/cli/index.ts | 12 ++++++---- 3 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 packages/cli/uni-builder/src/webpack/plugins/include.ts diff --git a/packages/cli/uni-builder/src/webpack/index.ts b/packages/cli/uni-builder/src/webpack/index.ts index d5084d876473..b7b7639549fa 100644 --- a/packages/cli/uni-builder/src/webpack/index.ts +++ b/packages/cli/uni-builder/src/webpack/index.ts @@ -17,6 +17,7 @@ import type { UniBuilderConfig, } from '../types'; import { pluginBabel } from './plugins/babel'; +import { pluginInclude } from './plugins/include'; import { pluginModuleScopes } from './plugins/moduleScopes'; import { pluginReact } from './plugins/react'; @@ -158,6 +159,8 @@ export async function parseConfig( pluginModuleScopes(uniBuilderConfig.source?.moduleScopes), ); + rsbuildPlugins.push(pluginInclude()); + return { rsbuildConfig, rsbuildPlugins, diff --git a/packages/cli/uni-builder/src/webpack/plugins/include.ts b/packages/cli/uni-builder/src/webpack/plugins/include.ts new file mode 100644 index 000000000000..cafad6e21936 --- /dev/null +++ b/packages/cli/uni-builder/src/webpack/plugins/include.ts @@ -0,0 +1,23 @@ +import type { RsbuildPlugin } from '@rsbuild/core'; + +export const pluginInclude = (): RsbuildPlugin => ({ + name: 'uni-builder:babel-include', + + setup(api) { + api.modifyWebpackChain((chain, { CHAIN_ID }) => { + const includes = chain.module.rule(CHAIN_ID.RULE.JS).include.values(); + includes.forEach(include => { + if ( + typeof include === 'object' && + !Array.isArray(include) && + !(include instanceof RegExp) && + include.not && + include.not.toString() === /[\\/]node_modules[\\/]/.toString() + ) { + include.and = [api.context.rootPath, { not: include.not }]; + delete include.not; + } + }); + }); + }, +}); diff --git a/packages/runtime/plugin-router-v7/src/cli/index.ts b/packages/runtime/plugin-router-v7/src/cli/index.ts index 1943ea6a78b7..3c81ad256712 100644 --- a/packages/runtime/plugin-router-v7/src/cli/index.ts +++ b/packages/runtime/plugin-router-v7/src/cli/index.ts @@ -1,3 +1,4 @@ +import { sep } from 'path'; import type { AppTools, CliPluginFuture } from '@modern-js/app-tools'; import { logger } from '@modern-js/utils'; @@ -17,18 +18,19 @@ export const routerPlugin = (): CliPluginFuture => ({ } }); } + + const cjsRegex = new RegExp(`${sep}cjs${sep}`); + const esm = `${sep}esm${sep}`; return { resolve: { alias: { - 'react-router-dom$': require - .resolve('../runtime') - .replace(/\/cjs\//, '/esm/'), + 'react-router-dom$': require.resolve('../runtime'), '@remix-run/router': require .resolve('../runtime') - .replace(/\/cjs\//, '/esm/'), + .replace(cjsRegex, esm), 'react-router-dom/server': require .resolve('../runtime') - .replace(/\/cjs\//, '/esm/'), + .replace(cjsRegex, esm), }, }, source: { From f23ae5f46880793de962e164e229f1d463dae20d Mon Sep 17 00:00:00 2001 From: kongjiacong Date: Tue, 1 Jul 2025 17:36:14 +0800 Subject: [PATCH 2/6] chore: add todo comment --- packages/cli/uni-builder/src/webpack/plugins/include.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/cli/uni-builder/src/webpack/plugins/include.ts b/packages/cli/uni-builder/src/webpack/plugins/include.ts index cafad6e21936..f6c1e5db1051 100644 --- a/packages/cli/uni-builder/src/webpack/plugins/include.ts +++ b/packages/cli/uni-builder/src/webpack/plugins/include.ts @@ -1,5 +1,7 @@ import type { RsbuildPlugin } from '@rsbuild/core'; +// Preserve the default behavior of compilation scope to the current directory when using webpack. +// TODO: Remove this plugin in next major version. export const pluginInclude = (): RsbuildPlugin => ({ name: 'uni-builder:babel-include', From be32fd2356432c90a30b6227ac0d4f7e81b224e5 Mon Sep 17 00:00:00 2001 From: kongjiacong Date: Tue, 1 Jul 2025 17:43:39 +0800 Subject: [PATCH 3/6] chore: update snapshot --- .../src/webpack/plugins/include.ts | 5 +- .../tests/__snapshots__/babel.test.ts.snap | 98 ++++++++++++++++--- .../tests/__snapshots__/default.test.ts.snap | 14 ++- .../tests/__snapshots__/react.test.ts.snap | 7 +- .../styledComponents.test.ts.snap | 28 +++++- .../transformImport.test.ts.snap | 28 +++++- .../tests/__snapshots__/tsLoader.test.ts.snap | 7 +- 7 files changed, 160 insertions(+), 27 deletions(-) diff --git a/packages/cli/uni-builder/src/webpack/plugins/include.ts b/packages/cli/uni-builder/src/webpack/plugins/include.ts index f6c1e5db1051..376e475bff18 100644 --- a/packages/cli/uni-builder/src/webpack/plugins/include.ts +++ b/packages/cli/uni-builder/src/webpack/plugins/include.ts @@ -16,7 +16,10 @@ export const pluginInclude = (): RsbuildPlugin => ({ include.not && include.not.toString() === /[\\/]node_modules[\\/]/.toString() ) { - include.and = [api.context.rootPath, { not: include.not }]; + include.and = [ + api.context.rootPath || process.cwd(), + { not: include.not }, + ]; delete include.not; } }); diff --git a/packages/cli/uni-builder/tests/__snapshots__/babel.test.ts.snap b/packages/cli/uni-builder/tests/__snapshots__/babel.test.ts.snap index fd1122dd8097..27c540c07a3c 100644 --- a/packages/cli/uni-builder/tests/__snapshots__/babel.test.ts.snap +++ b/packages/cli/uni-builder/tests/__snapshots__/babel.test.ts.snap @@ -800,7 +800,12 @@ exports[`plugin-babel > should add core-js-entry when output.polyfill is entry 1 }, "include": [ { - "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + "and": [ + "", + { + "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + }, + ], }, /\\\\\\.\\(\\?:ts\\|tsx\\|jsx\\|mts\\|cts\\)\\$/, ], @@ -948,7 +953,12 @@ exports[`plugin-babel > should adjust browserslist when target is node 1`] = ` }, "include": [ { - "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + "and": [ + "", + { + "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + }, + ], }, /\\\\\\.\\(\\?:ts\\|tsx\\|jsx\\|mts\\|cts\\)\\$/, ], @@ -1078,7 +1088,12 @@ exports[`plugin-babel > should apply exclude condition when using source.exclude ], "include": [ { - "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + "and": [ + "", + { + "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + }, + ], }, /\\\\\\.\\(\\?:ts\\|tsx\\|jsx\\|mts\\|cts\\)\\$/, ], @@ -1226,7 +1241,12 @@ exports[`plugin-babel > should merge environment and shared babel config 1`] = ` }, "include": [ { - "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + "and": [ + "", + { + "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + }, + ], }, /\\\\\\.\\(\\?:ts\\|tsx\\|jsx\\|mts\\|cts\\)\\$/, ], @@ -1390,7 +1410,12 @@ exports[`plugin-babel > should merge environment and shared babel config 2`] = ` }, "include": [ { - "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + "and": [ + "", + { + "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + }, + ], }, /\\\\\\.\\(\\?:ts\\|tsx\\|jsx\\|mts\\|cts\\)\\$/, ], @@ -1546,7 +1571,12 @@ exports[`plugin-babel > should not add core-js-entry when output.polyfill is usa }, "include": [ { - "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + "and": [ + "", + { + "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + }, + ], }, /\\\\\\.\\(\\?:ts\\|tsx\\|jsx\\|mts\\|cts\\)\\$/, ], @@ -1688,7 +1718,12 @@ exports[`plugin-babel > should not have any pluginImport in Babel 1`] = ` }, "include": [ { - "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + "and": [ + "", + { + "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + }, + ], }, /\\\\\\.\\(\\?:ts\\|tsx\\|jsx\\|mts\\|cts\\)\\$/, ], @@ -1908,7 +1943,12 @@ exports[`plugin-babel > should not set default pluginImport for Babel 1`] = ` }, "include": [ { - "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + "and": [ + "", + { + "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + }, + ], }, /\\\\\\.\\(\\?:ts\\|tsx\\|jsx\\|mts\\|cts\\)\\$/, ], @@ -2172,7 +2212,12 @@ exports[`plugin-babel > should override targets of babel-preset-env when using o }, "include": [ { - "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + "and": [ + "", + { + "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + }, + ], }, /\\\\\\.\\(\\?:ts\\|tsx\\|jsx\\|mts\\|cts\\)\\$/, ], @@ -2318,7 +2363,12 @@ exports[`plugin-babel > should set babel-loader 1`] = ` }, "include": [ { - "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + "and": [ + "", + { + "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + }, + ], }, /\\\\\\.\\(\\?:ts\\|tsx\\|jsx\\|mts\\|cts\\)\\$/, ], @@ -2466,7 +2516,12 @@ exports[`plugin-babel > should set babel-loader separate by environment 1`] = ` }, "include": [ { - "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + "and": [ + "", + { + "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + }, + ], }, /\\\\\\.\\(\\?:ts\\|tsx\\|jsx\\|mts\\|cts\\)\\$/, ], @@ -2622,7 +2677,12 @@ exports[`plugin-babel > should set babel-loader separate by environment 2`] = ` }, "include": [ { - "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + "and": [ + "", + { + "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + }, + ], }, /\\\\\\.\\(\\?:ts\\|tsx\\|jsx\\|mts\\|cts\\)\\$/, ], @@ -2773,7 +2833,12 @@ exports[`plugin-babel > should set include/exclude 1`] = ` ], "include": [ { - "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + "and": [ + "", + { + "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + }, + ], }, /\\\\\\.\\(\\?:ts\\|tsx\\|jsx\\|mts\\|cts\\)\\$/, "src/**/*.ts", @@ -2916,7 +2981,12 @@ exports[`plugin-babel > should set proper pluginImport option in Babel 1`] = ` }, "include": [ { - "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + "and": [ + "", + { + "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + }, + ], }, /\\\\\\.\\(\\?:ts\\|tsx\\|jsx\\|mts\\|cts\\)\\$/, ], diff --git a/packages/cli/uni-builder/tests/__snapshots__/default.test.ts.snap b/packages/cli/uni-builder/tests/__snapshots__/default.test.ts.snap index 8c716c06af50..69781c533b61 100644 --- a/packages/cli/uni-builder/tests/__snapshots__/default.test.ts.snap +++ b/packages/cli/uni-builder/tests/__snapshots__/default.test.ts.snap @@ -4852,7 +4852,12 @@ exports[`uni-builder webpack > should generator webpack config correctly 1`] = ` }, "include": [ { - "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + "and": [ + "", + { + "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + }, + ], }, /\\\\\\.\\(\\?:ts\\|tsx\\|jsx\\|mts\\|cts\\)\\$/, /\\[\\\\\\\\/\\]@rsbuild\\[\\\\\\\\/\\]core\\[\\\\\\\\/\\]dist\\[\\\\\\\\/\\]/, @@ -6418,7 +6423,12 @@ exports[`uni-builder webpack > should generator webpack config correctly when pr }, "include": [ { - "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + "and": [ + "", + { + "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + }, + ], }, /\\\\\\.\\(\\?:ts\\|tsx\\|jsx\\|mts\\|cts\\)\\$/, ], diff --git a/packages/cli/uni-builder/tests/__snapshots__/react.test.ts.snap b/packages/cli/uni-builder/tests/__snapshots__/react.test.ts.snap index ba5a8b4716a5..760c64b41e30 100644 --- a/packages/cli/uni-builder/tests/__snapshots__/react.test.ts.snap +++ b/packages/cli/uni-builder/tests/__snapshots__/react.test.ts.snap @@ -14,7 +14,12 @@ exports[`plugins/react > should work with babel-loader 1`] = ` }, "include": [ { - "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + "and": [ + "", + { + "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + }, + ], }, /\\\\\\.\\(\\?:ts\\|tsx\\|jsx\\|mts\\|cts\\)\\$/, ], diff --git a/packages/cli/uni-builder/tests/__snapshots__/styledComponents.test.ts.snap b/packages/cli/uni-builder/tests/__snapshots__/styledComponents.test.ts.snap index 1e81e6c0bcf7..2ae2a3b71879 100644 --- a/packages/cli/uni-builder/tests/__snapshots__/styledComponents.test.ts.snap +++ b/packages/cli/uni-builder/tests/__snapshots__/styledComponents.test.ts.snap @@ -14,7 +14,12 @@ exports[`plugins/styled-components > should enable ssr when target contain node }, "include": [ { - "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + "and": [ + "", + { + "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + }, + ], }, /\\\\\\.\\(\\?:ts\\|tsx\\|jsx\\|mts\\|cts\\)\\$/, ], @@ -162,7 +167,12 @@ exports[`plugins/styled-components > should enable ssr when target contain node }, "include": [ { - "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + "and": [ + "", + { + "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + }, + ], }, /\\\\\\.\\(\\?:ts\\|tsx\\|jsx\\|mts\\|cts\\)\\$/, ], @@ -289,7 +299,12 @@ exports[`plugins/styled-components > should works in webpack babel mode 1`] = ` }, "include": [ { - "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + "and": [ + "", + { + "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + }, + ], }, /\\\\\\.\\(\\?:ts\\|tsx\\|jsx\\|mts\\|cts\\)\\$/, ], @@ -437,7 +452,12 @@ exports[`plugins/styled-components > should works in webpack swc mode 1`] = ` }, "include": [ { - "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + "and": [ + "", + { + "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + }, + ], }, /\\\\\\.\\(\\?:ts\\|tsx\\|jsx\\|mts\\|cts\\)\\$/, ], diff --git a/packages/cli/uni-builder/tests/__snapshots__/transformImport.test.ts.snap b/packages/cli/uni-builder/tests/__snapshots__/transformImport.test.ts.snap index f18ef28c5f44..7a1dd892054e 100644 --- a/packages/cli/uni-builder/tests/__snapshots__/transformImport.test.ts.snap +++ b/packages/cli/uni-builder/tests/__snapshots__/transformImport.test.ts.snap @@ -506,7 +506,12 @@ exports[`uni-builder webpack > should allow custom arco import config 1`] = ` }, "include": [ { - "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + "and": [ + "", + { + "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + }, + ], }, /\\\\\\.\\(\\?:ts\\|tsx\\|jsx\\|mts\\|cts\\)\\$/, /\\[\\\\\\\\/\\]@rsbuild\\[\\\\\\\\/\\]core\\[\\\\\\\\/\\]dist\\[\\\\\\\\/\\]/, @@ -655,7 +660,12 @@ exports[`uni-builder webpack > should apply arco correctly 1`] = ` }, "include": [ { - "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + "and": [ + "", + { + "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + }, + ], }, /\\\\\\.\\(\\?:ts\\|tsx\\|jsx\\|mts\\|cts\\)\\$/, /\\[\\\\\\\\/\\]@rsbuild\\[\\\\\\\\/\\]core\\[\\\\\\\\/\\]dist\\[\\\\\\\\/\\]/, @@ -804,7 +814,12 @@ exports[`uni-builder webpack > should not apply arco when transformImport false }, "include": [ { - "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + "and": [ + "", + { + "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + }, + ], }, /\\\\\\.\\(\\?:ts\\|tsx\\|jsx\\|mts\\|cts\\)\\$/, /\\[\\\\\\\\/\\]@rsbuild\\[\\\\\\\\/\\]core\\[\\\\\\\\/\\]dist\\[\\\\\\\\/\\]/, @@ -934,7 +949,12 @@ exports[`uni-builder webpack > should not apply arco when transformImport return }, "include": [ { - "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + "and": [ + "", + { + "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + }, + ], }, /\\\\\\.\\(\\?:ts\\|tsx\\|jsx\\|mts\\|cts\\)\\$/, /\\[\\\\\\\\/\\]@rsbuild\\[\\\\\\\\/\\]core\\[\\\\\\\\/\\]dist\\[\\\\\\\\/\\]/, diff --git a/packages/cli/uni-builder/tests/__snapshots__/tsLoader.test.ts.snap b/packages/cli/uni-builder/tests/__snapshots__/tsLoader.test.ts.snap index 53ff9d98ad0a..3c84bfba2141 100644 --- a/packages/cli/uni-builder/tests/__snapshots__/tsLoader.test.ts.snap +++ b/packages/cli/uni-builder/tests/__snapshots__/tsLoader.test.ts.snap @@ -14,7 +14,12 @@ exports[`plugin-ts-loader > should insert babel plugin correctly in some edge ca }, "include": [ { - "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + "and": [ + "", + { + "not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + }, + ], }, /\\\\\\.\\(\\?:ts\\|tsx\\|jsx\\|mts\\|cts\\)\\$/, ], From 873331efe79b0d9b3842a94c1ab48429eeefcb5b Mon Sep 17 00:00:00 2001 From: kongjiacong Date: Tue, 1 Jul 2025 17:49:49 +0800 Subject: [PATCH 4/6] chore: cr --- packages/runtime/plugin-router-v7/src/cli/index.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/runtime/plugin-router-v7/src/cli/index.ts b/packages/runtime/plugin-router-v7/src/cli/index.ts index 3c81ad256712..a20cd118d6e7 100644 --- a/packages/runtime/plugin-router-v7/src/cli/index.ts +++ b/packages/runtime/plugin-router-v7/src/cli/index.ts @@ -24,7 +24,9 @@ export const routerPlugin = (): CliPluginFuture => ({ return { resolve: { alias: { - 'react-router-dom$': require.resolve('../runtime'), + 'react-router-dom$': require + .resolve('../runtime') + .replace(cjsRegex, esm), '@remix-run/router': require .resolve('../runtime') .replace(cjsRegex, esm), From 1a6e60f2356e21a9869dfc23300629b097dac058 Mon Sep 17 00:00:00 2001 From: kongjiacong Date: Tue, 1 Jul 2025 22:03:28 +0800 Subject: [PATCH 5/6] chore: add docs for source.include --- .../docs/en/configure/app/source/include.mdx | 41 +++++++++++++++---- .../docs/zh/configure/app/source/include.mdx | 39 +++++++++++++++--- .../runtime/plugin-router-v7/src/cli/index.ts | 15 +++---- 3 files changed, 73 insertions(+), 22 deletions(-) diff --git a/packages/document/main-doc/docs/en/configure/app/source/include.mdx b/packages/document/main-doc/docs/en/configure/app/source/include.mdx index 808bc634dfa5..9fdb53b89fb5 100644 --- a/packages/document/main-doc/docs/en/configure/app/source/include.mdx +++ b/packages/document/main-doc/docs/en/configure/app/source/include.mdx @@ -5,21 +5,48 @@ title: include # source.include - **Type:** [RuleSetCondition[]](https://rspack.rs/config/module#condition) -- **Default:** +- **Default value:** ```ts const defaultInclude = [ [ - /node_modules\/react-router/, - /node_modules\/react-router-dom/, - /node_modules\/@remix-run\/router/, - '/node_modules/.modern-js', + { not: /[\\/]node_modules[\\/]/ }, + /\.(?:ts|tsx|jsx|mts|cts)$/, ], ]; ``` -The `source.include` is used to specify additional JavaScript files that need to be compiled. +`source.include` is used to specify additional JavaScript files that need to be compiled. By default, the following files will be compiled: -import RsbuildConfig from '@site-docs-en/components/rsbuild-config-tooltip'; +- TypeScript and JSX files in any directory, with file extensions matching `.ts`, `.tsx`, `.jsx`, `.mts`, `.cts`. +- JavaScript files not in the `node_modules` directory, with file extensions matching `.js`, `.mjs`, `.cjs`. + +:::tip +Before Rsbuild version 1.4, the default value of `source.include` was: + +```ts +const defaultInclude = [ + [ + { + and: [ + APP_ROOT, + { not: /[\\/]node_modules[\\/]/ } + ] + }, + /\.(?:ts|tsx|jsx|mts|cts)$/, + ], +]; +``` + +The difference from the new version is that `.js`, `.mjs`, `.cjs` files only in the current directory will be compiled. +::: + +## Rspack + +import RsbuildConfig from '@site-docs/components/rsbuild-config-tooltip'; + +## Webpack + +To ensure legacy projects can run properly, when using Webpack, the default value of `source.include` is the same as before Rsbuild version 1.4. diff --git a/packages/document/main-doc/docs/zh/configure/app/source/include.mdx b/packages/document/main-doc/docs/zh/configure/app/source/include.mdx index 0a123272222f..08c5217ea83b 100644 --- a/packages/document/main-doc/docs/zh/configure/app/source/include.mdx +++ b/packages/document/main-doc/docs/zh/configure/app/source/include.mdx @@ -10,16 +10,45 @@ title: include ```ts const defaultInclude = [ [ - /node_modules\/react-router/, - /node_modules\/react-router-dom/, - /node_modules\/@remix-run\/router/, - '/node_modules/.modern-js', + { not: /[\\/]node_modules[\\/]/ }, + /\.(?:ts|tsx|jsx|mts|cts)$/, ], ]; ``` -`source.include` 用于指定额外需要编译的 JavaScript 文件。 +`source.include` 用于指定额外需要编译的 JavaScript 文件。默认情况下,会编译以下文件: + +- 任意目录下的 TypeScript 和 JSX 文件,匹配的文件后缀为 `.ts`、`.tsx`、`.jsx`、`.mts`、`.cts`。 +- 非 `node_modules` 目录下的 JavaScript 文件,匹配的文件后缀为 `.js`、`.mjs`、`.cjs`。 + +:::tip +在 Rsbuild 1.4 版本前,`source.include` 的默认值为: + +```ts +const defaultInclude = [ + [ + { + and: [ + APP_ROOT, + { not: /[\\/]node_modules[\\/]/ } + ] + }, + /\.(?:ts|tsx|jsx|mts|cts)$/, + ], +]; +``` + +与新版本的差异是,非当前项目目录的 `.js`、`.mjs`、`.cjs` 文件不会被编译。 +::: + +## Rspack import RsbuildConfig from '@site-docs/components/rsbuild-config-tooltip'; + +## Webpack + +为了保证历史项目可以正常运行,Webpack 编译时,`source.include` 与 Rsbuild 1.4 版本前的默认值一致。 + + diff --git a/packages/runtime/plugin-router-v7/src/cli/index.ts b/packages/runtime/plugin-router-v7/src/cli/index.ts index a20cd118d6e7..caef398705cb 100644 --- a/packages/runtime/plugin-router-v7/src/cli/index.ts +++ b/packages/runtime/plugin-router-v7/src/cli/index.ts @@ -19,20 +19,15 @@ export const routerPlugin = (): CliPluginFuture => ({ }); } - const cjsRegex = new RegExp(`${sep}cjs${sep}`); + const cjs = `${sep}cjs${sep}`; const esm = `${sep}esm${sep}`; + const runtimeAlias = require.resolve('../runtime').replace(cjs, esm); return { resolve: { alias: { - 'react-router-dom$': require - .resolve('../runtime') - .replace(cjsRegex, esm), - '@remix-run/router': require - .resolve('../runtime') - .replace(cjsRegex, esm), - 'react-router-dom/server': require - .resolve('../runtime') - .replace(cjsRegex, esm), + 'react-router-dom$': runtimeAlias, + '@remix-run/router': runtimeAlias, + 'react-router-dom/server': runtimeAlias, }, }, source: { From c4c2cec4cfaf44f4fefa9ccb6fa5b3bec848626b Mon Sep 17 00:00:00 2001 From: kongjiacong Date: Tue, 1 Jul 2025 22:39:33 +0800 Subject: [PATCH 6/6] docs: add changeset --- .changeset/thin-results-try.md | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 .changeset/thin-results-try.md diff --git a/.changeset/thin-results-try.md b/.changeset/thin-results-try.md new file mode 100644 index 000000000000..16931b78a8df --- /dev/null +++ b/.changeset/thin-results-try.md @@ -0,0 +1,8 @@ +--- +'@modern-js/plugin-router-v7': patch +'@modern-js/main-doc': patch +'@modern-js/uni-builder': patch +--- + +fix: keep the default behavior of scope for compiling JS when use webpack after upgrade to Rsbuild 1.4 +fix: 升级到 Rsbuild 1.4 后,如果使用 Webpack 构建,保持默认的 JS 编译范围