generated from plutack/deno_web_extension_starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.ts
138 lines (118 loc) · 3.83 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import * as esbuild from "https://deno.land/x/esbuild@v0.19.9/mod.js";
import { denoPlugins } from "https://deno.land/x/esbuild_deno_loader@0.8.2/mod.ts";
import { parse } from "https://deno.land/std@0.208.0/flags/mod.ts";
import { copySync, ensureDir } from "https://deno.land/std@0.208.0/fs/mod.ts";
import { resolve } from "https://deno.land/std@0.208.0/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"],
},
firefox: {
color: "\x1b[91m",
overrides: {
manifest_version: 2,
background: {
scripts: ["background.js"],
},
},
omits: ["options_page", "host_permissions", "action"],
},
};
if (args._[0] === "chrome") delete browsers.firefox;
if (args._[0] === "firefox") delete browsers.chrome;
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("source/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: [
"source/options.tsx",
"source/content_script.ts",
"source/background.ts",
"source/popup.tsx",
],
outdir,
bundle: true,
format: "esm",
logLevel: "verbose",
plugins: [],
};
// Build Deno Plugin Options
let importMapURL = new URL("file://" + resolve("./import_map.json"));
if (!existsSync(importMapURL)) {
const denoJSONFileURL = new URL("file://" + resolve("./deno.json"));
const denoJSON = await (await fetch(denoJSONFileURL)).json();
if (denoJSON.source || denoJSON.imports) {
importMapURL = denoJSONFileURL;
}
}
esBuildOptions.plugins = [
...denoPlugins(
importMapURL ? { importMapURL: importMapURL.toString() } : {},
),
];
// Add watch esbuild options
if (isWatching) {
const watchplugin: esbuild.Plugin = {
name: "watch-plugin",
setup(build) {
build.onEnd((result) => {
if (result.errors.length != 0) {
console.error(
`Rebuild for ${colorizedBrowserName} failed:`,
result.errors,
);
} else console.log(`Rebuilt for ${colorizedBrowserName}`);
});
},
};
esBuildOptions.plugins = [...esBuildOptions.plugins, watchplugin];
const ctx = await esbuild.context({ ...esBuildOptions });
await ctx.watch();
} else {
await esbuild.build({ ...esBuildOptions });
}
console.log(`Build complete for ${colorizedBrowserName}: ${resolve(outdir)}`);
});
await Promise.all(builds);
if (!isWatching) Deno.exit(0);
function existsSync(filePath: string | URL): boolean {
try {
Deno.lstatSync(filePath);
return true;
} catch (error) {
if (error instanceof Deno.errors.NotFound) return false;
throw error;
}
}