-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvite.build.js
More file actions
108 lines (96 loc) · 2.39 KB
/
Copy pathvite.build.js
File metadata and controls
108 lines (96 loc) · 2.39 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
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
import { build } from "rolldown-vite";
import path from "node:path";
import solidjs from "vite-plugin-solid";
import tailwindcss from "@tailwindcss/vite";
import { globSync } from "node:fs";
import fs from "node:fs/promises";
function isFirefox() {
return process.env.VITE_BUILD_FOR_FIREFOX === "true";
}
const contentRootDir = "src";
const outDir = `.dist/${isFirefox() ? "firefox" : "chrome"}`;
await Promise.all(
globSync("src/content-scripts/**/*.{js,ts}").map((item) => {
return build({
build: {
target: "es2017",
outDir: outDir,
modulePreload: {
polyfill: false,
},
emptyOutDir: false,
rollupOptions: {
external: ["webextension-polyfill"],
input: {
[path.relative(
"src",
item.slice(0, item.length - path.extname(item).length),
)]: path.resolve(item),
},
output: {
globals: {
"webextension-polyfill": isFirefox() ? "browser" : "chrome",
},
format: "iife",
entryFileNames: "[name].js",
},
},
},
});
}),
);
function inputBuilder(...files) {
return Object.fromEntries(
files.map((f) => [
path.relative(
contentRootDir,
f.slice(0, f.length - path.extname(f).length),
),
f,
f.slice(0, f.length - path.extname(f).length),
path.resolve(contentRootDir, f),
]),
);
}
const htmlFiles = ["src/app/index.html", "src/option/index.html"];
// 🧱 Build 2: Other Scripts
await build({
debug: true,
// root: "./src",
plugins: [solidjs(), tailwindcss()],
build: {
target: "esnext",
outDir: outDir,
emptyOutDir: false,
modulePreload: {
polyfill: false,
},
rollupOptions: {
input: inputBuilder("src/background/index.ts", ...htmlFiles),
output: {
entryFileNames: "[name].js",
},
},
},
});
// Step 3: move src/app/index.html src/option/index.html to their folders outside
async function fixHtmlInputs(...files) {
await Promise.all(
files.map(async (srcPath) => {
const src = path.resolve(outDir, srcPath);
const destPath = path.resolve(
outDir,
`./${srcPath.slice(contentRootDir.length, srcPath.length)}`,
);
await fs.mkdir(path.dirname(destPath), { recursive: true });
// INFO: copies from $DIST/src/$x dir to $DIST/$x
await fs.copyFile(src, destPath);
}),
);
// INFO: It removes $DIST/src directory
await fs.rm(path.resolve(outDir, contentRootDir), {
recursive: true,
force: true,
});
}
await fixHtmlInputs(...htmlFiles);