Skip to content

Commit 68eacc8

Browse files
committed
feat: support lib.id
1 parent 5bbe9e9 commit 68eacc8

File tree

6 files changed

+47
-16
lines changed

6 files changed

+47
-16
lines changed

examples/react-component-bundle-false/src/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import { CounterButton } from './components/CounterButton/index';
33
import { useCounter } from './useCounter';
4-
import './index.scss';
4+
import './index.css';
55

66
export const Counter: React.FC = () => {
77
const { count, increment, decrement } = useCounter();

packages/core/src/config.ts

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import type {
3333
AutoExternal,
3434
BannerAndFooter,
3535
DeepRequired,
36+
ExcludesFalse,
3637
Format,
3738
LibConfig,
3839
LibOnlyConfig,
@@ -1182,7 +1183,7 @@ async function composeLibRsbuildConfig(config: LibConfig, configPath: string) {
11821183
export async function composeCreateRsbuildConfig(
11831184
rslibConfig: RslibConfig,
11841185
path?: string,
1185-
): Promise<{ format: Format; config: RsbuildConfig }[]> {
1186+
): Promise<{ id?: string; format: Format; config: RsbuildConfig }[]> {
11861187
const constantRsbuildConfig = await createConstantRsbuildConfig();
11871188
const configPath = path ?? rslibConfig._privateMeta?.configFilePath!;
11881189
const { lib: libConfigsArray, ...sharedRsbuildConfig } = rslibConfig;
@@ -1217,6 +1218,7 @@ export async function composeCreateRsbuildConfig(
12171218
delete userConfig.output.externals;
12181219

12191220
return {
1221+
id: libConfig.id,
12201222
format: libConfig.format!,
12211223
// The merge order represents the priority of the configuration
12221224
// The priorities from high to low are as follows:
@@ -1230,6 +1232,7 @@ export async function composeCreateRsbuildConfig(
12301232
constantRsbuildConfig,
12311233
libRsbuildConfig,
12321234
omit<LibConfig, keyof LibOnlyConfig>(userConfig, {
1235+
id: true,
12331236
bundle: true,
12341237
format: true,
12351238
autoExtension: true,
@@ -1254,30 +1257,43 @@ export async function composeCreateRsbuildConfig(
12541257
export async function composeRsbuildEnvironments(
12551258
rslibConfig: RslibConfig,
12561259
): Promise<Record<string, EnvironmentConfig>> {
1257-
const rsbuildConfigObject = await composeCreateRsbuildConfig(rslibConfig);
1260+
const rsbuildConfigWithLibInfo =
1261+
await composeCreateRsbuildConfig(rslibConfig);
1262+
1263+
// User provided ids should take precedence over generated ids.
1264+
const usedIds = new Set<string>(
1265+
rsbuildConfigWithLibInfo
1266+
.map(({ id }) => id)
1267+
.filter(Boolean as any as ExcludesFalse),
1268+
);
12581269
const environments: RsbuildConfig['environments'] = {};
1259-
const formatCount: Record<Format, number> = rsbuildConfigObject.reduce(
1270+
const formatCount: Record<Format, number> = rsbuildConfigWithLibInfo.reduce(
12601271
(acc, { format }) => {
12611272
acc[format] = (acc[format] ?? 0) + 1;
12621273
return acc;
12631274
},
12641275
{} as Record<Format, number>,
12651276
);
12661277

1267-
const formatIndex: Record<Format, number> = {
1268-
esm: 0,
1269-
cjs: 0,
1270-
umd: 0,
1271-
mf: 0,
1272-
};
1278+
const getDefaultId = (format: Format): string => {
1279+
const getMaybeId = () => {
1280+
let index = 0;
1281+
while (usedIds.has(`${format}${index}`)) {
1282+
index++;
1283+
}
12731284

1274-
for (const { format, config } of rsbuildConfigObject) {
1275-
const currentFormatCount = formatCount[format];
1276-
const currentFormatIndex = formatIndex[format]++;
1285+
const finalId =
1286+
formatCount[format] === 1 && index === 0 ? '' : `${format}${index}`;
1287+
usedIds.add(finalId);
1288+
return finalId;
1289+
};
1290+
1291+
return getMaybeId();
1292+
};
12771293

1278-
environments[
1279-
currentFormatCount === 1 ? format : `${format}${currentFormatIndex}`
1280-
] = config;
1294+
for (const { format, id, config } of rsbuildConfigWithLibInfo) {
1295+
const libId = typeof id === 'string' ? id : getDefaultId(format);
1296+
environments[libId] = config;
12811297
}
12821298

12831299
return environments;

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ export type Redirect = {
7272
};
7373

7474
export interface LibConfig extends RsbuildConfig {
75+
/**
76+
* Each lib configuration has a unique identifier used to distinguish different lib configurations.
77+
* By default, Rslib generates a unique identifier based on the order of lib configurations, in the format `${format}${index}`.
78+
* When there is only one lib of the format, the index is empty, otherwise, it starts from 0 and increments.
79+
* For example:
80+
* - If only ESM and CJS formats are configured, the identifier for ESM is `esm` and for CJS is `cjs`.
81+
* - If two ESM formats and one CJS format are configured, they are represented as `esm0`, `esm1`, and `cjs`.
82+
* @default undefined
83+
*/
84+
id?: string;
7585
/**
7686
* Output format for the generated JavaScript files.
7787
* @default undefined

packages/core/src/types/utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ export type PkgJson = {
1010
export type DeepRequired<T> = Required<{
1111
[K in keyof T]: T[K] extends Required<T[K]> ? T[K] : DeepRequired<T[K]>;
1212
}>;
13+
14+
export type ExcludesFalse = <T>(x: T | false | undefined | null) => x is T;

packages/core/tests/__snapshots__/config.test.ts.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
230230
},
231231
},
232232
"format": "esm",
233+
"id": undefined,
233234
},
234235
{
235236
"config": {
@@ -455,6 +456,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
455456
},
456457
},
457458
"format": "cjs",
459+
"id": undefined,
458460
},
459461
{
460462
"config": {
@@ -664,6 +666,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
664666
},
665667
},
666668
"format": "umd",
669+
"id": undefined,
667670
},
668671
]
669672
`;

0 commit comments

Comments
 (0)