-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into fix/runtime-types
- Loading branch information
Showing
21 changed files
with
319 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
'@modern-js/builder-webpack-provider': minor | ||
'@modern-js/builder-rspack-provider': minor | ||
'@modern-js/builder-shared': minor | ||
--- | ||
|
||
feat(builder): add `source.aliasStrategy` option | ||
|
||
feat(builder): 新增 `source.aliasStrategy` 选项 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
packages/document/builder-doc/docs/en/config/source/aliasStrategy.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
- **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. | ||
|
||
### prefer-tsconfig | ||
|
||
By default, `source.aliasStrategy` is set to `'prefer-tsconfig'`. In this case, both the `paths` option in `tsconfig.json` and the `alias` option in the bundler will take effect, but the `paths` option in tsconfig has a higher priority. | ||
|
||
For example, if the following configurations are set at the same time: | ||
|
||
- tsconfig paths: | ||
|
||
```json title="tsconfig.json" | ||
{ | ||
"compilerOptions": { | ||
"paths": { | ||
"@common/*": ["./src/common-1/*"] | ||
} | ||
} | ||
} | ||
``` | ||
|
||
- `source.alias`: | ||
|
||
```ts | ||
export default { | ||
source: { | ||
alias: { | ||
'@common': './src/common-2', | ||
'@utils': './src/utils', | ||
}, | ||
}, | ||
}; | ||
``` | ||
|
||
Since the tsconfig paths have a higher priority, the following will happen: | ||
|
||
- `@common` will use the value defined in tsconfig paths, pointing to `./src/common-1` | ||
- `@utils` will use the value defined in `source.alias`, pointing to `./src/utils` | ||
|
||
### prefer-alias | ||
|
||
If the value of `source.aliasStrategy` is set to `prefer-alias`, the `paths` option in `tsconfig.json` will only be used to provide TypeScript type definitions and will not affect the bundling result. In this case, the bundler will only read the `alias` option as the path alias. | ||
|
||
```ts | ||
export default { | ||
source: { | ||
aliasStrategy: 'prefer-alias', | ||
}, | ||
}; | ||
``` | ||
|
||
For example, if the following configurations are set at the same time: | ||
|
||
- tsconfig paths: | ||
|
||
```json title="tsconfig.json" | ||
{ | ||
"compilerOptions": { | ||
"paths": { | ||
"@common/*": ["./src/common-1/*"], | ||
"@utils/*": ["./src/utils/*"] | ||
} | ||
} | ||
} | ||
``` | ||
|
||
- `source.alias`: | ||
|
||
```ts | ||
export default { | ||
source: { | ||
alias: { | ||
'@common': './src/common-2', | ||
}, | ||
}, | ||
}; | ||
``` | ||
|
||
Since the tsconfig paths are only used to provide types, only the `@common` alias will be effective, pointing to the `./src/common-2` directory. | ||
|
||
In most cases, you don't need to use `prefer-alias`, but you can consider using it if you need to dynamically generate some alias configurations. For example, generating the `alias` option based on environment variables: | ||
|
||
```ts | ||
export default { | ||
source: { | ||
alias: { | ||
'@common': | ||
process.env.NODE_ENV === 'production' | ||
? './src/common-prod' | ||
: './src/common-dev', | ||
}, | ||
}, | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
packages/document/builder-doc/docs/zh/config/source/aliasStrategy.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
- **类型:** `'prefer-tsconfig' | 'prefer-alias'` | ||
- **默认值:** `'prefer-tsconfig'` | ||
|
||
`source.aliasStrategy` 用于控制 `tsconfig.json` 中的 `paths` 选项与打包工具的 `alias` 选项的优先级。 | ||
|
||
### prefer-tsconfig | ||
|
||
`source.aliasStrategy` 默认为 `'prefer-tsconfig'`,此时 `tsconfig.json` 中的 `paths` 选项和打包工具的 `alias` 选项都会生效,但 tsconfig paths 选项的优先级更高。 | ||
|
||
比如同时配置以下内容: | ||
|
||
- tsconfig paths: | ||
|
||
```json title="tsconfig.json" | ||
{ | ||
"compilerOptions": { | ||
"paths": { | ||
"@common/*": ["./src/common-1/*"] | ||
} | ||
} | ||
} | ||
``` | ||
|
||
- `source.alias`: | ||
|
||
```ts | ||
export default { | ||
source: { | ||
alias: { | ||
'@common': './src/common-2', | ||
'@utils': './src/utils', | ||
}, | ||
}, | ||
}; | ||
``` | ||
|
||
由于 tsconfig paths 的优先级更高,所以: | ||
|
||
- `@common` 会使用 tsconfig paths 定义的值,指向 `./src/common-1` | ||
- `@utils` 会使用 `source.alias` 定义的值,指向 `./src/utils` | ||
|
||
### prefer-alias | ||
|
||
当 `source.aliasStrategy` 的值为 `prefer-alias` 时,`tsconfig.json` 中的 `paths` 选项只用于提供 TypeScript 类型定义,而不会对打包结果产生任何影响。此时,构建工具只会读取 `alias` 选项作为路径别名。 | ||
|
||
```ts | ||
export default { | ||
source: { | ||
aliasStrategy: 'prefer-alias', | ||
}, | ||
}; | ||
``` | ||
|
||
比如同时配置以下内容: | ||
|
||
- tsconfig paths: | ||
|
||
```json title="tsconfig.json" | ||
{ | ||
"compilerOptions": { | ||
"paths": { | ||
"@common/*": ["./src/common-1/*"], | ||
"@utils/*": ["./src/utils/*"] | ||
} | ||
} | ||
} | ||
``` | ||
|
||
- `source.alias`: | ||
|
||
```ts | ||
export default { | ||
source: { | ||
alias: { | ||
'@common': './src/common-2', | ||
}, | ||
}, | ||
}; | ||
``` | ||
|
||
由于 tsconfig paths 只用于提供类型,所以最终只有 `@common` 别名生效,并指向 `./src/common-2` 目录。 | ||
|
||
大部分情况下你不需要使用 `prefer-alias`,但当你需要动态生成一些别名配置时,可以考虑使用它。比如,基于环境变量来生成 `alias` 选项: | ||
|
||
```ts | ||
export default { | ||
source: { | ||
alias: { | ||
'@common': | ||
process.env.NODE_ENV === 'production' | ||
? './src/common-prod' | ||
: './src/common-dev', | ||
}, | ||
}, | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.