-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
108 lines (96 loc) · 2.6 KB
/
vite.config.ts
File metadata and controls
108 lines (96 loc) · 2.6 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 { defineConfig } from "vite";
import { resolve } from "path";
import {
copyFileSync,
existsSync,
mkdirSync,
readFileSync,
writeFileSync,
readdirSync,
statSync,
} from "fs";
function htmlInlinePlugin() {
return {
name: "html-inline",
generateBundle(options: any, bundle: any) {
const htmlAsset = Object.keys(bundle).find((key) =>
key.endsWith(".html"),
);
if (!htmlAsset) return;
const htmlContent = readFileSync(
resolve(__dirname, "src/popup/popup.html"),
"utf-8",
);
const updatedHtml = htmlContent.replace(
'<script type="module"></script>',
'<script type="module" src="popup.js"></script>',
);
writeFileSync(resolve(__dirname, "dist/popup.html"), updatedHtml);
delete bundle[htmlAsset];
},
};
}
export default defineConfig({
build: {
outDir: "dist",
emptyOutDir: true,
minify: "esbuild",
rollupOptions: {
input: {
content: resolve(__dirname, "src/content/content.ts"),
popup: resolve(__dirname, "src/popup/popup.ts"),
},
output: {
entryFileNames: "[name].js",
chunkFileNames: "[name].js",
assetFileNames: "[name].[ext]",
},
},
target: "es2020",
},
plugins: [
htmlInlinePlugin(),
{
name: "copy-manifest-and-assets",
writeBundle() {
if (!existsSync("dist")) {
mkdirSync("dist", { recursive: true });
}
copyFileSync("manifest.json", "dist/manifest.json");
copyFileSync("src/content/content.css", "dist/content.css");
let htmlContent = readFileSync(
resolve(__dirname, "src/popup/popup.html"),
"utf-8",
);
htmlContent = htmlContent.replace(
'<script type="module"></script>',
'<script type="module" src="popup.js"></script>',
);
writeFileSync(resolve(__dirname, "dist/popup.html"), htmlContent);
if (existsSync("icons")) {
if (!existsSync("dist/icons")) {
mkdirSync("dist/icons", { recursive: true });
}
const iconFiles = readdirSync("icons");
iconFiles.forEach((file) => {
const sourcePath = `icons/${file}`;
const destPath = `dist/icons/${file}`;
if (statSync(sourcePath).isFile()) {
copyFileSync(sourcePath, destPath);
}
});
}
},
},
],
resolve: {
alias: {
"@": resolve(__dirname, "src"),
},
},
define: {
"process.env.NODE_ENV": JSON.stringify(
process.env.NODE_ENV || "production",
),
},
});