Skip to content

Commit 9f732e5

Browse files
authored
feat: external support RegExp and not external sub path of package (#673)
* feat: external support RegExp and not external sub path of package * fix: remove duplicate push
1 parent 3440be9 commit 9f732e5

File tree

6 files changed

+141
-37
lines changed

6 files changed

+141
-37
lines changed

.changeset/dry-pots-buy.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ice/pkg': major
3+
---
4+
5+
feat: external support RegExp and not external sub path of package

.github/workflows/release-next.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Release
1+
name: Release Next
22

33
on:
44
push:
Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,112 @@
11
// Copy from https://github.com/sindresorhus/builtin-modules#readme
22
export const builtinNodeModules = [
3+
'node:assert',
34
'assert',
5+
'node:assert/strict',
6+
'assert/strict',
7+
'node:async_hooks',
48
'async_hooks',
9+
'node:buffer',
510
'buffer',
11+
'node:child_process',
612
'child_process',
13+
'node:cluster',
714
'cluster',
15+
'node:console',
816
'console',
17+
'node:constants',
918
'constants',
19+
'node:crypto',
1020
'crypto',
21+
'node:dgram',
1122
'dgram',
23+
'node:diagnostics_channel',
24+
'diagnostics_channel',
25+
'node:dns',
1226
'dns',
27+
'node:dns/promises',
28+
'dns/promises',
29+
'node:domain',
1330
'domain',
31+
'node:events',
1432
'events',
33+
'node:fs',
1534
'fs',
35+
'node:fs/promises',
36+
'fs/promises',
37+
'node:http',
1638
'http',
39+
'node:http2',
1740
'http2',
41+
'node:https',
1842
'https',
43+
'node:inspector',
1944
'inspector',
45+
'node:inspector/promises',
46+
'inspector/promises',
47+
'node:module',
2048
'module',
49+
'node:net',
2150
'net',
51+
'node:os',
2252
'os',
53+
'node:path',
2354
'path',
55+
'node:path/posix',
56+
'path/posix',
57+
'node:path/win32',
58+
'path/win32',
59+
'node:perf_hooks',
2460
'perf_hooks',
61+
'node:process',
2562
'process',
26-
'punycode',
63+
'node:querystring',
2764
'querystring',
65+
'node:quic',
66+
'node:readline',
2867
'readline',
68+
'node:readline/promises',
69+
'readline/promises',
70+
'node:repl',
2971
'repl',
72+
'node:sea',
73+
'node:sqlite',
74+
'node:stream',
3075
'stream',
76+
'node:stream/consumers',
77+
'stream/consumers',
78+
'node:stream/promises',
79+
'stream/promises',
80+
'node:stream/web',
81+
'stream/web',
82+
'node:string_decoder',
3183
'string_decoder',
84+
'node:test',
85+
'node:test/reporters',
86+
'node:timers',
3287
'timers',
88+
'node:timers/promises',
89+
'timers/promises',
90+
'node:tls',
3391
'tls',
92+
'node:trace_events',
3493
'trace_events',
94+
'node:tty',
3595
'tty',
96+
'node:url',
3697
'url',
98+
'node:util',
3799
'util',
100+
'node:util/types',
101+
'util/types',
102+
'node:v8',
38103
'v8',
104+
'node:vm',
39105
'vm',
106+
'node:wasi',
40107
'wasi',
108+
'node:worker_threads',
41109
'worker_threads',
110+
'node:zlib',
42111
'zlib',
43112
];

packages/pkg/src/helpers/getRollupOptions.ts

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -216,44 +216,57 @@ function getRollupOutputs({
216216
}));
217217
}
218218

