-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
esbuildNanobundlePlugin.ts
138 lines (122 loc) · 3.01 KB
/
esbuildNanobundlePlugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import * as path from 'node:path';
import { type Plugin } from 'esbuild';
import { type Context } from '../context';
import * as fsUtils from '../fsUtils';
import { type ImportMaps, replaceSubpathPattern } from '../importMaps';
type PluginOptions = {
context: Context,
importMaps: ImportMaps,
};
export function makePlugin({
context,
context: {
reporter,
standalone,
externalDependencies,
forceExternalDependencies,
},
importMaps,
}: PluginOptions): Plugin {
const ownedModule = (packageName: string, modulePath: string) => {
return packageName === modulePath || modulePath.startsWith(packageName + '/');
};
const isNodeApi = (modulePath: string) => {
if (externalDependencies.some(dep => modulePath.startsWith(dep))) {
return false;
}
return modulePath.startsWith('node:') || nodeApis.some(api => ownedModule(api, modulePath));
};
const shouldEmbed = (modulePath: string) => {
if (forceExternalDependencies.some(dep => ownedModule(dep, modulePath))) {
return false;
}
return standalone || !externalDependencies.some(dep => ownedModule(dep, modulePath));
};
const resolveModulePathFromImportMaps = async (modulePath: string) => {
const resolved = path.resolve(path.dirname(context.importMapsPath), modulePath);
const exist = await fsUtils.chooseExist([
resolved.replace(/\.(c|m)?js$/, '.tsx'),
resolved.replace(/\.(c|m)?js$/, '.ts'),
resolved,
]);
return exist || resolved;
};
return {
name: 'nanobundle',
setup(build) {
let dependOnNode = false;
build.onResolve({ filter: /.*/ }, async args => {
if (fsUtils.isFileSystemReference(args.path)) {
return;
}
const modulePath = replaceSubpathPattern(importMaps, args.path);
const external = !fsUtils.isFileSystemReference(modulePath);
let resolvedAsNodeApi = isNodeApi(modulePath);
if (resolvedAsNodeApi) {
dependOnNode = true;
}
if (!resolvedAsNodeApi && shouldEmbed(modulePath)) {
return {};
}
return {
external,
path: external
? modulePath
: await resolveModulePathFromImportMaps(modulePath),
};
});
build.onEnd(() => {
if (standalone && dependOnNode) {
reporter.warn('Not completely standalone bundle, while the code depends on some Node.js APIs.');
}
});
},
};
}
const nodeApis = [
'assert',
'async_hooks',
'buffer',
'child_process',
'cluster',
'console',
'crypto',
'diagnostics_channel',
'dns',
'events',
'fs',
'http',
'http2',
'https',
'inspector',
'module',
'net',
'os',
'path',
'perf_hooks',
'process',
'readline',
'stream',
'string_decoder',
'timers',
'tls',
'trace_events',
'tty',
'dgram',
'url',
'util',
'v8',
'vm',
'wasi',
'worker_threads',
'zlib',
// legacy
'querystring',
// deprecated
'_linklist',
'_stream_wrap',
'constants',
'domain',
'punycode',
'sys',
];