What problem does this feature solve?
Currently, the Modern.js Builder automatically reads the compilerOptions.paths field in tsconfig.json as the alias config, and the priority of tsconfig paths is higher than source.alias config, so the source.alias is basically useless for TypeScript projects.
- Example for tsconfig paths:
{
"compilerOptions": {
"paths": {
"@common/*": ["./src/common/*"]
}
}
}
- Example for
source.alias:
export default {
source: {
alias: {
'@common': './src/common',
},
},
};
Detailed document of the current behavior: https://modernjs.dev/builder/en/guide/advanced/alias.html
But in some scenarios, the user does not want the default behavior, they want to use source.alias because it allows user to create some alias config dynamically, for example, use process.env to set alias.
export default {
source: {
alias: {
'@common': process.env.NODE_ENV === 'production' ? './src/common-prod' : './src/common-dev',
},
},
};
And tsconfig paths config is static (written by JSON) and not as flexible as source.alias. In this case, tsconfig paths config is only used to provide the type of alias, and should not override the user configured alias config.
What does the proposed API look like?
The Modern.js Builder will add a new source.aliasStrategy config to change the default behavior, so users can use the source.alias and it will not be overridden by tsconfig paths.
- Type:
'prefer-tsconfig' | 'prefer-alias'
- Default:
'prefer-alias'
- Example:
export default {
source: {
aliasStrategy:'prefer-tsconfig',
}
}
What problem does this feature solve?
Currently, the Modern.js Builder automatically reads the
compilerOptions.pathsfield intsconfig.jsonas the alias config, and the priority of tsconfig paths is higher thansource.aliasconfig, so thesource.aliasis basically useless for TypeScript projects.{ "compilerOptions": { "paths": { "@common/*": ["./src/common/*"] } } }source.alias:But in some scenarios, the user does not want the default behavior, they want to use
source.aliasbecause it allows user to create some alias config dynamically, for example, useprocess.envto set alias.And tsconfig paths config is static (written by JSON) and not as flexible as
source.alias. In this case, tsconfig paths config is only used to provide the type of alias, and should not override the user configured alias config.What does the proposed API look like?
The Modern.js Builder will add a new
source.aliasStrategyconfig to change the default behavior, so users can use thesource.aliasand it will not be overridden by tsconfig paths.'prefer-tsconfig' | 'prefer-alias''prefer-alias'