In modern front-end development, modularity has become a key approach to managing code effectively:
Simplified Module Imports:
@components/Button instead of ../../../src/components/Button, which significantly improves code maintainability.Environment-Aware Module Substitution:
Path aliases allow developers to define aliases for modules, making it easier to reference them in code. This can be useful when you want to use a short, easy-to-remember name for a module instead of a long, complex path.
For example, if you frequently reference the src/common/request.ts module in your project, you can define an alias for it as @request and then use import request from '@request' in your code instead of writing the full relative path every time. This also allows you to move the module to a different location without needing to update all the import statements in your code.
tsconfig.json's paths ConfigurationYou can configure aliases through the paths configuration in tsconfig.json, which is the recommended approach in TypeScript projects as it also resolves the TS type issues related to path aliases.
For example:
After configuring, if you reference @common/Foo.tsx in your code, it will be mapped to the <project>/src/common/Foo.tsx path.
You can refer to the TypeScript - paths documentation for more details.
source.alias ConfigurationRsbuild provides the source.alias configuration option, which corresponds to the webpack/Rspack native resolve.alias configuration. You can configure this option using an object or a function.
The paths configuration in tsconfig.json is static and lacks dynamism. Furthermore, paths only takes effect when the module is included in source.include.
The source.alias configuration can overcome this limitation by enabling you to dynamically set source.alias using JavaScript code.
For example, use the workspace version of lodash-es for all dependencies:
NodeJS's conditional exports provide a way to map to different paths depending on certain conditions.
For example, consider an arbitrary library with a package.json that contains the following exports:
Importing:
foo will resolve to foo/index-lynx.jsfoo/bar will resolve to foo/bar-import.jsDuring condition matching, earlier entries have higher priority and take precedence over later entries.
The default value of resolve.conditionNames is:
To change the default value, use tools.rspack:
When importing from an npm package, the mainFields option will determine which fields in its package.json are checked.
For example, consider an arbitrary library with a package.json that contains the following fields:
When we import * as Upstream from 'upstream' this will actually resolve to the file in the lynx property. The lynx property takes precedence because it's the first item in mainFields.
The default value of resolve.mainFields is:
To change the default value, use tools.rspack:
If the path to be resolved is an directory, the resolver will try to resolve the mainFiles of the directory.
For example, consider an directory with:
Importing
dir/ will resolve to dir/index.lynx.jsdir/sub will resolve to dir/sub/index.jsThe default value of resolve.mainFiles is:
To change the default value, use tools.rspack: