-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.js
139 lines (125 loc) · 3.53 KB
/
build.js
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
import fs from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';
import getPort from 'get-port';
import esbuild from 'esbuild';
import { clean } from 'esbuild-plugin-clean';
import esbuildPluginTsc from 'esbuild-plugin-tsc';
import inlineWorkerPlugin from 'esbuild-plugin-inline-worker';
import { polyfillNode } from 'esbuild-plugin-polyfill-node';
import { wasmLoader } from 'esbuild-plugin-wasm';
import copy from 'esbuild-plugin-copy';
import { minify } from 'terser';
import { handleWasmUrl } from './handleWasmUrl.cjs';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const args = process.argv.slice(2);
const watchMode = args.includes('--watch');
const production = process.env.NODE_ENV === 'production' && !watchMode;
export async function buildWithWatch() {
await fs.mkdir(path.resolve(__dirname, 'dist'), { recursive: true });
const livereloadPort = await getPort({ port: 53100 });
const workerBuildOptions = {
entryPoints: ['src/mine.worker.ts'],
bundle: true,
format: 'iife',
platform: 'browser',
target: ['esnext'],
minify: true,
plugins: [
handleWasmUrl,
wasmLoader(),
],
loader: {
'.ts': 'ts',
'.js': 'js',
'.wasm': 'file',
},
define: {
'import.meta.url': 'self.location.href',
},
outdir: 'dist',
write: true,
};
let workerResult;
try {
workerResult = await esbuild.build(workerBuildOptions);
} catch (error) {
console.error('Worker build failed:', error);
process.exit(1);
}
if (!workerResult || workerResult.errors?.length > 0) {
console.error('Worker build output is missing or contains errors.');
process.exit(1);
}
const workerFilePath = path.resolve(__dirname, 'dist', 'mine.worker.js');
const workerCode = await fs.readFile(workerFilePath, 'utf8');
const minifiedWorkerCode = await minify(workerCode);
if (minifiedWorkerCode.error) {
console.error('Terser minification failed:', minifiedWorkerCode.error);
process.exit(1);
}
await fs.writeFile(workerFilePath, minifiedWorkerCode.code);
const distPath = path.resolve(__dirname, 'dist');
await fs.mkdir(distPath, { recursive: true });
const mainBuildOptions = {
entryPoints: ['src/index.ts'],
outdir: 'dist',
bundle: true,
splitting: true,
format: 'esm',
platform: 'browser',
target: ['esnext'],
sourcemap: !production,
minify: production,
plugins: [
esbuildPluginTsc({
force: true,
}),
handleWasmUrl,
inlineWorkerPlugin({
entryPoints: ['dist/mine.worker.js'],
minify: true,
target: 'esnext',
format: 'iife',
}),
polyfillNode({
polyfills: {
fs: false,
path: false,
util: true,
assert: true,
stream: true,
os: true,
},
}),
copy({
assets: {
from: '../node_modules/@notemine/core/notemine_bg.wasm',
to: 'dist',
},
}),
].filter(Boolean),
loader: {
'.ts': 'ts',
'.js': 'js',
'.wasm': 'file',
},
define: {
'import.meta.url': 'self.location.href',
},
};
try {
if (watchMode) {
const mainContext = await esbuild.context(mainBuildOptions);
await mainContext.watch();
console.log('Watching for changes...');
} else {
await esbuild.build(mainBuildOptions);
console.log('Build completed.');
}
} catch (error) {
console.error('Main build failed:', error);
process.exit(1);
}
}
buildWithWatch();