forked from cms/tibi-svelte-starter
62 lines
1.7 KiB
JavaScript
62 lines
1.7 KiB
JavaScript
const esbuild = require("esbuild")
|
|
const fs = require("fs")
|
|
const path = require("path")
|
|
|
|
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)
|
|
if (config.options.metafile) {
|
|
fs.writeFileSync(
|
|
path.dirname(config.options.outfile) + "/meta.json",
|
|
JSON.stringify(buildResults.metafile, null, 4)
|
|
)
|
|
}
|
|
} 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()
|
|
}
|