Skip to content

Commit d931b48

Browse files
9aoychenjiahan
andauthored
feat: transformImport support function type (#3266)
Co-authored-by: neverland <[email protected]>
1 parent 436a4ed commit d931b48

File tree

7 files changed

+418
-8
lines changed

7 files changed

+418
-8
lines changed

packages/compat/plugin-swc/src/utils.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { __internalHelper } from '@rsbuild/core';
44
import type {
55
ModifyChainUtils,
66
NormalizedEnvironmentConfig,
7+
NormalizedSourceConfig,
8+
TransformImport,
79
} from '@rsbuild/core';
810
import _ from 'lodash';
911
import semver from 'semver';
@@ -17,6 +19,13 @@ import type {
1719

1820
const { applySwcDecoratorConfig } = __internalHelper;
1921

22+
const castArray = <T>(arr?: T | T[]): T[] => {
23+
if (arr === undefined) {
24+
return [];
25+
}
26+
return Array.isArray(arr) ? arr : [arr];
27+
};
28+
2029
async function findUp({
2130
filename,
2231
cwd = process.cwd(),
@@ -184,6 +193,24 @@ const getCoreJsVersion = (corejsPkgPath: string) => {
184193
}
185194
};
186195

196+
const reduceTransformImportConfig = (
197+
options: NormalizedSourceConfig['transformImport'],
198+
): TransformImport[] | false => {
199+
if (options === false || !options) {
200+
return false;
201+
}
202+
203+
let imports: TransformImport[] = [];
204+
for (const item of castArray(options)) {
205+
if (typeof item === 'function') {
206+
imports = item(imports) ?? imports;
207+
} else {
208+
imports.push(item);
209+
}
210+
}
211+
return imports;
212+
};
213+
187214
export async function applyPluginConfig(
188215
rawOptions: PluginSwcOptions,
189216
utils: ModifyChainUtils,
@@ -248,9 +275,13 @@ export async function applyPluginConfig(
248275

249276
const extensions = swc.extensions;
250277

251-
if (rsbuildConfig.source?.transformImport) {
278+
const finalPluginImport = reduceTransformImportConfig(
279+
rsbuildConfig.source?.transformImport,
280+
);
281+
282+
if (finalPluginImport !== false && finalPluginImport?.length) {
252283
extensions.pluginImport ??= [];
253-
extensions.pluginImport.push(...rsbuildConfig.source.transformImport);
284+
extensions.pluginImport.push(...finalPluginImport);
254285
}
255286

256287
if (pluginOptions?.transformLodash !== false) {

packages/core/src/plugins/swc.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ import {
88
PLUGIN_SWC_NAME,
99
SCRIPT_REGEX,
1010
} from '../constants';
11-
import { castArray, cloneDeep, isWebTarget } from '../helpers';
11+
import { castArray, cloneDeep, isFunction, isWebTarget } from '../helpers';
1212
import type {
1313
NormalizedEnvironmentConfig,
1414
NormalizedSourceConfig,
1515
Polyfill,
1616
RsbuildContext,
1717
RsbuildPlugin,
1818
RspackChain,
19+
TransformImport,
1920
} from '../types';
2021

2122
const builtinSwcLoaderName = 'builtin:swc-loader';
@@ -215,14 +216,34 @@ async function applyCoreJs(
215216
return coreJsDir;
216217
}
217218

219+
const reduceTransformImportConfig = (
220+
options: NormalizedSourceConfig['transformImport'],
221+
): TransformImport[] | false => {
222+
if (options === false || !options) {
223+
return false;
224+
}
225+
226+
let imports: TransformImport[] = [];
227+
for (const item of castArray(options)) {
228+
if (isFunction(item)) {
229+
imports = item(imports) ?? imports;
230+
} else {
231+
imports.push(item);
232+
}
233+
}
234+
return imports;
235+
};
236+
218237
function applyTransformImport(
219238
swcConfig: SwcLoaderOptions,
220239
pluginImport?: NormalizedSourceConfig['transformImport'],
221240
) {
222-
if (pluginImport !== false && pluginImport) {
241+
const finalPluginImport = reduceTransformImportConfig(pluginImport);
242+
243+
if (finalPluginImport !== false && finalPluginImport?.length) {
223244
swcConfig.rspackExperiments ??= {};
224245
swcConfig.rspackExperiments.import ??= [];
225-
swcConfig.rspackExperiments.import.push(...pluginImport);
246+
swcConfig.rspackExperiments.import.push(...finalPluginImport);
226247
}
227248
}
228249

packages/core/src/types/config/source.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ export interface SourceConfig {
6161
/**
6262
* Used to import the code and style of the component library on demand.
6363
*/
64-
transformImport?: false | TransformImport[];
64+
transformImport?:
65+
| TransformImportFn
66+
| Array<TransformImport | TransformImportFn>
67+
| false;
6568
/**
6669
* Configure a custom tsconfig.json file path to use, can be a relative or absolute path.
6770
* @default 'tsconfig.json'
@@ -82,6 +85,10 @@ export type TransformImport = {
8285
customStyleName?: any;
8386
};
8487

88+
type TransformImportFn = (
89+
imports: TransformImport[],
90+
) => TransformImport[] | void;
91+
8592
export interface NormalizedSourceConfig extends SourceConfig {
8693
define: Define;
8794
alias: ConfigChain<Alias>;

0 commit comments

Comments
 (0)