Skip to content

Commit

Permalink
refactor: esbuild scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
0x4007 committed Oct 8, 2024
1 parent 858ab74 commit 499b57b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 33 deletions.
42 changes: 19 additions & 23 deletions build/esbuild-build.ts
Original file line number Diff line number Diff line change
@@ -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();
22 changes: 12 additions & 10 deletions build/esbuild-server.ts
Original file line number Diff line number Diff line change
@@ -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}`);
}

0 comments on commit 499b57b

Please sign in to comment.