Skip to content

Instantly share code, notes, and snippets.

@adamjarling
Last active January 3, 2022 21:37
Show Gist options
  • Save adamjarling/aef3899adbb856c0c67d974655907092 to your computer and use it in GitHub Desktop.
Save adamjarling/aef3899adbb856c0c67d974655907092 to your computer and use it in GitHub Desktop.
const { build } = require("esbuild");
const chokidar = require("chokidar");
const liveServer = require("live-server");
(async () => {
const builder = await build({
bundle: true,
// Defines env variables for bundled JavaScript; here `process.env.NODE_ENV`
// is propagated with a fallback.
define: {
"process.env.NODE_ENV": JSON.stringify(
process.env.NODE_ENV || "development"
),
},
entryPoints: ["src/dev.tsx"],
// Uses incremental compilation (see `chokidar.on`).
incremental: true,
// Removes whitespace, etc. depending on `NODE_ENV=...`.
minify: process.env.NODE_ENV === "production",
outfile: "public/script.js",
sourcemap: true,
});
// `chokidar` watcher source changes.
chokidar
// Watches TypeScript and React TypeScript.
.watch("src/**/*.{ts,tsx}", {
interval: 0, // No delay
})
// Rebuilds esbuild (incrementally -- see `build.incremental`).
.on("all", () => {
builder.rebuild();
});
// `liveServer` local server for hot reload.
liveServer.start({
// Opens the local server on start.
open: true,
// Uses `PORT=...` or 8080 as a fallback.
port: +process.env.PORT || 8080,
// Uses `public` as the local server folder.
root: "public",
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment