-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.ts
91 lines (74 loc) · 2.55 KB
/
build.ts
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import * as esbuild from "https://deno.land/x/[email protected]/mod.js";
import { denoPlugin } from "https://deno.land/x/[email protected]/mod.ts";
import { parse } from "https://deno.land/[email protected]/flags/mod.ts";
import { copySync, ensureDir } from "https://deno.land/[email protected]/fs/mod.ts";
import { resolve } from "https://deno.land/[email protected]/path/mod.ts";
interface BrowserManifestSettings {
color: string;
omits: string[];
// deno-lint-ignore no-explicit-any
overrides?: { [id: string]: any };
}
interface BrowserManifests {
[id: string]: BrowserManifestSettings;
}
const args = parse(Deno.args);
const isWatching = args.watch || args.w;
const browsers: BrowserManifests = {
chrome: {
color: "\x1b[32m",
omits: ["applications", "options_ui", "browser_action"],
},
};
console.log("\x1b[37mPackager\n========\x1b[0m");
const builds = Object.keys(browsers).map(async (browserId) => {
const distDir = `_dist/${browserId}`;
// Copy JS/HTML/CSS/ICONS
ensureDir(`${distDir}/static`);
const options = { overwrite: true };
copySync("static", distDir, options);
const browserManifestSettings = browsers[browserId];
// Transform Manifest
const manifest = {
...JSON.parse(Deno.readTextFileSync("extension/manifest.json")),
...browserManifestSettings.overrides,
};
browserManifestSettings.omits.forEach((omit) => delete manifest[omit]);
Deno.writeTextFileSync(
distDir + "/manifest.json",
JSON.stringify(manifest, null, 2),
);
const color = browserManifestSettings.color || "";
const browserName = browserId.toUpperCase();
const colorizedBrowserName = `\x1b[1m${color}${browserName}\x1b[0m`;
const outdir = `_dist/${browserId}/`;
console.log(`Initializing ${colorizedBrowserName} build...`);
const esBuildOptions: esbuild.BuildOptions = {
entryPoints: [
"extension/options.tsx",
"extension/deco_content.ts",
"extension/deco_worker.ts",
"extension/popup.tsx",
],
outdir,
bundle: true,
format: "esm",
logLevel: "verbose",
plugins: [],
};
esBuildOptions.plugins = [denoPlugin()];
// Add watch esbuild options
if (isWatching) {
esBuildOptions.watch = {
onRebuild(error: Error) {
if (error) {
console.error(`Rebuild for ${colorizedBrowserName} failed:`, error);
} else console.log(`Rebuilt for ${colorizedBrowserName}`);
},
};
}
await esbuild.build(esBuildOptions);
console.log(`Build complete for ${colorizedBrowserName}: ${resolve(outdir)}`);
});
await Promise.all(builds);
if (!isWatching) Deno.exit(0);