forked from MayDay-wpf/snow-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.mjs
More file actions
101 lines (93 loc) · 2.54 KB
/
Copy pathbuild.mjs
File metadata and controls
101 lines (93 loc) · 2.54 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
import * as esbuild from 'esbuild';
import {copyFileSync, existsSync, mkdirSync} from 'fs';
import {builtinModules} from 'module';
// Plugin to stub out optional dependencies
const stubPlugin = {
name: 'stub',
setup(build) {
build.onResolve({filter: /^react-devtools-core$/}, () => ({
path: 'react-devtools-core',
namespace: 'stub-ns',
}));
build.onLoad({filter: /.*/, namespace: 'stub-ns'}, () => ({
contents: 'export default {}',
}));
},
};
// Create bundle directory
if (!existsSync('bundle')) {
mkdirSync('bundle');
}
await esbuild.build({
entryPoints: ['dist/cli.js'],
bundle: true,
platform: 'node',
target: 'node16',
format: 'esm',
outfile: 'bundle/cli.mjs',
banner: {
js: `import { createRequire as _createRequire } from 'module';
import { fileURLToPath as _fileURLToPath } from 'url';
const require = _createRequire(import.meta.url);
const __filename = _fileURLToPath(import.meta.url);
const __dirname = _fileURLToPath(new URL('.', import.meta.url));
// Polyfill for undici's web API dependencies
// undici uses File, Blob, etc. which are only available in Node.js 20+
// For Node.js 16-18, we provide minimal polyfills
if (typeof globalThis.File === 'undefined') {
globalThis.File = class File {
constructor(bits, name, options) {
this.bits = bits;
this.name = name;
this.options = options;
}
};
}
if (typeof globalThis.FormData === 'undefined') {
globalThis.FormData = class FormData {
constructor() {
this._data = new Map();
}
append(key, value) {
this._data.set(key, value);
}
get(key) {
return this._data.get(key);
}
};
}`,
},
external: [
// Only Node.js built-in modules should be external
...builtinModules,
...builtinModules.map(m => `node:${m}`),
// Optional native dependencies (dynamically imported in code)
'sharp',
// SSH2 has native bindings that cannot be bundled
'ssh2',
'cpu-features',
// Note: katex and markdown-it-math are bundled (not external)
],
plugins: [stubPlugin],
minify: false,
sourcemap: false,
metafile: true,
logLevel: 'info',
});
// Copy WASM files
copyFileSync(
'node_modules/sql.js/dist/sql-wasm.wasm',
'bundle/sql-wasm.wasm',
);
copyFileSync(
'node_modules/tiktoken/tiktoken_bg.wasm',
'bundle/tiktoken_bg.wasm',
);
// Copy PDF.js worker file for PDF parsing
copyFileSync(
'node_modules/pdfjs-dist/build/pdf.worker.mjs',
'bundle/pdf.worker.mjs',
);
// Copy package.json to bundle directory for version reading
copyFileSync('package.json', 'bundle/package.json');
console.log('✓ Bundle created successfully');