Repository navigation
Expand file tree
/
Copy pathbuild.js
More file actions
184 lines (167 loc) · 4.43 KB
/
Copy pathbuild.js
File metadata and controls
184 lines (167 loc) · 4.43 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
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 cliConfig = {
...sharedConfig,
entryPoints: ['./src/cli/index.ts'],
platform: 'node',
outfile: './out/cli/index.cjs',
target: 'node18',
// Keep stack traces and error messages readable for a CLI.
minify: false,
};
/**
* @type {import('esbuild').BuildOptions}
*/
const webviewConfig = {
entryPoints: [
'./src/webview/preview.tsx',
'./src/webview/backlinks.tsx',
'./src/webview/graph-view.tsx',
'./src/server-app/server-app.tsx',
],
bundle: true,
minify: true,
platform: 'browser',
// Entries live in src/webview and src/server-app; with an explicit outbase
// they land at out/webview/preview.js and out/server-app/server-app.js.
// (The standalone wiki inlines the server-app bundle as its shell.)
outbase: './src',
outdir: './out',
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,
});
// CLI
const cliContext = await context({
...cliConfig,
sourcemap: true,
});
// Webview
const webviewContext = await context({
...webviewConfig,
sourcemap: true,
});
await Promise.all([
cjsContext.watch(),
esmContext.watch(),
cliContext.watch(),
webviewContext.watch(),
]);
} else {
// CommonJS
await build(cjsConfig);
// CLI
await build(cliConfig);
// 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();