219+
const BUILTIN_EXTERNAL_MAP: Record<string, string[]> = {
220+
'builtin:normal': [
221+
'core-js',
222+
'regenerator-runtime',
223+
],
224+
'builtin:node': builtinNodeModules,
225+
}
226+
219227
function getExternalsAndGlobals(
220228
bundleTaskConfig: BundleTaskConfig,
221229
pkg: PkgJson,
222230
): [(id?: string) => boolean, Record<string, string>] {
223-
let externals: string[] = [];
224-
let globals: Record<string, string> = {};
231+
// TODO: unique externals after all pushed
232+
const exactExternals: string[] = [];
233+
const regexpExternals: RegExp[] = [];
234+
const globals: Record<string, string> = {};
225235

226-
const builtinExternals = [
227-
'react/jsx-runtime',
228-
'core-js',
229-
'regenerator-runtime',
230-
];
236+
const userExternals = bundleTaskConfig.externals ?? false;
231237

232-
const externalsConfig = bundleTaskConfig.externals ?? false;
233-
234-
switch (externalsConfig) {
235-
case true:
236-
externals = [
237-
...builtinNodeModules,
238-
...builtinExternals,
239-
...Object.keys(pkg.dependencies ?? {}),
240-
...Object.keys(pkg.peerDependencies ?? {}),
241-
];
242-
break;
243-
case false:
244-
externals = [];
245-
break;
246-
default:
247-
externals = Object.keys(externalsConfig);
248-
globals = externalsConfig;
249-
break;
238+
if (userExternals === true) {
239+
exactExternals.push(
240+
...BUILTIN_EXTERNAL_MAP['builtin:normal'],
241+
...BUILTIN_EXTERNAL_MAP['builtin:node'],
242+
...Object.keys(pkg.dependencies ?? {}),
243+
...Object.keys(pkg.peerDependencies ?? {}),
244+
)
245+
} else if (userExternals === false) {
246+
// do nothing
247+
} else if (Array.isArray(userExternals)) {
248+
for (const item of userExternals) {
249+
if (typeof item === 'string') {
250+
if (item in BUILTIN_EXTERNAL_MAP) {
251+
exactExternals.push(...BUILTIN_EXTERNAL_MAP[item]);
252+
} else {
253+
exactExternals.push(item);
254+
}
255+
} else if (item instanceof RegExp) {
256+
regexpExternals.push(item);
257+
} else if (typeof item === 'object') {
258+
exactExternals.push(...Object.keys(item));
259+
Object.assign(globals, item)
260+
}
261+
}
262+
} else if (typeof userExternals === 'object') {
263+
exactExternals.push(...Object.keys(userExternals));
264+
Object.assign(globals, userExternals);
250265
}
251266

252-
const externalPredicate = new RegExp(`^(${externals.map(escapeStringRegexp).join('|')})($|/)`);
253-
254-
const externalFun = externals.length === 0
267+
const externalFun = !exactExternals.length && !regexpExternals.length
255268
? () => false
256-
: (id: string) => externalPredicate.test(id);
269+
: (id: string) => exactExternals.includes(id) || regexpExternals.some(reg => reg.test(id));
257270

258271
return [externalFun, globals];
259272
}

packages/pkg/src/types.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,11 @@ export interface BundleUserConfig {
9696
* Specify external dependencies.
9797
* "boolean" - whether to bundle all dependencies or not;
9898
* "object" - specific external dependencies.
99-
* @default false All of dependencies will be bundled by default.
99+
* "array" - specific external match logic
100+
* "false" - all the dependencies will be bundled by default.
101+
* @default false
100102
*/
101-
externals?: boolean | Record<string, string>;
103+
externals?: boolean | Record<string, string> | (string | RegExp | Record<string, string>)[];
102104

103105
/**
104106
* Minify JS and CSS bundle.

website/docs/reference/config.md

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -373,10 +373,12 @@ export default defineConfig({
373373

374374
#### externals
375375

376-
+ 类型:`boolean | Record<string, string>`
377-
+ 默认值:`true`
376+
+ 类型:`boolean | Record<string, string> | (string | RegExp | Record<string, string>)[]`
377+
+ 默认值:`false`
378378

379-
默认情况下,bundle 的产物包含所有依赖产物。该选项可修改这一结果。若想要 Bundle 不包含依赖产物,可如下配置:
379+
默认情况下,bundle 的产物包含所有依赖产物。该选项可修改这一结果。
380+
若想要 Bundle 不包含依赖产物,可以传入 `true`,其会解析 `package.json` 并将所有依赖 external 掉,包括 node 的依赖。
381+
适合针对 Node 环境的构建。
380382

381383
```ts title="build.config.mts"
382384
import { defineConfig } from '@ice/pkg';
@@ -388,7 +390,18 @@ export default defineConfig({
388390
});
389391
```
390392

391-
若想要自定义配置 externals,参考如下配置:
393+
若想要自定义配置 externals,则可以直接传入想要 external 的依赖,支持字符串和正则表达式。
394+
395+
```ts title="build.config.mts"
396+
import { defineConfig } from '@ice/pkg';
397+
export default defineConfig({
398+
bundle: {
399+
externals: ['react', 'react-dom', /^@ice($|\/)/],
400+
},
401+
});
402+
```
403+
404+
如果你选择构建 umd 格式,默认情况下会根据一定的规则生成从全局对象上获取依赖的名字,如果你想自定义,则可以直接传入一个对象来配置。
392405

393406
```ts title="build.config.mts"
394407
import { defineConfig } from '@ice/pkg';
@@ -403,6 +416,8 @@ export default defineConfig({
403416
});
404417
```
405418

419+
当然,也可以进行混合使用。
420+
406421
#### minify
407422

408423
+ 类型:`boolean | { js?: boolean | ((mode: string, command: string) => boolean | { options?: swc.JsMinifyOptions }); css?: boolean | ((mode: string, command: string) => boolean | { options?: cssnano.Options });}`

0 commit comments

Comments
 (0)