Used to import the code and style of the component library on demand, which is equivalent to babel-plugin-import.
The difference between it and babel-plugin-import is that source.transformImport
is not coupled with Babel. Rsbuild will automatically identify whether the currently used tools is Babel, SWC or Rspack, and apply the corresponding on-demand import configuration.
When using the above antd default configuration:
The source code is as follows:
It will be transformed into:
You can manually set transformImport: false
to disable the default config.
For example, if you use externals
to avoid bundling antd, because transformImport
will convert the imported path of antd by default, the matching path changes and externals cannot take effect. At this time, you can disable transformImport
to avoid this problem.
string
The original import path that needs to be transformed.
string
'lib'
Used to splice the transformed path, the splicing rule is ${libraryName}/${libraryDirectory}/${member}
, where member is the imported member.
Example:
Out:
boolean
undefined
Determines whether to import related styles. If it is true
, the path ${libraryName}/${libraryDirectory}/${member}/style
will be imported. If it is false
or undefined
, the style will not be imported.
When it is set to true
:
Out:
string
undefined
This configuration is used to splice the import path when importing styles. If this configuration is specified, the style
configuration option will be ignored. The spliced import path is ${libraryName}/${styleLibraryDirectory}/${member}
.
When it is set to styles
:
Out:
boolean
true
Whether to convert camelCase imports to kebab-case.
Example:
Out:
boolean
true
Whether to convert import statements to default imports.
Example:
Out:
((member: string) => string | undefined) | string
undefined
Customize the imported path after conversion. The input is the imported member. For example, configure it as (member) => `my-lib/${member}`
, which will convert import { foo } from 'bar'
to import foo from 'my-lib/foo'
.
When using Rspack to build, function configurations cannot be used, but you can use handlebars template strings. For the above function configuration, you can use the following template instead of my-lib/{{ member }}
, or use some built-in helper methods, such as my-lib/{{ kebabCase member }}
to convert it to kebab-case format. In addition to kebabCase, there are also camelCase, snakeCase, upperCase, and lowerCase that can be used.
((member: string) => string | undefined) | string
undefined
Customize the imported style path after conversion. The input is the imported member. For example, configure it as (member) => `my-lib/${member}`
, which will convert import { foo } from 'bar'
to import foo from 'my-lib/foo'
.
When using Rspack to build, function configurations cannot be used, but you can use handlebars template strings. For the above function configuration, you can use the following template instead of my-lib/{{ member }}
, or use some built-in helper methods, such as my-lib/{{ kebabCase member }}
to convert it to kebab-case format. In addition to kebabCase, there are also camelCase, snakeCase, upperCase, and lowerCase that can be used.