-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbundle.mts
65 lines (55 loc) · 1.47 KB
/
bundle.mts
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
import { promises } from "fs";
import { parseArgs } from "util";
const DISTRIBUTION_DIR = "./dist";
const WATCHED_FILE_PATHS = ["src/", "assets/"];
const { values, positionals: _ } = parseArgs({
args: Bun.argv,
options: {
watch: {
type: "boolean",
},
},
strict: true,
allowPositionals: true,
});
async function compile() {
await promises.rm(DISTRIBUTION_DIR, { recursive: true, force: true });
const result = await Bun.build({
entrypoints: [
"./src/background/background.mts",
"./src/options/options.html",
"./src/offscreen/offscreen.html",
],
naming: "[name].[ext]", // Keep all entrypoints at the root
html: true,
experimentalCss: true,
outdir: DISTRIBUTION_DIR,
sourcemap: "linked",
target: "browser",
minify: true,
});
if (!result.success) {
console.error("Build failed");
for (const message of result.logs) {
console.error(message);
}
}
await promises.cp("assets/", DISTRIBUTION_DIR, { recursive: true });
}
await compile();
if (values.watch) {
const watcher = promises.watch(import.meta.dir, { recursive: true });
process.on("SIGINT", () => {
// close watcher when Ctrl-C is pressed
console.log("Closing watcher...");
process.exit(0);
});
for await (const event of watcher) {
for (const prefix of WATCHED_FILE_PATHS) {
if (event.filename?.startsWith(prefix)) {
console.log(`${event.filename} changed`);
await compile();
}
}
}
}