Skip to content

Commit c58d08a

Browse files
authored
feat: support umdName of array and object type (#1020)
1 parent 6707ee5 commit c58d08a

File tree

8 files changed

+58
-10
lines changed

8 files changed

+58
-10
lines changed

packages/core/src/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ const composeFormatConfig = ({
565565
format: Format;
566566
pkgJson: PkgJson;
567567
bundle?: boolean;
568-
umdName?: string;
568+
umdName?: Rspack.LibraryName;
569569
}): EnvironmentConfig => {
570570
const jsParserOptions = {
571571
cjs: {

packages/core/src/types/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ export interface LibConfig extends EnvironmentConfig {
297297
* @defaultValue `undefined`
298298
* @see {@link https://lib.rsbuild.dev/config/lib/umd-name}
299299
*/
300-
umdName?: string;
300+
umdName?: Rspack.LibraryName;
301301
/**
302302
* The base directory of the output files.
303303
* @defaultValue `undefined`

tests/integration/umd-library-name/index.test.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,21 @@ test('correct read UMD name from CommonJS', async () => {
1313
version: '1.2.3',
1414
},
1515
};
16-
const context = vm.createContext({
16+
let context = vm.createContext({
1717
globalThis: mockGlobalThis,
1818
});
1919

20-
vm.runInContext(entries.umd, context);
20+
vm.runInContext(entries.umd0!, context);
2121

2222
// @ts-expect-error
2323
expect(await mockGlobalThis.MyLibrary.fn('ok')).toBe('DEBUG:1.2.3/ok');
24+
25+
context = vm.createContext({
26+
globalThis: mockGlobalThis,
27+
});
28+
29+
vm.runInContext(entries.umd1!, context);
30+
31+
// @ts-expect-error
32+
expect(await mockGlobalThis.MyLibrary.Utils.fn('ok')).toBe('DEBUG:1.2.3/ok');
2433
});

tests/integration/umd-library-name/rslib.config.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@ export default defineConfig({
55
lib: [
66
generateBundleUmdConfig({
77
umdName: 'MyLibrary',
8+
output: {
9+
distPath: {
10+
root: './dist/string',
11+
},
12+
},
13+
}),
14+
generateBundleUmdConfig({
15+
umdName: ['MyLibrary', 'Utils'],
16+
output: {
17+
distPath: {
18+
root: './dist/array',
19+
},
20+
},
821
}),
922
],
1023
source: {

website/docs/en/config/lib/index.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ interface LibConfig extends EnvironmentConfig {
1515
footer?: BannerAndFooter;
1616
shims?: Shims;
1717
dts?: Dts;
18-
umdName?: string;
18+
umdName?: Rspack.LibraryName;
1919
}
2020

2121
interface RslibConfig extends RsbuildConfig {

website/docs/en/config/lib/umd-name.mdx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# lib.umdName
22

3-
- **Type:** `string`
3+
- **Type:** `string | string[] | { amd?: string, commonjs?: string, root?: string | string[] }`
44
- **Default:** `undefined`
55

66
The export name of the [UMD](/guide/basic/output-format#umd) bundle.
@@ -13,7 +13,7 @@ The module name of the UMD bundle must not conflict with the global variable nam
1313

1414
## Example
1515

16-
The UMD bundle will be mounted to `global.MyLibrary`.
16+
- Mount the UMD bundle to `global.MyLibrary`.
1717

1818
```ts title="rslib.config.ts"
1919
export default {
@@ -25,3 +25,16 @@ export default {
2525
],
2626
};
2727
```
28+
29+
- Mount the UMD bundle to `global.MyLibrary.Utils`.
30+
31+
```ts title="rslib.config.ts"
32+
export default {
33+
lib: [
34+
{
35+
format: 'umd',
36+
umdName: ['MyLibrary', 'Utils'], // [!code highlight]
37+
},
38+
],
39+
};
40+
```

website/docs/zh/config/lib/index.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ interface LibConfig extends EnvironmentConfig {
1515
footer?: BannerAndFooter;
1616
shims?: Shims;
1717
dts?: Dts;
18-
umdName?: string;
18+
umdName?: Rspack.LibraryName;
1919
}
2020

2121
interface RslibConfig extends RsbuildConfig {

website/docs/zh/config/lib/umd-name.mdx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# lib.umdName
22

3-
- **类型:** `string`
3+
- **类型:** `string | string[] | { amd?: string, commonjs?: string, root?: string | string[] }`
44
- **默认值:** `undefined`
55

66
[UMD](/guide/basic/output-format#umd) 包的导出名称。
@@ -13,7 +13,7 @@ UMD 包的模块名称不能与全局变量名称冲突。
1313

1414
## 示例
1515

16-
UMD 包将会挂载到 `global.MyLibrary`
16+
-UMD 包挂载到 `global.MyLibrary`
1717

1818
```ts title="rslib.config.ts"
1919
export default {
@@ -25,3 +25,16 @@ export default {
2525
],
2626
};
2727
```
28+
29+
- 将 UMD 包挂载到 `global.MyLibrary.Utils`
30+
31+
```ts title="rslib.config.ts"
32+
export default {
33+
lib: [
34+
{
35+
format: 'umd',
36+
umdName: ['MyLibrary', 'Utils'], // [!code highlight]
37+
},
38+
],
39+
};
40+
```

0 commit comments

Comments
 (0)