esbuild: add watching

This commit is contained in:
Starbeamrainbowlabs 2023-03-04 22:11:16 +00:00
parent 418e4344b6
commit ae595b45e6
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
2 changed files with 36 additions and 10 deletions

View file

@ -5,7 +5,8 @@
"main": "src/index.mjs", "main": "src/index.mjs",
"scripts": { "scripts": {
"test": "echo \"No tests have been written yet.\"", "test": "echo \"No tests have been written yet.\"",
"build": "node ./src/esbuild.mjs" "build": "node ./src/esbuild.mjs",
"watch": "ES_MODE=\"watch\" node ./src/esbuild.mjs"
}, },
"repository": { "repository": {
"type": "git", "type": "git",

View file

@ -57,10 +57,8 @@ async function do_html() {
} }
} }
(async () => { function make_context() {
"use strict"; return {
const result = await esbuild.build({
entryPoints: [ entryPoints: [
"./app.mjs", "./app.mjs",
"./app.css", "./app.css",
@ -79,10 +77,37 @@ async function do_html() {
".ttf": "file", ".ttf": "file",
}, },
external: ["fs", "path"], external: ["fs", "path"],
}); plugins: [
{
name: "copy-html",
setup(build) {
build.onEnd(_result => {
do_html();
})
}
}
]
};
}
(async () => {
"use strict";
switch(process.env.ES_MODE ?? "build") {
case "build":
const result = await esbuild.build(make_context());
if (result.errors.length > 0 || result.warnings.length > 0) if (result.errors.length > 0 || result.warnings.length > 0)
console.log(result); console.log(result);
break;
await do_html(); case "watch":
const ctx = await esbuild.context(make_context());
await ctx.watch();
console.log(`>>> Watching for changes`);
break;
}
// await do_html();
// console.log(await esbuild.analyzeMetafile(result.metafile)); // console.log(await esbuild.analyzeMetafile(result.metafile));
})(); })();