forked from SevenTV/Extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.hosted.ts
126 lines (114 loc) · 3.21 KB
/
vite.config.hosted.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
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
119
120
121
122
123
124
125
126
import { appName, getFullVersion, r } from "./vite.utils";
import vuei18n from "@intlify/unplugin-vue-i18n/vite";
import vue from "@vitejs/plugin-vue";
import fs from "fs-extra";
import path from "path";
import { defineConfig, loadEnv } from "vite";
export default defineConfig(() => {
const mode = process.env.NODE_ENV ?? "";
const isNightly = process.env.BRANCH === "nightly";
const outDir = process.env.OUT_DIR || "";
const fullVersion = getFullVersion(isNightly);
process.env = {
...process.env,
...loadEnv(mode, process.cwd()),
VITE_APP_NAME: appName,
VITE_APP_VERSION: fullVersion,
VITE_APP_VERSION_BRANCH: process.env.BRANCH,
VITE_APP_CHANGELOG: fs.readFileSync(
r(
{
nightly: "CHANGELOG-nightly.md",
}[process.env.BRANCH ?? ""] ?? "CHANGELOG.md",
),
"utf-8",
),
};
return {
mode,
resolve: {
alias: {
"@": path.resolve(__dirname, "src"),
"@locale": path.resolve(__dirname, "locale"),
"vue-i18n": "vue-i18n/dist/vue-i18n.runtime.esm-bundler.js",
},
},
define: {
"process.env": {},
},
base: process.env.VITE_APP_HOST + "/",
build: {
emptyOutDir: true,
outDir: "dist-hosted" + "/" + outDir,
minify: "terser",
terserOptions: {
mangle: true,
compress: true,
},
chunkSizeWarningLimit: 1000,
cssCodeSplit: false,
rollupOptions: {
input: {
index: r("src/site/site.app.ts"),
},
output: {
format: "es",
inlineDynamicImports: false,
entryFileNames: `v${fullVersion}/[name].${fullVersion}.[hash].js`,
assetFileNames: `v${fullVersion}/seventv.[name].${fullVersion}.[hash][extname]`,
chunkFileNames: `v${fullVersion}/seventv.[name].${fullVersion}.[hash].js`,
sanitizeFileName: (name: string) => {
return (
name
.toLowerCase()
.match(/^([^?#]+).*/)
?.at(1) ?? name
);
},
},
},
},
plugins: [
vue(),
vuei18n({
include: r("./locale/*"),
}),
// Create hosted manifest
{
name: "create-hosted-manifest",
async writeBundle(this, _, bun) {
let indexPath = "";
let stylePath = "";
for (const v of Object.values(bun)) {
if (v.type === "asset" && v.fileName.includes("seventv.style")) {
stylePath = v.fileName;
} else if (v.type === "chunk" && v.fileName.includes("index")) {
indexPath = v.fileName;
}
}
const man = {
version: getFullVersion(isNightly),
index_file: `${process.env.VITE_APP_HOST}/${indexPath}`,
stylesheet_file: `${process.env.VITE_APP_HOST}/${stylePath}`,
worker_file: `${process.env.VITE_APP_HOST}/v${fullVersion}/worker.${fullVersion}.js`,
};
const manifestName = process.env.BRANCH
? `manifest.${process.env.BRANCH.toLowerCase()}.json`
: "manifest.json";
setTimeout(() => {
const p = r("dist-hosted") + (outDir ? "/" + outDir : "");
// Copy worker to version scope (if it's there)
const workerPath = r("dist/worker.js");
if (fs.existsSync(workerPath)) {
fs.copySync(workerPath, `${p}/v${fullVersion}/worker.${fullVersion}.js`);
} else {
man.worker_file = "";
}
// Set up manifest
fs.writeJSONSync(p + "/" + manifestName, man);
});
},
},
],
};
});