-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathbuild.js
More file actions
157 lines (143 loc) · 3.74 KB
/
Copy pathbuild.js
File metadata and controls
157 lines (143 loc) · 3.74 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const { context, build } = require('esbuild');
const { dependencies, devDependencies } = require('./package.json');
const { tailwindPlugin } = require('esbuild-plugin-tailwindcss');
const fs = require('fs');
/**
* Node.js builtins that esbuild-plugin-polyfill-node provides *empty*
* polyfills for — if any of these are imported by the ESM library
* bundle, downstream browser consumers will fail to bundle.
*/
const BROWSER_UNSAFE_IMPORTS = ['crypto'];
function verifyEsmForBrowser(esmPath) {
const content = fs.readFileSync(esmPath, 'utf8');
// Match named imports: `import { X } from "mod"`. Namespace imports
// (`import * as X from "mod"`) are safe because the empty polyfill
// exports an empty module, so accessing mod.randomBytes is undefined
// at runtime (which the D2 renderer already guards against).
const regex = /import\s*\{([^}]*)\}\s*from\s*["']([^"']+)["']/g;
let match;
const found = new Set();
while ((match = regex.exec(content)) !== null) {
const mod = match[2];
const names = match[1].split(',').map((s) => s.trim());
if (BROWSER_UNSAFE_IMPORTS.includes(mod)) {
for (const name of names) {
if (name && !name.startsWith('//')) {
found.add(`${name} from ${mod}`);
}
}
}
}
if (found.size > 0) {
const list = [...found].join(', ');
throw new Error(
`ESM bundle has named imports from browser-unsafe Node.js builtins: ${list}. ` +
'These will cause esbuild-plugin-polyfill-node failures in browser builds. ' +
'Replace with browser-compatible alternatives or wrap in an is-browser guard.',
);
}
}
/**
* @type {import('esbuild').BuildOptions}
*/
const sharedConfig = {
entryPoints: ['./src/index.ts'],
bundle: true,
minify: true,
// sourcemap: true,
external: [
'fs',
'module',
'path',
'child_process',
'os',
'crypto',
'vm',
'stream',
'node:fs/promises',
'url',
// === from package.json
...Object.keys(dependencies),
...Object.keys(devDependencies),
],
};
/**
* @type {import('esbuild').BuildOptions}
*/
const cjsConfig = {
...sharedConfig,
platform: 'node', // For CJS
outfile: './out/cjs/index.cjs',
target: 'node16',
};
/**
* @type {import('esbuild').BuildOptions}
*/
const esmConfig = {
...sharedConfig,
// TODO: Support browser
platform: 'neutral', // For ESM
outfile: './out/esm/index.mjs',
};
/**
* @type {import('esbuild').BuildOptions}
*/
const webviewConfig = {
entryPoints: [
'./src/webview/preview.tsx',
'./src/webview/backlinks.tsx',
'./src/webview/graph-view.tsx',
],
bundle: true,
minify: true,
platform: 'browser',
// outfile: './out/webview/index.js',
outdir: './out/webview',
loader: {
'.png': 'dataurl',
'.woff': 'dataurl',
'.woff2': 'dataurl',
'.eot': 'dataurl',
'.ttf': 'dataurl',
'.svg': 'dataurl',
},
plugins: [tailwindPlugin({})],
};
async function main() {
try {
if (process.argv.includes('--watch')) {
// CommonJS
const cjsContext = await context({
...cjsConfig,
sourcemap: true,
});
// ESM
const esmContext = await context({
...esmConfig,
sourcemap: true,
});
// Webview
const webviewContext = await context({
...webviewConfig,
sourcemap: true,
});
await Promise.all([
cjsContext.watch(),
esmContext.watch(),
webviewContext.watch(),
]);
} else {
// CommonJS
await build(cjsConfig);
// ESM
await build(esmConfig);
// Verify the ESM bundle is safe for browser consumers
verifyEsmForBrowser('./out/esm/index.mjs');
// Webview
await build(webviewConfig);
}
} catch (error) {
console.error(error);
}
}
main();