Module Resolution
In modern front-end development, modularity has become a key approach to managing code effectively:
-
Simplified Module Imports:
- Module resolution allows you to use concise and more readable paths for importing modules, instead of cumbersome relative paths. For example, with an alias configuration, you can use
@components/Buttoninstead of../../../src/components/Button, which significantly improves code maintainability.
- Module resolution allows you to use concise and more readable paths for importing modules, instead of cumbersome relative paths. For example, with an alias configuration, you can use
-
Environment-Aware Module Substitution:
- Module resolution enables you to load different versions of modules based on the environment (e.g., development and production or Lynx and browser). You can easily achieve environment-aware module substitution, ensuring that the most appropriate dependencies are used in different scenarios.
Path Aliases
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.
Using tsconfig.json's paths Configuration
You 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.
Use resolve.alias Configuration
Rsbuild provides the resolve.alias configuration option, which corresponds to the webpack/Rspack native resolve.alias configuration. You can configure this option using an object or a function.
Use Cases
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 resolve.alias configuration can overcome this limitation by enabling you to dynamically set resolve.alias using JavaScript code.
For example, use the workspace version of lodash-es for all dependencies:
Conditional Exports
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:
foowill resolve tofoo/index-lynx.jsfoo/barwill resolve tofoo/bar-import.js
During 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:
Main Fields
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:
Main Files
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 todir/index.lynx.jsdir/subwill resolve todir/sub/index.js
The default value of resolve.mainFiles is:
To change the default value, use tools.rspack:

