-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesbuild.js
77 lines (68 loc) · 1.79 KB
/
esbuild.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import esbuild from "esbuild";
import { sassPlugin } from "esbuild-sass-plugin";
import autoprefixer from "autoprefixer";
import postcss from "postcss";
import liveServer from "live-server";
import path from "path";
import { fileURLToPath } from "url";
const isDev = process.env.NODE_ENV === "development";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
/** @type {import('esbuild').BuildOptions} */
const buildOptions = {
entryPoints: ["src/main.scss", "src/main.js"],
outdir: "src/public",
bundle: true,
minify: !isDev,
sourcemap: isDev,
metafile: true,
loader: {
".png": "file",
".jpg": "file",
".svg": "file",
".gif": "file",
".woff": "file",
".woff2": "file",
},
plugins: [
sassPlugin({
sourceMap: isDev,
async transform(source) {
const { css } = await postcss([autoprefixer]).process(source);
return css;
},
loadPaths: [path.resolve(__dirname, "src")],
}),
],
};
const liveServerParams = {
port: 3000,
host: "localhost",
root: "",
open: true,
file: "index.html",
wait: 100,
logLevel: 2,
};
async function runBuild() {
try {
const context = await esbuild.context(buildOptions);
const result = await context.rebuild();
console.log("⚡ Build complete! ⚡");
if (result.metafile) {
console.log("📦 Bundle size:", await esbuild.analyzeMetafile(result.metafile));
}
if (isDev) {
await context.watch();
console.log("👀 Watching for changes...");
liveServer.start(liveServerParams);
console.log("🌐 Live server started on http://localhost:3000");
} else {
await context.dispose();
}
} catch (error) {
console.error("Build failed:", error);
process.exit(1);
}
}
void runBuild();