diff --git a/package.json b/package.json index 50c64c4..c0ccb4c 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,8 @@ "main": "src/index.mjs", "scripts": { "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": { "type": "git", diff --git a/src/esbuild.mjs b/src/esbuild.mjs index 06c9130..c449aab 100644 --- a/src/esbuild.mjs +++ b/src/esbuild.mjs @@ -57,10 +57,8 @@ async function do_html() { } } -(async () => { - "use strict"; - - const result = await esbuild.build({ +function make_context() { + return { entryPoints: [ "./app.mjs", "./app.css", @@ -79,10 +77,37 @@ async function do_html() { ".ttf": "file", }, external: ["fs", "path"], - }); - if (result.errors.length > 0 || result.warnings.length > 0) - console.log(result); + plugins: [ + { + name: "copy-html", + setup(build) { + build.onEnd(_result => { + do_html(); + }) + } + } + ] + }; +} - await 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) + console.log(result); + break; + + 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)); -})(); \ No newline at end of file +})(); +