Skip to content

Commit 1c2685d

Browse files
committed
chore: update
1 parent ba55822 commit 1c2685d

File tree

5 files changed

+94
-40
lines changed

5 files changed

+94
-40
lines changed

packages/core/src/config.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,15 +235,29 @@ const composeExternalsWarnConfig = (
235235
};
236236
};
237237

238+
const getAutoExternalDefaultValue = (
239+
format: Format,
240+
autoExternal?: AutoExternal,
241+
): AutoExternal => {
242+
return isIntermediateOutputFormat(format)
243+
? (autoExternal ?? true)
244+
: (autoExternal ?? false);
245+
};
246+
238247
export const composeAutoExternalConfig = (options: {
239248
format: Format;
240249
autoExternal?: AutoExternal;
241250
pkgJson?: PkgJson;
242251
userExternals?: NonNullable<RsbuildConfig['output']>['externals'];
243252
}): RsbuildConfig => {
244-
const { format, autoExternal = true, pkgJson, userExternals } = options;
253+
const { format, pkgJson, userExternals } = options;
254+
255+
const autoExternal = getAutoExternalDefaultValue(
256+
format,
257+
options.autoExternal,
258+
);
245259

246-
if (autoExternal === false || !isIntermediateOutputFormat(format)) {
260+
if (autoExternal === false) {
247261
return {};
248262
}
249263

@@ -1007,7 +1021,7 @@ const composeDtsConfig = async (
10071021
libConfig: LibConfig,
10081022
dtsExtension: string,
10091023
): Promise<RsbuildConfig> => {
1010-
const { format, autoExternal = true, banner, footer } = libConfig;
1024+
const { format, autoExternal, banner, footer } = libConfig;
10111025

10121026
let { dts } = libConfig;
10131027

@@ -1030,9 +1044,7 @@ const composeDtsConfig = async (
10301044
build: dts?.build,
10311045
abortOnError: dts?.abortOnError,
10321046
dtsExtension: dts?.autoExtension ? dtsExtension : '.d.ts',
1033-
autoExternal: !isIntermediateOutputFormat(format!)
1034-
? false
1035-
: autoExternal,
1047+
autoExternal: getAutoExternalDefaultValue(format!, autoExternal),
10361048
banner: banner?.dts,
10371049
footer: footer?.dts,
10381050
}),

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,7 @@ export interface LibConfig extends RsbuildConfig {
109109
autoExtension?: boolean;
110110
/**
111111
* Whether to automatically externalize dependencies of different dependency types and do not bundle them.
112-
* Only takes effect when {@link format} is `cjs` or `esm`.
113-
* @defaultValue `true`
112+
* @defaultValue `true` when {@link format} is `cjs` or `esm`, `false` when {@link format} is `umd` or `mf`.
114113
* @see {@link https://lib.rsbuild.dev/config/lib/auto-external}
115114
*/
116115
autoExternal?: AutoExternal;

packages/core/tests/external.test.ts

Lines changed: 69 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { composeAutoExternalConfig } from '../src/config';
44
vi.mock('rslog');
55

66
describe('should composeAutoExternalConfig correctly', () => {
7-
it('autoExternal is undefined', () => {
7+
it('autoExternal default value', () => {
88
const esmResult = composeAutoExternalConfig({
99
format: 'esm',
1010
autoExternal: undefined,
@@ -27,6 +27,28 @@ describe('should composeAutoExternalConfig correctly', () => {
2727
},
2828
});
2929

30+
const umdResult = composeAutoExternalConfig({
31+
format: 'umd',
32+
autoExternal: undefined,
33+
pkgJson: {
34+
name: 'umd',
35+
dependencies: {
36+
foo: '1.0.0',
37+
},
38+
},
39+
});
40+
41+
const mfResult = composeAutoExternalConfig({
42+
format: 'mf',
43+
autoExternal: undefined,
44+
pkgJson: {
45+
name: 'mf',
46+
dependencies: {
47+
foo: '1.0.0',
48+
},
49+
},
50+
});
51+
3052
expect(esmResult).toMatchInlineSnapshot(`
3153
{
3254
"output": {
@@ -48,33 +70,8 @@ describe('should composeAutoExternalConfig correctly', () => {
4870
},
4971
}
5072
`);
51-
});
52-
53-
it('autoExternal should be disabled when format is umd or mf', () => {
54-
const umdResult = composeAutoExternalConfig({
55-
format: 'umd',
56-
autoExternal: undefined,
57-
pkgJson: {
58-
name: 'umd',
59-
dependencies: {
60-
foo: '1.0.0',
61-
},
62-
},
63-
});
6473

6574
expect(umdResult).toMatchInlineSnapshot('{}');
66-
67-
const mfResult = composeAutoExternalConfig({
68-
format: 'mf',
69-
autoExternal: undefined,
70-
pkgJson: {
71-
name: 'mf',
72-
dependencies: {
73-
foo: '1.0.0',
74-
},
75-
},
76-
});
77-
7875
expect(mfResult).toMatchInlineSnapshot('{}');
7976
});
8077

@@ -111,6 +108,52 @@ describe('should composeAutoExternalConfig correctly', () => {
111108
});
112109
});
113110

111+
it('autoExternal is true when format is umd or mf', () => {
112+
const umdResult = composeAutoExternalConfig({
113+
format: 'umd',
114+
autoExternal: true,
115+
pkgJson: {
116+
name: 'umd',
117+
dependencies: {
118+
foo: '1.0.0',
119+
},
120+
},
121+
});
122+
123+
expect(umdResult).toMatchInlineSnapshot(`
124+
{
125+
"output": {
126+
"externals": [
127+
/\\^foo\\(\\$\\|\\\\/\\|\\\\\\\\\\)/,
128+
"foo",
129+
],
130+
},
131+
}
132+
`);
133+
134+
const mfResult = composeAutoExternalConfig({
135+
format: 'mf',
136+
autoExternal: true,
137+
pkgJson: {
138+
name: 'mf',
139+
dependencies: {
140+
foo: '1.0.0',
141+
},
142+
},
143+
});
144+
145+
expect(mfResult).toMatchInlineSnapshot(`
146+
{
147+
"output": {
148+
"externals": [
149+
/\\^foo\\(\\$\\|\\\\/\\|\\\\\\\\\\)/,
150+
"foo",
151+
],
152+
},
153+
}
154+
`);
155+
});
156+
114157
it('autoExternal will deduplication ', () => {
115158
const result = composeAutoExternalConfig({
116159
format: 'esm',

website/docs/en/config/lib/auto-external.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ type AutoExternal =
1717
};
1818
```
1919

20-
- **Default:** `true`
20+
- **Default:**
21+
- `true` when [format](/config/lib/format) is `cjs` or `esm`
22+
- `false` when [format](/config/lib/format) is `umd` or `mf`
2123

2224
Whether to automatically externalize dependencies of different dependency types and do not bundle them.
2325

24-
This option only takes effect when [format](/config/lib/format) is `cjs` or `esm`.
25-
2626
## Object Type
2727

2828
### autoExternal.dependencies

website/docs/zh/config/lib/auto-external.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ type AutoExternal =
1717
};
1818
```
1919

20-
- **默认值:** `true`
20+
- **默认值:**
21+
-[format](/config/lib/format)`cjs``esm` 时为 `true`
22+
-[format](/config/lib/format)`umd``mf` 时为 `false`
2123

2224
是否自动对不同依赖类型的依赖进行外部化处理,不将其打包。
2325

24-
该选项仅当 [format](/config/lib/format)`cjs``esm` 时生效。
25-
2626
## 对象类型
2727

2828
### autoExternal.dependencies

0 commit comments

Comments
 (0)