-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
65 lines (55 loc) · 1.93 KB
/
Copy pathbuild.js
File metadata and controls
65 lines (55 loc) · 1.93 KB
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 esbuild from "esbuild";
import { copyFileSync, mkdirSync, readFileSync, writeFileSync } from "fs";
import { join } from "path";
const watch = process.argv.includes("--watch");
const targetArg = process.argv.find((a) => a.startsWith("--target="));
const targets = targetArg
? [targetArg.split("=")[1]]
: ["chrome", "firefox"];
const entryPoints = [
{ in: "src/background/service-worker.ts", out: "background/service-worker" },
{ in: "src/popup/popup.ts", out: "popup/popup" },
{ in: "src/options/options.ts", out: "options/options" },
];
function copyStaticAssets(target) {
const out = `dist/${target}`;
mkdirSync(`${out}/icons`, { recursive: true });
mkdirSync(`${out}/popup`, { recursive: true });
mkdirSync(`${out}/options`, { recursive: true });
copyFileSync("src/icons/icon-48.png", `${out}/icons/icon-48.png`);
copyFileSync("src/icons/icon-96.png", `${out}/icons/icon-96.png`);
copyFileSync("src/popup/popup.html", `${out}/popup/popup.html`);
copyFileSync("src/popup/popup.css", `${out}/popup/popup.css`);
copyFileSync("src/options/options.html", `${out}/options/options.html`);
copyFileSync("src/options/options.css", `${out}/options/options.css`);
const manifest = JSON.parse(
readFileSync(`manifests/manifest.${target}.json`, "utf8")
);
writeFileSync(join(out, "manifest.json"), JSON.stringify(manifest, null, 2));
}
async function build(target) {
const outdir = `dist/${target}`;
mkdirSync(outdir, { recursive: true });
const ctx = await esbuild.context({
entryPoints,
outdir,
bundle: true,
format: "esm",
target: "es2022",
platform: "browser",
sourcemap: watch ? "inline" : false,
minify: !watch,
});
copyStaticAssets(target);
if (watch) {
await ctx.watch();
console.log(`Watching ${target}...`);
} else {
await ctx.rebuild();
await ctx.dispose();
console.log(`Built ${target}`);
}
}
for (const target of targets) {
await build(target);
}