-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
74 lines (70 loc) · 2.37 KB
/
build.js
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
import { build as viteBuild } from "vite";
import fs from "node:fs/promises";
import path from "node:path";
import zipPack from "vite-plugin-zip-pack";
// Shared Manifest v3 config (keys supported by both Chrome and Firefox)
const manifestShared = JSON.parse(
await fs.readFile("manifests/manifest-base.json", "utf-8")
);
/**
* Builds and merges a manifest.json file for the specified browser target.
* @param {string} outDir The output directory for the final manifest.json.
* @param {string} manifestSrc The browser-specific manifest.json config to read and merge with the shared config.
*/
function buildManifest(outDir, manifestSrc) {
return {
name: "build-manifest",
closeBundle: async () => {
const browserSpecificManifest = JSON.parse(
await fs.readFile(manifestSrc, "utf-8")
);
const manifestOutFile = path.join(outDir, "manifest.json");
console.log(`Writing manifest from ${manifestSrc} to ${manifestOutFile}`);
await fs.writeFile(
manifestOutFile,
JSON.stringify({ ...manifestShared, ...browserSpecificManifest })
);
},
};
}
/**
* @param {string} outDir The output directory for the browser-specific code.
* @param {string} manifestSrc The browser-specific manifest.json config to read and merge with the shared config.
*/
async function build(outDir, manifestSrc) {
const plugins = [buildManifest(outDir, manifestSrc)];
// Chrome and Firefox require .zip archives when publishing your extension,
// so auto-zip as part of the build process for convenience. Don't do this for dev.
if (process.env.BUILD_TARGET === "production") {
plugins.push(
zipPack({
// We're zipping the output directory (e.g., dist/firefox)
inDir: outDir,
// ... and writing the archive back to the same output directory (e.g., dist/firefox/packed.zip)
outDir,
outFileName: "packed.zip",
})
);
}
return await viteBuild({
plugins,
build: {
target: "esnext",
outDir: outDir,
copyPublicDir: true,
rollupOptions: {
input: {
popup: "src/popup.html",
background: "src/background.ts",
},
output: {
entryFileNames: "src/[name].js",
},
},
},
});
}
await Promise.all([
build("dist/chrome", "manifests/manifest-chrome.json"),
build("dist/firefox", "manifests/manifest-firefox.json"),
]);