From 499b57b84247442beff8a89226d1a115fd9edbe5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A2=E3=83=AC=E3=82=AF=E3=82=B5=E3=83=B3=E3=83=80?= =?UTF-8?q?=E3=83=BC=2Eeth?= Date: Wed, 9 Oct 2024 06:06:13 +0900 Subject: [PATCH] refactor: esbuild scripts --- build/esbuild-build.ts | 42 +++++++++++++++++++---------------------- build/esbuild-server.ts | 22 +++++++++++---------- 2 files changed, 31 insertions(+), 33 deletions(-) diff --git a/build/esbuild-build.ts b/build/esbuild-build.ts index 3004b1d..00fc141 100644 --- a/build/esbuild-build.ts +++ b/build/esbuild-build.ts @@ -1,33 +1,29 @@ -import esbuild from "esbuild"; -const typescriptEntries = ["static/main.ts"]; -// const cssEntries = ["static/style.css"]; -const entries = [ - ...typescriptEntries, - // ...cssEntries -]; +import esbuild, { BuildOptions } from "esbuild"; -export const esBuildContext: esbuild.BuildOptions = { +const ENTRY_POINTS = { + typescript: ["static/main.ts"], + // css: ["static/style.css"], +}; + +const DATA_URL_LOADERS = [".png", ".woff", ".woff2", ".eot", ".ttf", ".svg"]; + +export const esbuildOptions: BuildOptions = { sourcemap: true, - entryPoints: entries, + entryPoints: [...ENTRY_POINTS.typescript /* ...ENTRY_POINTS.css */], bundle: true, minify: false, - loader: { - ".png": "dataurl", - ".woff": "dataurl", - ".woff2": "dataurl", - ".eot": "dataurl", - ".ttf": "dataurl", - ".svg": "dataurl", - }, + loader: Object.fromEntries(DATA_URL_LOADERS.map((ext) => [ext, "dataurl"])), outdir: "static/dist", }; -esbuild - .build(esBuildContext) - .then(() => { +async function runBuild() { + try { + await esbuild.build(esbuildOptions); console.log("\tesbuild complete"); - }) - .catch((err) => { + } catch (err) { console.error(err); process.exit(1); - }); + } +} + +void runBuild(); diff --git a/build/esbuild-server.ts b/build/esbuild-server.ts index bb992ae..0ba3c3f 100644 --- a/build/esbuild-server.ts +++ b/build/esbuild-server.ts @@ -1,18 +1,20 @@ import esbuild from "esbuild"; -import { esBuildContext } from "./esbuild-build"; +import { esbuildOptions } from "./esbuild-build"; -(async () => { - await server(); -})().catch((error) => { - console.error("Unhandled error:", error); - process.exit(1); -}); +void (async () => { + try { + await server(); + } catch (error) { + console.error("Unhandled error:", error); + process.exit(1); + } +})(); export async function server() { - const _context = await esbuild.context(esBuildContext); - const { port } = await _context.serve({ + const context = await esbuild.context(esbuildOptions); + const { host, port } = await context.serve({ servedir: "static", port: 8080, }); - console.log(`http://localhost:${port}`); + console.log(`Server running at http://${host}:${port}`); }