Skip to content

Commit

Permalink
feat: add resolve.aliasStrategy and deprecate source.aliasStrategy (
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Dec 1, 2024
1 parent 8b341f2 commit 87e2003
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 22 deletions.
2 changes: 1 addition & 1 deletion e2e/cases/alias/jsconfig-paths/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ rspackOnlyTest(
alias: {
'@/common': './src/common2',
},
aliasStrategy: 'prefer-alias',
},
source: {
aliasStrategy: 'prefer-alias',
tsconfigPath: './jsconfig.json',
},
},
Expand Down
2 changes: 0 additions & 2 deletions e2e/cases/alias/tsconfig-paths/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ test('tsconfig paths should not work when aliasStrategy is "prefer-alias"', asyn
alias: {
'@/common': './src/common2',
},
},
source: {
aliasStrategy: 'prefer-alias',
},
},
Expand Down
4 changes: 3 additions & 1 deletion packages/compat/webpack/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ const getDefaultSourceConfig = (): NormalizedSourceConfig => {
'@swc/helpers': swcHelpersPath,
},
define: {},
aliasStrategy: 'prefer-tsconfig',
preEntry: [],
decorators: {
version: '2022-03',
Expand Down Expand Up @@ -195,6 +194,7 @@ const getDefaultOutputConfig = (): NormalizedOutputConfig => ({

const getDefaultResolveConfig = (): NormalizedResolveConfig => ({
alias: {},
aliasStrategy: 'prefer-tsconfig',
});

const createDefaultConfig = (): RsbuildConfig => ({
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 9 additions & 4 deletions packages/core/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ export interface SourceConfig {
*/
alias?: ConfigChain<Alias>;
/**
* 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;
/**
Expand Down Expand Up @@ -253,7 +253,6 @@ export interface NormalizedSourceConfig extends SourceConfig {
* `source.alias` will be removed in v2.0.0.
*/
alias: ConfigChain<Alias>;
aliasStrategy: AliasStrategy;
preEntry: string[];
decorators: Required<Decorators>;
}
Expand Down Expand Up @@ -1410,10 +1409,16 @@ export interface ResolveConfig {
* same as the [resolve.alias](https://rspack.dev/config/resolve) config of Rspack.
*/
alias?: ConfigChain<Alias>;
/**
* 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<Required<ResolveConfig>, 'alias'>;
Pick<Required<ResolveConfig>, 'alias' | 'aliasStrategy'>;

export type ModuleFederationConfig = {
options: ModuleFederationPluginOptions;
Expand Down
18 changes: 9 additions & 9 deletions packages/core/tests/__snapshots__/environments.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ exports[`environment config > should normalize environment config correctly 1`]
},
"resolve": {
"alias": {},
"aliasStrategy": "prefer-tsconfig",
},
"root": "<ROOT>",
"security": {
Expand All @@ -112,7 +113,6 @@ exports[`environment config > should normalize environment config correctly 1`]
"@common": "./src/common",
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
"aliasStrategy": "prefer-tsconfig",
"decorators": {
"version": "2022-03",
},
Expand Down Expand Up @@ -222,6 +222,7 @@ exports[`environment config > should normalize environment config correctly 2`]
},
"resolve": {
"alias": {},
"aliasStrategy": "prefer-tsconfig",
},
"root": "<ROOT>",
"security": {
Expand All @@ -245,7 +246,6 @@ exports[`environment config > should normalize environment config correctly 2`]
"@common": "./src/common",
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
"aliasStrategy": "prefer-tsconfig",
"decorators": {
"version": "2022-03",
},
Expand Down Expand Up @@ -355,6 +355,7 @@ exports[`environment config > should print environment config when inspect confi
},
"resolve": {
"alias": {},
"aliasStrategy": "prefer-tsconfig",
},
"root": "<ROOT>",
"security": {
Expand All @@ -378,7 +379,6 @@ exports[`environment config > should print environment config when inspect confi
"@common": "./src/common",
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
"aliasStrategy": "prefer-tsconfig",
"decorators": {
"version": "2022-03",
},
Expand Down Expand Up @@ -484,6 +484,7 @@ exports[`environment config > should print environment config when inspect confi
},
"resolve": {
"alias": {},
"aliasStrategy": "prefer-tsconfig",
},
"root": "<ROOT>",
"security": {
Expand All @@ -507,7 +508,6 @@ exports[`environment config > should print environment config when inspect confi
"@common": "./src/common",
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
"aliasStrategy": "prefer-tsconfig",
"decorators": {
"version": "2022-03",
},
Expand Down Expand Up @@ -633,6 +633,7 @@ exports[`environment config > should support modify environment config by api.mo
},
"resolve": {
"alias": {},
"aliasStrategy": "prefer-tsconfig",
},
"root": "<ROOT>",
"security": {
Expand All @@ -656,7 +657,6 @@ exports[`environment config > should support modify environment config by api.mo
"@common": "./src/common",
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
"aliasStrategy": "prefer-tsconfig",
"decorators": {
"version": "2022-03",
},
Expand Down Expand Up @@ -762,6 +762,7 @@ exports[`environment config > should support modify environment config by api.mo
},
"resolve": {
"alias": {},
"aliasStrategy": "prefer-tsconfig",
},
"root": "<ROOT>",
"security": {
Expand All @@ -786,7 +787,6 @@ exports[`environment config > should support modify environment config by api.mo
"@common1": "./src/common1",
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
"aliasStrategy": "prefer-tsconfig",
"decorators": {
"version": "2022-03",
},
Expand Down Expand Up @@ -892,6 +892,7 @@ exports[`environment config > should support modify environment config by api.mo
},
"resolve": {
"alias": {},
"aliasStrategy": "prefer-tsconfig",
},
"root": "<ROOT>",
"security": {
Expand All @@ -916,7 +917,6 @@ exports[`environment config > should support modify environment config by api.mo
"@common1": "./src/common1",
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
"aliasStrategy": "prefer-tsconfig",
"decorators": {
"version": "2022-03",
},
Expand Down Expand Up @@ -1025,6 +1025,7 @@ exports[`environment config > should support modify single environment config by
},
"resolve": {
"alias": {},
"aliasStrategy": "prefer-tsconfig",
},
"root": "<ROOT>",
"security": {
Expand All @@ -1048,7 +1049,6 @@ exports[`environment config > should support modify single environment config by
"@common": "./src/common",
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
"aliasStrategy": "prefer-tsconfig",
"decorators": {
"version": "2022-03",
},
Expand Down Expand Up @@ -1154,6 +1154,7 @@ exports[`environment config > should support modify single environment config by
},
"resolve": {
"alias": {},
"aliasStrategy": "prefer-tsconfig",
},
"root": "<ROOT>",
"security": {
Expand All @@ -1178,7 +1179,6 @@ exports[`environment config > should support modify single environment config by
"@common1": "./src/common1",
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
"aliasStrategy": "prefer-tsconfig",
"decorators": {
"version": "2022-03",
},
Expand Down
2 changes: 1 addition & 1 deletion packages/core/tests/resolve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('plugin-resolve', () => {
const rsbuild = await createStubRsbuild({
plugins: [pluginResolve()],
rsbuildConfig: {
source: {
resolve: {
aliasStrategy: 'prefer-alias',
},
},
Expand Down
2 changes: 1 addition & 1 deletion website/docs/en/config/source/alias-strategy.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion website/docs/zh/config/source/alias-strategy.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 87e2003

Please sign in to comment.