#!/usr/bin/env node /* eslint-env node */ const esbuild = require("esbuild"); const path = require("path"); const fs = require("fs"); const { createServer } = require("http"); const { wasmTextPlugin } = require("./scripts/esbuild/wasm-text"); const { forthPlugin } = require("./scripts/esbuild/forth"); function withWatcher(config, handleBuildFinished = () => {}, port = 8880) { const watchClients = []; createServer((req, res) => { return watchClients.push( res.writeHead(200, { "Content-Type": "text/event-stream", "Cache-Control": "no-cache", "Access-Control-Allow-Origin": "*", Connection: "keep-alive", }) ); }).listen(port); return { ...config, banner: { js: `(function () { new EventSource("http://localhost:${port}").onmessage = function() { location.reload();};})();`, }, watch: { async onRebuild(error, result) { if (error) { console.error(error); } else { // Doing this first, because this may do some ES5 transformations await handleBuildFinished(result); watchClients.forEach((res) => res.write("data: update\n\n")); watchClients.length = 0; } }, }, }; } let dev = false; let watch = false; for (const arg of process.argv.slice(2)) { switch (arg) { case "--development": dev = true; break; case "--watch": watch = true; break; } } let buildConfig = { bundle: true, logLevel: "info", entryPoints: [ path.join(__dirname, "src", "web", "shell", "shell"), path.join(__dirname, "src", "web", "tests", "tests"), path.join(__dirname, "src", "web", "benchmarks", "benchmarks"), ], entryNames: dev ? "[name]" : "[name]-c$[hash]", assetNames: "[name]-c$[hash]", // target: "es6", outdir: path.join(__dirname, "public/waforth/dist"), external: ["fs", "stream", "util", "events"], minify: !dev, loader: { ".wasm": "binary", ".js": "jsx", }, sourcemap: true, metafile: true, plugins: [wasmTextPlugin(), forthPlugin()], }; const INDEX_TEMPLATE = `