-
Notifications
You must be signed in to change notification settings - Fork 0
/
dev.build.mjs
69 lines (59 loc) · 1.72 KB
/
dev.build.mjs
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
import * as esbuild from 'esbuild'
import { htmlPlugin } from '@craftamap/esbuild-plugin-html';
import config from './common.build.mjs';
import fs from "fs";
let startTime;
if (fs.existsSync('dist')) {
fs.rmSync("dist", { recursive: true });
}
fs.mkdirSync("dist");
let devPlugin = {
name: 'example',
setup(build) {
build.onStart(() => {
startTime = process.hrtime();
console.log("Build started");
});
build.onEnd(result => {
let diff = process.hrtime(startTime);
let diffSeconds = ((diff[0] * 1e9) + diff[1]) / 1e9;
console.log(`Build finished in ${diffSeconds.toFixed(2)} seconds with ${result.errors.length} errors`);
});
},
}
let ctx = await esbuild.context({
...config,
plugins: [
devPlugin,
htmlPlugin({
files: [
{
entryPoints: [
'app.jsx',
],
filename: 'index.html',
htmlTemplate: `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div id="root">
</div>
</body>
<script>new EventSource('/esbuild').addEventListener('change', () => location.reload())</script>
<script src="./index.js"></script>
</html>
`,
}
]
})
]
});
await ctx.watch();
let { host, port } = await ctx.serve({
servedir: 'dist',
});
console.log(`Server is running at http://${host}:${port}`);