Skip to content

Commit 797964d

Browse files
committed
WIP - Switch to esbuild for watch bundling
1 parent 481e4c3 commit 797964d

File tree

3 files changed

+639
-97
lines changed

3 files changed

+639
-97
lines changed

esbuild.mjs

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import * as esbuild from 'esbuild'
2+
import yargs from "yargs";
3+
import { hideBin } from "yargs/helpers";
4+
5+
const myYargs = yargs(hideBin(process.argv));
6+
myYargs
7+
.usage("Usage: esbuild [options]")
8+
.demandCommand(0, 0)
9+
.example("sfx-wasm --watch", "Bundle and watch for changes")
10+
.option("d", {
11+
alias: "debug",
12+
describe: "Debug build",
13+
boolean: true
14+
})
15+
.option("w", {
16+
alias: "watch",
17+
describe: "Watch for changes",
18+
boolean: true
19+
})
20+
.help("h")
21+
.alias("h", "help")
22+
.epilog("https://github.com/hpcc-systems/hpcc-js-wasm")
23+
;
24+
const argv = await myYargs.argv;
25+
26+
async function build(config) {
27+
esbuild.build(config);
28+
}
29+
30+
async function watch(config) {
31+
const ctx = await esbuild.context({
32+
...config,
33+
plugins: [{
34+
name: 'rebuild-notify',
35+
setup(build) {
36+
build.onEnd(result => {
37+
console.log(`Built ${config.outfile}`);
38+
})
39+
},
40+
}]
41+
});
42+
await ctx.watch();
43+
}
44+
45+
async function browserTpl(input, umdOutput, esOutput, globalName = undefined, external = []) {
46+
const entryPoints = [input + ".js"];
47+
(argv.watch ? watch : build)({
48+
entryPoints,
49+
outfile: esOutput + '.js',
50+
sourcemap: true,
51+
platform: "browser",
52+
format: "esm",
53+
bundle: true,
54+
minify: !argv.debug,
55+
external
56+
});
57+
if (global) {
58+
(argv.watch ? watch : build)({
59+
entryPoints,
60+
outfile: umdOutput + '.js',
61+
sourcemap: true,
62+
platform: "browser",
63+
format: "iife",
64+
globalName,
65+
bundle: true,
66+
minify: !argv.debug,
67+
external
68+
});
69+
}
70+
}
71+
72+
function nodeTpl(input, esOutput, external = []) {
73+
const entryPoints = [input + ".js"];
74+
(argv.watch ? watch : build)({
75+
entryPoints,
76+
outfile: esOutput + '.mjs',
77+
sourcemap: true,
78+
platform: "node",
79+
format: "esm",
80+
bundle: true,
81+
minify: !argv.debug,
82+
external
83+
});
84+
(argv.watch ? watch : build)({
85+
entryPoints,
86+
outfile: esOutput + '.cjs',
87+
sourcemap: true,
88+
platform: "node",
89+
format: "cjs",
90+
bundle: true,
91+
minify: !argv.debug,
92+
external
93+
});
94+
}
95+
96+
function bothTpl(input, umdOutput, esOutput, globalName = undefined, external = []) {
97+
browserTpl(input, umdOutput, esOutput, globalName, external);
98+
nodeTpl(input, esOutput, external);
99+
}
100+
101+
bothTpl("lib-esm/index", "dist/index.umd", "dist/index", "hpccjs_wasm", ["./base91.js", "./duckdb.js", "./expat.js", "./graphviz.js", "./zstd.js"]);
102+
bothTpl("lib-esm/base91", "dist/base91.umd", "dist/base91", "hpccjs_wasm_base91");
103+
bothTpl("lib-esm/duckdb", "dist/duckdb.umd", "dist/duckdb", "hpccjs_wasm_duckdb");
104+
bothTpl("lib-esm/graphviz", "dist/graphviz.umd", "dist/graphviz", "hpccjs_wasm_graphviz");
105+
bothTpl("lib-esm/expat", "dist/expat.umd", "dist/expat", "hpccjs_wasm_expat");
106+
bothTpl("lib-esm/zstd", "dist/zstd.umd", "dist/zstd", "hpccjs_wasm_zstd");
107+
108+
browserTpl("lib-esm/__tests__/index-browser", "dist-test/index.umd", "dist-test/index");
109+
browserTpl("lib-esm/__tests__/worker-browser", "dist-test/worker.umd", "dist-test/worker");
110+
nodeTpl("lib-esm/__tests__/index-node", "dist-test/index.node");
111+
nodeTpl("lib-esm/__tests__/worker-node", "dist-test/worker.node");
112+
113+
114+
115+
// await esbuild.build({
116+
// entryPoints: ['./lib-esm/base91.js'],
117+
// bundle: true,
118+
// outfile: './dist/base91.js',
119+
// })
120+
121+
// await esbuild.build({
122+
// entryPoints: ['./lib-esm/base91.js'],
123+
// bundle: true,
124+
// platform: 'node',
125+
// format: 'cjs',
126+
// outfile: './dist/base91.cjs',
127+
// })
128+
129+
// await esbuild.build({
130+
// entryPoints: ['./lib-esm/base91.js'],
131+
// bundle: true,
132+
// platform: 'node',
133+
// outfile: './dist/base91.mjs',
134+
// })

0 commit comments

Comments
 (0)