-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.js
42 lines (39 loc) · 1.17 KB
/
build.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
import { readFile } from 'fs/promises'
import { optimize } from 'svgo'
import { resolve } from 'path';
import esbuild from 'esbuild';
import esbuildMacros from 'esbuild-plugin-macros';
import esbuildHtmlMinify from 'esbuild-plugin-html-minify';
const dev = process.argv[2] === "dev";
const ctx = await esbuild.context({
entryPoints: ["./src/index.html", './src/index.jsx', './src/styles.css'],
outdir: "./build",
jsxFactory: "__jsx",
jsxFragment: "__fragment",
loader: { ".svg": "file", '.html': 'copy' },
format: "esm",
bundle: true,
sourcemap: true,
minify: !dev,
plugins: [esbuildMacros, esbuildHtmlMinify(), {
name: 'svg',
setup(build) {
build.onResolve({ filter: /\.svg$/ }, args => ({ path: resolve(args.resolveDir, args.path), namespace: 'svg' }));
build.onLoad({ filter: /.*/, namespace: 'svg' }, async args => {
const contents = await readFile(args.path, 'utf8');
return {
contents: optimize(contents).data,
loader: 'file',
};
});
}
}]
});
if (dev) {
await ctx.watch();
await ctx.serve()
console.log('Listening on http://localhost:8000')
} else {
await ctx.rebuild()
process.exit()
}