52 lines
1.4 KiB
JavaScript
52 lines
1.4 KiB
JavaScript
|
const esbuild = require("esbuild")
|
||
|
const config = require(process.cwd() +
|
||
|
(process.argv?.length > 3 ? "/" + process.argv[3] : "/esbuild.config.js"))
|
||
|
const { watch } = require("chokidar")
|
||
|
|
||
|
function log(str, clear) {
|
||
|
if (clear) {
|
||
|
process.stdout.cursorTo(0, 0)
|
||
|
process.stdout.clearScreenDown()
|
||
|
}
|
||
|
console.log("\x1b[36m%s\x1b[0m", str)
|
||
|
}
|
||
|
|
||
|
let buildResults
|
||
|
|
||
|
async function build(catchError) {
|
||
|
log((buildResults ? "re" : "") + "building...")
|
||
|
const timerStart = Date.now()
|
||
|
try {
|
||
|
buildResults = buildResults
|
||
|
? await buildResults.rebuild()
|
||
|
: await esbuild.build(config.options)
|
||
|
} catch (e) {
|
||
|
console.log(e)
|
||
|
if (!catchError) throw e
|
||
|
}
|
||
|
const timerEnd = Date.now()
|
||
|
log(`built in ${timerEnd - timerStart}ms.`)
|
||
|
}
|
||
|
|
||
|
switch (process.argv?.length > 2 ? process.argv[2] : "build") {
|
||
|
case "serve":
|
||
|
console.log("\x1b[36m%s\x1b[0mserving...")
|
||
|
esbuild.serve(config.serve, config.options).catch((err) => {
|
||
|
console.error(err)
|
||
|
process.exit(1)
|
||
|
})
|
||
|
break
|
||
|
case "watch":
|
||
|
config.options.incremental = true
|
||
|
build(true)
|
||
|
const watcher = watch(config.watch.path)
|
||
|
log("watching files...")
|
||
|
watcher.on("change", function (path) {
|
||
|
log(`${path} changed`, true)
|
||
|
build(true)
|
||
|
})
|
||
|
break
|
||
|
default:
|
||
|
build()
|
||
|
}
|