Skip to content

Commit 87e2003

Browse files
authored
feat: add resolve.aliasStrategy and deprecate source.aliasStrategy (#4101)
1 parent 8b341f2 commit 87e2003

File tree

10 files changed

+30
-22
lines changed

10 files changed

+30
-22
lines changed

e2e/cases/alias/jsconfig-paths/index.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ rspackOnlyTest(
3737
alias: {
3838
'@/common': './src/common2',
3939
},
40+
aliasStrategy: 'prefer-alias',
4041
},
4142
source: {
42-
aliasStrategy: 'prefer-alias',
4343
tsconfigPath: './jsconfig.json',
4444
},
4545
},

e2e/cases/alias/tsconfig-paths/index.test.ts

-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ test('tsconfig paths should not work when aliasStrategy is "prefer-alias"', asyn
3333
alias: {
3434
'@/common': './src/common2',
3535
},
36-
},
37-
source: {
3836
aliasStrategy: 'prefer-alias',
3937
},
4038
},

packages/compat/webpack/src/plugin.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,10 @@ export const pluginAdaptor = (
6666
setup(api) {
6767
api.modifyBundlerChain(async (chain, { CHAIN_ID, environment, target }) => {
6868
const { config, tsconfigPath } = environment;
69+
const aliasStrategy =
70+
config.source.aliasStrategy ?? config.resolve.aliasStrategy;
6971

70-
if (tsconfigPath && config.source.aliasStrategy === 'prefer-tsconfig') {
72+
if (tsconfigPath && aliasStrategy === 'prefer-tsconfig') {
7173
await applyTsConfigPathsPlugin({
7274
chain,
7375
CHAIN_ID,

packages/core/src/config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ const getDefaultSourceConfig = (): NormalizedSourceConfig => {
9999
'@swc/helpers': swcHelpersPath,
100100
},
101101
define: {},
102-
aliasStrategy: 'prefer-tsconfig',
103102
preEntry: [],
104103
decorators: {
105104
version: '2022-03',
@@ -195,6 +194,7 @@ const getDefaultOutputConfig = (): NormalizedOutputConfig => ({
195194

196195
const getDefaultResolveConfig = (): NormalizedResolveConfig => ({
197196
alias: {},
197+
aliasStrategy: 'prefer-tsconfig',
198198
});
199199

200200
const createDefaultConfig = (): RsbuildConfig => ({

packages/core/src/plugins/resolve.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,14 @@ export const pluginResolve = (): RsbuildPlugin => ({
175175
// In some cases (modern.js), there is an error if the fullySpecified rule is after the js rule
176176
applyFullySpecified({ chain, config, CHAIN_ID });
177177

178+
const aliasStrategy =
179+
config.source.aliasStrategy ?? config.resolve.aliasStrategy;
180+
178181
if (
179182
tsconfigPath &&
180183
// Only Rspack has the tsConfig option
181184
api.context.bundlerType === 'rspack' &&
182-
config.source.aliasStrategy === 'prefer-tsconfig'
185+
aliasStrategy === 'prefer-tsconfig'
183186
) {
184187
chain.resolve.tsConfig({
185188
configFile: tsconfigPath,

packages/core/src/types/config.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@ export interface SourceConfig {
179179
*/
180180
alias?: ConfigChain<Alias>;
181181
/**
182-
* Used to control the priority between the `paths` option in `tsconfig.json`
183-
* and the `alias` option in the bundler.
182+
* @deprecated Use `resolve.aliasStrategy` instead.
183+
* `source.aliasStrategy` will be removed in v2.0.0.
184184
*/
185185
aliasStrategy?: AliasStrategy;
186186
/**
@@ -253,7 +253,6 @@ export interface NormalizedSourceConfig extends SourceConfig {
253253
* `source.alias` will be removed in v2.0.0.
254254
*/
255255
alias: ConfigChain<Alias>;
256-
aliasStrategy: AliasStrategy;
257256
preEntry: string[];
258257
decorators: Required<Decorators>;
259258
}
@@ -1410,10 +1409,16 @@ export interface ResolveConfig {
14101409
* same as the [resolve.alias](https://rspack.dev/config/resolve) config of Rspack.
14111410
*/
14121411
alias?: ConfigChain<Alias>;
1412+
/**
1413+
* Control the priority between the `paths` option in `tsconfig.json`
1414+
* and the `resolve.alias` option of Rsbuild.
1415+
* @default 'prefer-tsconfig'
1416+
*/
1417+
aliasStrategy?: AliasStrategy;
14131418
}
14141419

14151420
export type NormalizedResolveConfig = ResolveConfig &
1416-
Pick<Required<ResolveConfig>, 'alias'>;
1421+
Pick<Required<ResolveConfig>, 'alias' | 'aliasStrategy'>;
14171422

14181423
export type ModuleFederationConfig = {
14191424
options: ModuleFederationPluginOptions;

packages/core/tests/__snapshots__/environments.test.ts.snap

+9-9
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ exports[`environment config > should normalize environment config correctly 1`]
8989
},
9090
"resolve": {
9191
"alias": {},
92+
"aliasStrategy": "prefer-tsconfig",
9293
},
9394
"root": "<ROOT>",
9495
"security": {
@@ -112,7 +113,6 @@ exports[`environment config > should normalize environment config correctly 1`]
112113
"@common": "./src/common",
113114
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
114115
},
115-
"aliasStrategy": "prefer-tsconfig",
116116
"decorators": {
117117
"version": "2022-03",
118118
},
@@ -222,6 +222,7 @@ exports[`environment config > should normalize environment config correctly 2`]
222222
},
223223
"resolve": {
224224
"alias": {},
225+
"aliasStrategy": "prefer-tsconfig",
225226
},
226227
"root": "<ROOT>",
227228
"security": {
@@ -245,7 +246,6 @@ exports[`environment config > should normalize environment config correctly 2`]
245246
"@common": "./src/common",
246247
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
247248
},
248-
"aliasStrategy": "prefer-tsconfig",
249249
"decorators": {
250250
"version": "2022-03",
251251
},
@@ -355,6 +355,7 @@ exports[`environment config > should print environment config when inspect confi
355355
},
356356
"resolve": {
357357
"alias": {},
358+
"aliasStrategy": "prefer-tsconfig",
358359
},
359360
"root": "<ROOT>",
360361
"security": {
@@ -378,7 +379,6 @@ exports[`environment config > should print environment config when inspect confi
378379
"@common": "./src/common",
379380
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
380381
},
381-
"aliasStrategy": "prefer-tsconfig",
382382
"decorators": {
383383
"version": "2022-03",
384384
},
@@ -484,6 +484,7 @@ exports[`environment config > should print environment config when inspect confi
484484
},
485485
"resolve": {
486486
"alias": {},
487+
"aliasStrategy": "prefer-tsconfig",
487488
},
488489
"root": "<ROOT>",
489490
"security": {
@@ -507,7 +508,6 @@ exports[`environment config > should print environment config when inspect confi
507508
"@common": "./src/common",
508509
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
509510
},
510-
"aliasStrategy": "prefer-tsconfig",
511511
"decorators": {
512512
"version": "2022-03",
513513
},
@@ -633,6 +633,7 @@ exports[`environment config > should support modify environment config by api.mo
633633
},
634634
"resolve": {
635635
"alias": {},
636+
"aliasStrategy": "prefer-tsconfig",
636637
},
637638
"root": "<ROOT>",
638639
"security": {
@@ -656,7 +657,6 @@ exports[`environment config > should support modify environment config by api.mo
656657
"@common": "./src/common",
657658
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
658659
},
659-
"aliasStrategy": "prefer-tsconfig",
660660
"decorators": {
661661
"version": "2022-03",
662662
},
@@ -762,6 +762,7 @@ exports[`environment config > should support modify environment config by api.mo
762762
},
763763
"resolve": {
764764
"alias": {},
765+
"aliasStrategy": "prefer-tsconfig",
765766
},
766767
"root": "<ROOT>",
767768
"security": {
@@ -786,7 +787,6 @@ exports[`environment config > should support modify environment config by api.mo
786787
"@common1": "./src/common1",
787788
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
788789
},
789-
"aliasStrategy": "prefer-tsconfig",
790790
"decorators": {
791791
"version": "2022-03",
792792
},
@@ -892,6 +892,7 @@ exports[`environment config > should support modify environment config by api.mo
892892
},
893893
"resolve": {
894894
"alias": {},
895+
"aliasStrategy": "prefer-tsconfig",
895896
},
896897
"root": "<ROOT>",
897898
"security": {
@@ -916,7 +917,6 @@ exports[`environment config > should support modify environment config by api.mo
916917
"@common1": "./src/common1",
917918
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
918919
},
919-
"aliasStrategy": "prefer-tsconfig",
920920
"decorators": {
921921
"version": "2022-03",
922922
},
@@ -1025,6 +1025,7 @@ exports[`environment config > should support modify single environment config by
10251025
},
10261026
"resolve": {
10271027
"alias": {},
1028+
"aliasStrategy": "prefer-tsconfig",
10281029
},
10291030
"root": "<ROOT>",
10301031
"security": {
@@ -1048,7 +1049,6 @@ exports[`environment config > should support modify single environment config by
10481049
"@common": "./src/common",
10491050
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
10501051
},
1051-
"aliasStrategy": "prefer-tsconfig",
10521052
"decorators": {
10531053
"version": "2022-03",
10541054
},
@@ -1154,6 +1154,7 @@ exports[`environment config > should support modify single environment config by
11541154
},
11551155
"resolve": {
11561156
"alias": {},
1157+
"aliasStrategy": "prefer-tsconfig",
11571158
},
11581159
"root": "<ROOT>",
11591160
"security": {
@@ -1178,7 +1179,6 @@ exports[`environment config > should support modify single environment config by
11781179
"@common1": "./src/common1",
11791180
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
11801181
},
1181-
"aliasStrategy": "prefer-tsconfig",
11821182
"decorators": {
11831183
"version": "2022-03",
11841184
},

packages/core/tests/resolve.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe('plugin-resolve', () => {
2727
const rsbuild = await createStubRsbuild({
2828
plugins: [pluginResolve()],
2929
rsbuildConfig: {
30-
source: {
30+
resolve: {
3131
aliasStrategy: 'prefer-alias',
3232
},
3333
},

website/docs/en/config/source/alias-strategy.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
- **Type:** `'prefer-tsconfig' | 'prefer-alias'`
44
- **Default:** `'prefer-tsconfig'`
55

6-
`source.aliasStrategy` is used to control the priority between the `paths` option in `tsconfig.json` and the `alias` option in the bundler.
6+
Control the priority between the `paths` option in `tsconfig.json` and the [resolve.alias](/config/source/alias) option of Rsbuild.
77

88
### prefer-tsconfig
99

website/docs/zh/config/source/alias-strategy.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
- **类型:** `'prefer-tsconfig' | 'prefer-alias'`
44
- **默认值:** `'prefer-tsconfig'`
55

6-
`source.aliasStrategy` 用于控制 `tsconfig.json` 中的 `paths` 选项与打包工具的 `alias` 选项的优先级。
6+
控制 `tsconfig.json` 中的 `paths` 选项与 Rsbuild 的 [resolve.alias](/config/source/alias) 选项的优先级。
77

88
### prefer-tsconfig
99

0 commit comments

Comments
 (0)