-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.js
75 lines (70 loc) · 1.95 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
// Copyright (C) 2024, Nuklai. All rights reserved.
// See the file LICENSE for licensing terms.
import { build } from "esbuild";
import { NodeModulesPolyfillPlugin } from "@esbuild-plugins/node-modules-polyfill";
const commonOptions = {
bundle: true,
sourcemap: true,
target: "es2020",
define: {
"process.env.NODE_ENV": JSON.stringify("production"),
global: "globalThis",
},
inject: ["./polyfills.js"],
logLevel: "info",
external: ["ws"],
};
const builds = [
// ESM build (for modern environments including Next.js)
{
...commonOptions,
entryPoints: ["./src/index.ts"],
format: "esm",
outfile: "dist/index.esm.js",
platform: "browser",
plugins: [
NodeModulesPolyfillPlugin(),
{
name: 'node-globals',
setup(build) {
build.onResolve({ filter: /^(process|buffer|crypto)$/ }, args => {
return { path: args.path, namespace: 'node-globals' }
})
build.onLoad({ filter: /.*/, namespace: 'node-globals' }, () => {
return { contents: 'export default {}' }
})
},
},
],
},
// CJS build (for Node.js and older bundlers)
{
...commonOptions,
entryPoints: ["./src/index.ts"],
format: "cjs",
outfile: "dist/index.cjs.js",
platform: "node",
}
];
async function runBuilds() {
for (const config of builds) {
try {
console.log(`Starting build for ${config.outfile}...`);
const result = await build(config);
console.log(`Build completed for ${config.outfile}`);
if (result.warnings.length > 0) {
console.warn('Warnings:', result.warnings);
}
if (result.errors.length > 0) {
console.error('Errors:', result.errors);
}
} catch (error) {
console.error(`Build failed for ${config.outfile}:`, error);
process.exit(1);
}
}
}
runBuilds().catch(error => {
console.error('Unexpected error during build process:', error);
process.exit(1);
});