From 87e200315fe01dfaa2a141b581f0236bd1d88064 Mon Sep 17 00:00:00 2001 From: neverland Date: Sun, 1 Dec 2024 22:00:02 +0800 Subject: [PATCH] feat: add `resolve.aliasStrategy` and deprecate `source.aliasStrategy` (#4101) --- e2e/cases/alias/jsconfig-paths/index.test.ts | 2 +- e2e/cases/alias/tsconfig-paths/index.test.ts | 2 -- packages/compat/webpack/src/plugin.ts | 4 +++- packages/core/src/config.ts | 2 +- packages/core/src/plugins/resolve.ts | 5 ++++- packages/core/src/types/config.ts | 13 +++++++++---- .../__snapshots__/environments.test.ts.snap | 18 +++++++++--------- packages/core/tests/resolve.test.ts | 2 +- .../docs/en/config/source/alias-strategy.mdx | 2 +- .../docs/zh/config/source/alias-strategy.mdx | 2 +- 10 files changed, 30 insertions(+), 22 deletions(-) diff --git a/e2e/cases/alias/jsconfig-paths/index.test.ts b/e2e/cases/alias/jsconfig-paths/index.test.ts index ad9205befc..ea65d47d58 100644 --- a/e2e/cases/alias/jsconfig-paths/index.test.ts +++ b/e2e/cases/alias/jsconfig-paths/index.test.ts @@ -37,9 +37,9 @@ rspackOnlyTest( alias: { '@/common': './src/common2', }, + aliasStrategy: 'prefer-alias', }, source: { - aliasStrategy: 'prefer-alias', tsconfigPath: './jsconfig.json', }, }, diff --git a/e2e/cases/alias/tsconfig-paths/index.test.ts b/e2e/cases/alias/tsconfig-paths/index.test.ts index 72c811cc9e..81fa0f6d05 100644 --- a/e2e/cases/alias/tsconfig-paths/index.test.ts +++ b/e2e/cases/alias/tsconfig-paths/index.test.ts @@ -33,8 +33,6 @@ test('tsconfig paths should not work when aliasStrategy is "prefer-alias"', asyn alias: { '@/common': './src/common2', }, - }, - source: { aliasStrategy: 'prefer-alias', }, }, diff --git a/packages/compat/webpack/src/plugin.ts b/packages/compat/webpack/src/plugin.ts index 53ea50affd..c4789474b1 100644 --- a/packages/compat/webpack/src/plugin.ts +++ b/packages/compat/webpack/src/plugin.ts @@ -66,8 +66,10 @@ export const pluginAdaptor = ( setup(api) { api.modifyBundlerChain(async (chain, { CHAIN_ID, environment, target }) => { const { config, tsconfigPath } = environment; + const aliasStrategy = + config.source.aliasStrategy ?? config.resolve.aliasStrategy; - if (tsconfigPath && config.source.aliasStrategy === 'prefer-tsconfig') { + if (tsconfigPath && aliasStrategy === 'prefer-tsconfig') { await applyTsConfigPathsPlugin({ chain, CHAIN_ID, diff --git a/packages/core/src/config.ts b/packages/core/src/config.ts index 9f98c8ec1b..60724b1b2c 100644 --- a/packages/core/src/config.ts +++ b/packages/core/src/config.ts @@ -99,7 +99,6 @@ const getDefaultSourceConfig = (): NormalizedSourceConfig => { '@swc/helpers': swcHelpersPath, }, define: {}, - aliasStrategy: 'prefer-tsconfig', preEntry: [], decorators: { version: '2022-03', @@ -195,6 +194,7 @@ const getDefaultOutputConfig = (): NormalizedOutputConfig => ({ const getDefaultResolveConfig = (): NormalizedResolveConfig => ({ alias: {}, + aliasStrategy: 'prefer-tsconfig', }); const createDefaultConfig = (): RsbuildConfig => ({ diff --git a/packages/core/src/plugins/resolve.ts b/packages/core/src/plugins/resolve.ts index e3abc04812..cddfdab1fc 100644 --- a/packages/core/src/plugins/resolve.ts +++ b/packages/core/src/plugins/resolve.ts @@ -175,11 +175,14 @@ export const pluginResolve = (): RsbuildPlugin => ({ // In some cases (modern.js), there is an error if the fullySpecified rule is after the js rule applyFullySpecified({ chain, config, CHAIN_ID }); + const aliasStrategy = + config.source.aliasStrategy ?? config.resolve.aliasStrategy; + if ( tsconfigPath && // Only Rspack has the tsConfig option api.context.bundlerType === 'rspack' && - config.source.aliasStrategy === 'prefer-tsconfig' + aliasStrategy === 'prefer-tsconfig' ) { chain.resolve.tsConfig({ configFile: tsconfigPath, diff --git a/packages/core/src/types/config.ts b/packages/core/src/types/config.ts index 75c99f1cc3..926d96dfc4 100644 --- a/packages/core/src/types/config.ts +++ b/packages/core/src/types/config.ts @@ -179,8 +179,8 @@ export interface SourceConfig { */ alias?: ConfigChain; /** - * Used to control the priority between the `paths` option in `tsconfig.json` - * and the `alias` option in the bundler. + * @deprecated Use `resolve.aliasStrategy` instead. + * `source.aliasStrategy` will be removed in v2.0.0. */ aliasStrategy?: AliasStrategy; /** @@ -253,7 +253,6 @@ export interface NormalizedSourceConfig extends SourceConfig { * `source.alias` will be removed in v2.0.0. */ alias: ConfigChain; - aliasStrategy: AliasStrategy; preEntry: string[]; decorators: Required; } @@ -1410,10 +1409,16 @@ export interface ResolveConfig { * same as the [resolve.alias](https://rspack.dev/config/resolve) config of Rspack. */ alias?: ConfigChain; + /** + * Control the priority between the `paths` option in `tsconfig.json` + * and the `resolve.alias` option of Rsbuild. + * @default 'prefer-tsconfig' + */ + aliasStrategy?: AliasStrategy; } export type NormalizedResolveConfig = ResolveConfig & - Pick, 'alias'>; + Pick, 'alias' | 'aliasStrategy'>; export type ModuleFederationConfig = { options: ModuleFederationPluginOptions; diff --git a/packages/core/tests/__snapshots__/environments.test.ts.snap b/packages/core/tests/__snapshots__/environments.test.ts.snap index d1730f4bf2..32aee30b70 100644 --- a/packages/core/tests/__snapshots__/environments.test.ts.snap +++ b/packages/core/tests/__snapshots__/environments.test.ts.snap @@ -89,6 +89,7 @@ exports[`environment config > should normalize environment config correctly 1`] }, "resolve": { "alias": {}, + "aliasStrategy": "prefer-tsconfig", }, "root": "", "security": { @@ -112,7 +113,6 @@ exports[`environment config > should normalize environment config correctly 1`] "@common": "./src/common", "@swc/helpers": "/node_modules//@swc/helpers", }, - "aliasStrategy": "prefer-tsconfig", "decorators": { "version": "2022-03", }, @@ -222,6 +222,7 @@ exports[`environment config > should normalize environment config correctly 2`] }, "resolve": { "alias": {}, + "aliasStrategy": "prefer-tsconfig", }, "root": "", "security": { @@ -245,7 +246,6 @@ exports[`environment config > should normalize environment config correctly 2`] "@common": "./src/common", "@swc/helpers": "/node_modules//@swc/helpers", }, - "aliasStrategy": "prefer-tsconfig", "decorators": { "version": "2022-03", }, @@ -355,6 +355,7 @@ exports[`environment config > should print environment config when inspect confi }, "resolve": { "alias": {}, + "aliasStrategy": "prefer-tsconfig", }, "root": "", "security": { @@ -378,7 +379,6 @@ exports[`environment config > should print environment config when inspect confi "@common": "./src/common", "@swc/helpers": "/node_modules//@swc/helpers", }, - "aliasStrategy": "prefer-tsconfig", "decorators": { "version": "2022-03", }, @@ -484,6 +484,7 @@ exports[`environment config > should print environment config when inspect confi }, "resolve": { "alias": {}, + "aliasStrategy": "prefer-tsconfig", }, "root": "", "security": { @@ -507,7 +508,6 @@ exports[`environment config > should print environment config when inspect confi "@common": "./src/common", "@swc/helpers": "/node_modules//@swc/helpers", }, - "aliasStrategy": "prefer-tsconfig", "decorators": { "version": "2022-03", }, @@ -633,6 +633,7 @@ exports[`environment config > should support modify environment config by api.mo }, "resolve": { "alias": {}, + "aliasStrategy": "prefer-tsconfig", }, "root": "", "security": { @@ -656,7 +657,6 @@ exports[`environment config > should support modify environment config by api.mo "@common": "./src/common", "@swc/helpers": "/node_modules//@swc/helpers", }, - "aliasStrategy": "prefer-tsconfig", "decorators": { "version": "2022-03", }, @@ -762,6 +762,7 @@ exports[`environment config > should support modify environment config by api.mo }, "resolve": { "alias": {}, + "aliasStrategy": "prefer-tsconfig", }, "root": "", "security": { @@ -786,7 +787,6 @@ exports[`environment config > should support modify environment config by api.mo "@common1": "./src/common1", "@swc/helpers": "/node_modules//@swc/helpers", }, - "aliasStrategy": "prefer-tsconfig", "decorators": { "version": "2022-03", }, @@ -892,6 +892,7 @@ exports[`environment config > should support modify environment config by api.mo }, "resolve": { "alias": {}, + "aliasStrategy": "prefer-tsconfig", }, "root": "", "security": { @@ -916,7 +917,6 @@ exports[`environment config > should support modify environment config by api.mo "@common1": "./src/common1", "@swc/helpers": "/node_modules//@swc/helpers", }, - "aliasStrategy": "prefer-tsconfig", "decorators": { "version": "2022-03", }, @@ -1025,6 +1025,7 @@ exports[`environment config > should support modify single environment config by }, "resolve": { "alias": {}, + "aliasStrategy": "prefer-tsconfig", }, "root": "", "security": { @@ -1048,7 +1049,6 @@ exports[`environment config > should support modify single environment config by "@common": "./src/common", "@swc/helpers": "/node_modules//@swc/helpers", }, - "aliasStrategy": "prefer-tsconfig", "decorators": { "version": "2022-03", }, @@ -1154,6 +1154,7 @@ exports[`environment config > should support modify single environment config by }, "resolve": { "alias": {}, + "aliasStrategy": "prefer-tsconfig", }, "root": "", "security": { @@ -1178,7 +1179,6 @@ exports[`environment config > should support modify single environment config by "@common1": "./src/common1", "@swc/helpers": "/node_modules//@swc/helpers", }, - "aliasStrategy": "prefer-tsconfig", "decorators": { "version": "2022-03", }, diff --git a/packages/core/tests/resolve.test.ts b/packages/core/tests/resolve.test.ts index c1c5e5a61b..abc4f2b7f5 100644 --- a/packages/core/tests/resolve.test.ts +++ b/packages/core/tests/resolve.test.ts @@ -27,7 +27,7 @@ describe('plugin-resolve', () => { const rsbuild = await createStubRsbuild({ plugins: [pluginResolve()], rsbuildConfig: { - source: { + resolve: { aliasStrategy: 'prefer-alias', }, }, diff --git a/website/docs/en/config/source/alias-strategy.mdx b/website/docs/en/config/source/alias-strategy.mdx index 69bff99123..e4f7fa5c55 100644 --- a/website/docs/en/config/source/alias-strategy.mdx +++ b/website/docs/en/config/source/alias-strategy.mdx @@ -3,7 +3,7 @@ - **Type:** `'prefer-tsconfig' | 'prefer-alias'` - **Default:** `'prefer-tsconfig'` -`source.aliasStrategy` is used to control the priority between the `paths` option in `tsconfig.json` and the `alias` option in the bundler. +Control the priority between the `paths` option in `tsconfig.json` and the [resolve.alias](/config/source/alias) option of Rsbuild. ### prefer-tsconfig diff --git a/website/docs/zh/config/source/alias-strategy.mdx b/website/docs/zh/config/source/alias-strategy.mdx index bbc6eeded8..fd7bd1576f 100644 --- a/website/docs/zh/config/source/alias-strategy.mdx +++ b/website/docs/zh/config/source/alias-strategy.mdx @@ -3,7 +3,7 @@ - **类型:** `'prefer-tsconfig' | 'prefer-alias'` - **默认值:** `'prefer-tsconfig'` -`source.aliasStrategy` 用于控制 `tsconfig.json` 中的 `paths` 选项与打包工具的 `alias` 选项的优先级。 +控制 `tsconfig.json` 中的 `paths` 选项与 Rsbuild 的 [resolve.alias](/config/source/alias) 选项的优先级。 ### prefer-tsconfig