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