forked from magic-akari/lrc-maker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
105 lines (96 loc) · 2.98 KB
/
vite.config.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
import { presets, tagBuilder } from "gen_dep_tag";
import { execSync } from "node:child_process";
import { readdirSync } from "node:fs";
import { readFile } from "node:fs/promises";
import { join } from "node:path";
import { externals } from "rollup-plugin-externals";
import { swc } from "rollup-plugin-swc3";
import { defineConfig, type HtmlTagDescriptor } from "vite";
import pkg from "./package.json" assert { type: "json" };
import sw_plugin from "./plugins/sw-plugin";
const hash = execSync("git rev-parse --short HEAD").toString().trim();
const updateTime = execSync("git log -1 --format=%cI").toString().trim();
const json_suffix = ".json";
const lang_dir = "src/languages";
const langFileList = readdirSync(lang_dir).filter((filename) => filename.endsWith(json_suffix));
langFileList.sort();
interface LangContent {
languageName: string;
}
const langMap = await Promise.all(
langFileList.map(async (f) => {
const filePath = join(lang_dir, f);
const fileContent = await readFile(filePath, {
encoding: "utf-8",
});
const langCode = f.slice(0, -json_suffix.length);
const langJson = JSON.parse(fileContent) as LangContent;
const languageName = langJson.languageName;
return [langCode, languageName] as const;
}),
);
const tag = tagBuilder({ sri: true });
export default defineConfig({
clearScreen: false,
json: {
namedExports: false,
},
plugins: [
swc(),
externals({
react: "React",
"react-dom": "ReactDOM",
}),
{
name: "html-cdn-codegen",
apply: "build",
transformIndexHtml(html) {
return {
html,
tags: [presets.react, presets["react-dom"]].map(tag).map(htmlTag),
};
},
},
sw_plugin(),
],
base: "./",
define: {
"import.meta.env.app": JSON.stringify({ hash, updateTime, version: pkg.version }),
"i18n.langCodeList": JSON.stringify(langFileList.map((f) => f.slice(0, -json_suffix.length))),
"i18n.langMap": JSON.stringify(langMap),
},
css: {
transformer: "lightningcss",
},
build: {
minify: true,
cssMinify: "lightningcss",
outDir: "build",
modulePreload: {
polyfill: false,
},
rollupOptions: {
input: ["index.html", "worker/sw.ts"],
output: {
entryFileNames(chunkInfo) {
if (chunkInfo.name === "sw") {
return "sw.js";
}
return "assets/[name]-[hash].js";
},
},
},
},
});
function htmlTag(meta: ReturnType<typeof tag>): HtmlTagDescriptor {
const { url, integrity } = meta;
return {
tag: "script",
attrs: {
src: url,
integrity,
crossorigin: "anonymous",
},
injectTo: "head",
};
}