Skip to content

[Feature]: Support ignoring tsconfig paths config #4192

@chenjiahan

Description

@chenjiahan

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',
  }
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions