#!/usr/bin/env node const p = require('path') const { build, cliopts } = require('estrella') const fs = require('fs/promises') const copy = require('recursive-copy') const [opts, args] = cliopts.parse( ['host', 'Development server: Hostname', ''], ['port', 'Development server: Port'] ) const base = process.cwd() const outdir = p.join(base, 'build') const staticdir = p.join(base, 'static') const entry = p.join(base, 'src/index.js') const outfile = p.join(outdir, 'bundle.js') let devServerMessage let firstRun = true build({ entry, outfile, // external: ['http', 'https'], bundle: true, sourcemap: true, minify: false, loader: { '.js': 'jsx', '.woff': 'file', '.woff2': 'file' }, // This banner fixes some modules that were designed for Node.js // to run in the browser by providing minimal shims. banner: ` var global = window; window.process = { title: "browser", env: {}, nextTick: function (cb, ...args) { Promise.resolve().then(() => cb(...args)) } }; `, define: { 'process.title': 'browser', 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'development') }, // dgram external needed for osc-js to compile external: ['dgram'], onEnd }) // Run a local web server with livereload when -watch is set if (cliopts.watch) { const instant = require('instant') const express = require('express') const port = cliopts.port || 3000 const host = opts.host || 'localhost' const app = express() app.use(instant({ root: outdir })) app.listen(port, host, () => { devServerMessage = `Listening on http://${host}:${port} and watching for changes ...` console.log(devServerMessage) }) } async function onEnd () { if (!firstRun) return firstRun = false if (devServerMessage) console.log(devServerMessage) try { const stat = await fs.stat(staticdir) if (!stat.isDirectory()) return await copy(staticdir, outdir) } catch (err) {} }