-
Notifications
You must be signed in to change notification settings - Fork 1
/
buildContentScripts.ts
90 lines (85 loc) · 3.39 KB
/
buildContentScripts.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
import { build } from "vite";
import * as path from "path";
import { getAllFilesSync } from "./src/utils/fs";
import { changeExtension } from "./src/utils/stringParser";
const rootDir = path.resolve(__dirname, "src");
const contentScriptsSrcDir = path.join(rootDir, "contentScripts");
const outDir = path.resolve(__dirname, "build");
// contentScripts subdirectory added during compilation
const contentScriptsOutDir = path.join(outDir, "scripts");
/**
* Returns all of the content scripts in the `contentScripts/*` directory
* in a {@link Record}<string, string>
* from entry names to full paths without file extensions
*/
async function getContentScripts(): Promise<Record<string, string>> {
return Object.fromEntries(
Object.entries(await getAllFilesSync(contentScriptsSrcDir))
.map(([k, v]) => [changeExtension(k, "js"), changeExtension(v, "")])
);
}
/**
* Returns the file name passed with the only flag
* or undefined if none is passed
*
* @todo use this to allow only certain file compilation (partial compilation)
*/
function onlyFileFlag() {
for (let i = 0; i < process.argv.length; i++) {
if (process.argv[i] === "--only" && i + 1 < process.argv.length) {
return process.argv[i + 1];
}
}
}
(async () => {
const contentScripts = await getContentScripts();
let completed = 0;
const total = Object.keys(contentScripts).length;
// maybe clean contentScriptsOutDir once here?
//Object.values(getContentScripts()).forEach(filePath => {
for (const [entryName, filePath] of Object.entries(contentScripts)) {
console.log(`Compiling [${++completed}/${total}] ${filePath}`);
await build({
// if no environment specified, use default: production
mode: process.env.NODE_ENV === "development" ? "development" : undefined,
resolve: {
alias: {
//"@src": rootDir,
//"@assets": assetsDir,
"utils": path.resolve(rootDir, "utils"),
"config": path.resolve(rootDir, "config"),
},
},
root: contentScriptsSrcDir, //rootDir
//publicDir: assetsDir,
build: {
/*lib: {
entry: filePath,
name: "MyLib",
//fileName: (format) => `my-lib.${format}.js`
},*/
target: "ES2015",
outDir: contentScriptsOutDir,
// this is done in the previous build
// and in each prior build
// so definately don't do this
emptyOutDir: false,
// this is done in the previous build
copyPublicDir: false,
sourcemap: process.env.NODE_ENV === "development",
minify: process.env.NODE_ENV === "development" ? false : "esbuild",
rollupOptions: {
input: { [entryName]: filePath },
output: {
// relative to contentScriptsOutDir
entryFileNames: "contentScripts/[name]",
chunkFileNames: "contentScripts/[name]-[hash]",
format: "iife",
// no imports
inlineDynamicImports: false,
},
},
},
});
}
})();