-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
118 lines (96 loc) · 2.55 KB
/
Copy pathvite.config.ts
File metadata and controls
118 lines (96 loc) · 2.55 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
109
110
111
112
113
114
115
116
117
118
import { defineConfig } from "vite";
import path from "node:path";
import react from "@vitejs/plugin-react";
import process from "node:process";
import zipPack from "vite-plugin-zip-pack";
import checker from "vite-plugin-checker";
import clean from "vite-plugin-clean";
import WextManifest from "vite-plugin-wext-manifest";
import type { Plugin } from "vite";
export default defineConfig(({ mode }) => {
const isDevelopment = mode !== "production";
const sourcePath = path.resolve(__dirname, "source");
const destPath = path.resolve(__dirname, "extension");
const targetBrowser = process.env.TARGET_BROWSER || "chrome";
const getOutDir = () => path.resolve(destPath, targetBrowser);
const getExtensionZipFileName = () => {
switch (targetBrowser) {
case "opera": {
return `${targetBrowser}.crx`;
}
case "firefox": {
return `${targetBrowser}.xpi`;
}
default: {
return `${targetBrowser}.zip`;
}
}
};
return {
root: sourcePath,
publicDir: path.resolve(sourcePath, "public"),
resolve: {
alias: {
"@": path.resolve(sourcePath),
"~": path.resolve(__dirname, "node_modules"),
},
},
define: {
__DEV__: isDevelopment,
__TARGET_BROWSER__: JSON.stringify(targetBrowser),
},
plugins: [
react(),
// delete previous built compressed file
clean({
targetFiles: [path.resolve(destPath, getExtensionZipFileName())],
}) as Plugin,
// Run typescript checker in worker thread
checker({
typescript: {
tsconfigPath: "./tsconfig.json",
},
}),
// Generate manifest.json for the browser
WextManifest({
manifestPath: "manifest.json",
usePackageJSONVersion: true,
}),
!isDevelopment &&
zipPack({
inDir: getOutDir(),
outDir: destPath,
outFileName: getExtensionZipFileName(),
enableLogging: true,
}),
],
build: {
outDir: getOutDir(),
emptyOutDir: !isDevelopment,
sourcemap: isDevelopment ? "inline" : false,
minify: mode === "production",
rollupOptions: {
input: {
newtab: path.resolve(sourcePath, "index.html"),
},
output: {
entryFileNames: "assets/js/[name].bundle.js",
assetFileNames: (assetInfo) => {
if (assetInfo.names?.[0]?.match(/\.(css|s[ac]ss|less)$/)) {
return "assets/css/[name]-[hash].css";
}
return "assets/[name]-[hash].[ext]";
},
chunkFileNames: "assets/js/[name]-[hash].chunk.js",
},
},
},
// esbuild options - drop console/debugger in production
esbuild:
mode === "production"
? {
drop: ["console", "debugger"],
}
: {},
};
});