-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathesbuild.mjs
136 lines (123 loc) · 4.31 KB
/
esbuild.mjs
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
import * as process from "process";
import { readFileSync } from "fs";
import * as esbuild from "esbuild";
import { umdWrapper } from "esbuild-plugin-umd-wrapper";
import { rebuildLogger, sfxWasm } from "@hpcc-js/esbuild-plugins";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
const pkg = JSON.parse(readFileSync("./package.json", "utf8"));
const NODE_MJS = pkg.type === "module" ? "js" : "mjs";
const NODE_CJS = pkg.type === "module" ? "cjs" : "js";
const myYargs = yargs(hideBin(process.argv));
myYargs
.usage("Usage: node esbuild.mjs [options]")
.demandCommand(0, 0)
.example("node esbuild.mjs --watch", "Bundle and watch for changes")
.option("mode", {
alias: "m",
describe: "Build mode",
choices: ["development", "production"],
default: "production"
})
.option("w", {
alias: "watch",
describe: "Watch for changes",
type: "boolean"
})
.help("h")
.alias("h", "help")
.epilog("https://github.com/hpcc-systems/hpcc-js-wasm")
;
const argv = await myYargs.argv;
const isDevelopment = argv.mode === "development";
const isProduction = !isDevelopment;
const isWatch = argv.watch;
// helpers ---
function build(config) {
isDevelopment && console.log("Start: ", config.entryPoints[0], config.outfile);
return esbuild.build({
...config,
sourcemap: "linked",
plugins: [
...(config.plugins ?? []),
sfxWasm()
]
}).then(() => {
isDevelopment && console.log("Stop: ", config.entryPoints[0], config.outfile);
});
}
async function watch(config) {
await build(config);
return esbuild.context({
...config,
sourcemap: "external",
plugins: [
...(config.plugins ?? []),
rebuildLogger(config),
sfxWasm()
]
}).then(ctx => {
return ctx.watch();
});
}
function buildWatch(config) {
return isWatch ? watch(config) : build(config);
}
function browserTpl(input, output, format, globalName = "", external = []) {
return buildWatch({
entryPoints: [input],
outfile: `${output}.${format === "esm" ? "js" : "umd.js"}`,
platform: "browser",
target: "es2022",
format,
globalName,
bundle: true,
minify: isProduction,
external,
plugins: format === "umd" ? [umdWrapper()] : []
});
}
function browserBoth(input, output, globalName = undefined, external = []) {
return Promise.all([
browserTpl(input, output, "esm", globalName, external),
browserTpl(input, output, "umd", globalName, external)
]);
}
function nodeTpl(input, output, format, external = []) {
return buildWatch({
entryPoints: [input],
outfile: `${output}.${format === "esm" ? NODE_MJS : NODE_CJS}`,
platform: "node",
target: "node20",
format,
bundle: true,
minify: isProduction,
external
});
}
function nodeBoth(input, output, external = []) {
return Promise.all([
nodeTpl(input, output, "esm", external),
nodeTpl(input, output, "cjs", external)
]);
}
function bothTpl(input, output, globalName = undefined, external = []) {
return Promise.all([
browserBoth(input, output, globalName, external),
nodeTpl(input, output, "cjs", external)
]);
}
// config ---
await Promise.all([
bothTpl("src-ts/base91.ts", "dist/base91", 'window["@hpcc-js/wasm"]'),
bothTpl("src-ts/duckdb.ts", "dist/duckdb", 'window["@hpcc-js/wasm"]'),
bothTpl("src-ts/graphviz.ts", "dist/graphviz", 'window["@hpcc-js/wasm"]'),
bothTpl("src-ts/expat.ts", "dist/expat", 'window["@hpcc-js/wasm"]'),
bothTpl("src-ts/zstd.ts", "dist/zstd", 'window["@hpcc-js/wasm"]')
]);
await bothTpl("src-ts/index.ts", "dist/index", 'window["@hpcc-js/wasm"]', ["./base91.js", "./duckdb.js", "./expat.js", "./graphviz.js", "./zstd.js"]);
browserBoth("src-ts/__tests__/index-browser.ts", "dist-test/index", 'window["@hpcc-js/wasm-test"]');
browserBoth("src-ts/__tests__/worker-browser.ts", "dist-test/worker", 'window["@hpcc-js/wasm-test"]');
nodeBoth("src-ts/__tests__/index-node.ts", "dist-test/index.node");
nodeBoth("src-ts/__tests__/worker-node.ts", "dist-test/worker.node");
nodeTpl("src-ts/__bin__/dot-wasm.ts", "bin/dot-wasm", "esm", ["@hpcc-js/wasm/graphviz"]);