-
Notifications
You must be signed in to change notification settings - Fork 2
/
serve.js
41 lines (40 loc) · 1.32 KB
/
serve.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const { build } = require("esbuild")
const chokidar = require("chokidar")
const liveServer = require("live-server")
;(async () => {
// `esbuild` bundler for JavaScript / TypeScript.
const builder = await build({
// Bundles JavaScript.
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") },
// Bundles JavaScript from (see `outfile`).
entryPoints: ["src/index.ts"],
// Uses incremental compilation (see `chokidar.on`).
incremental: true,
// Removes whitespace, etc. depending on `NODE_ENV=...`.
minify: process.env.NODE_ENV === "production",
// Bundles JavaScript to (see `entryPoints`).
outfile: "public/script.js",
})
// `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",
})
})()