-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvite.config.js
More file actions
100 lines (95 loc) · 2.33 KB
/
vite.config.js
File metadata and controls
100 lines (95 loc) · 2.33 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
import { resolve } from "path";
import { readFileSync } from "fs";
import { defineConfig, mergeConfig } from "vite";
// function formatMd(content) {
// let result = content;
// result = result.replace(/^---\n(.*\n)*?---\n*/, "");
// result = result.replace(
// /^[ \t]*:{3,}[ \t]*\w[^\n]*\n([\s\S]*?)^[ \t]*:{3,}[ \t]*$/gm,
// (_, inner) => {
// return inner
// .trimEnd()
// .split("\n")
// .map((line) => `> ${line}`)
// .join("\n");
// }
// );
// return result;
// }
function formatMarkdownPlugin() {
return {
name: "format-markdown-raw",
enforce: "pre",
load(id) {
if (/\.md\?raw$/.test(id)) {
const filePath = id.replace(/\?raw$/, "");
const content = readFileSync(filePath, "utf-8");
return `export default ${JSON.stringify(content)}`;
}
},
};
}
function transformCodePlugin(mode) {
return {
name: "transform-code-plugin",
transform(code, id) {
if (id.includes("src/en.ts") || id.includes("src/zh.ts")) {
const name = id.includes("en.ts") ? "FlashDocsEn" : "FlashDocsZh";
if (mode === "iife") {
return `${code.replace(
"export default docs;",
""
)} global.${name} = docs;`;
}
}
},
};
}
// 创建 ESM 配置
const esmConfig = {
build: {
emptyOutDir: false,
lib: {
entry: {
zh: resolve(__dirname, "src/zh.ts"),
en: resolve(__dirname, "src/en.ts"),
},
},
format: "es",
rollupOptions: {
output: {
entryFileNames: "esm/[name].js",
},
},
},
};
// 创建 IIFE 配置
const iifeConfig = {
build: {
emptyOutDir: false,
lib: {
entry: {
zh: resolve(__dirname, "src/zh.ts"),
en: resolve(__dirname, "src/en.ts"),
},
},
format: "iife",
rollupOptions: {
output: {
entryFileNames: "iife/[name].js",
// 包装成立即执行函数
banner: "(function (global) {",
footer: `})(window);`,
},
},
},
};
// 根据命令行参数选择配置
export default defineConfig(({ command, mode }) => {
const plugins = [formatMarkdownPlugin()];
if (mode === "iife") {
plugins.push(transformCodePlugin(mode));
return mergeConfig(iifeConfig, { plugins });
}
return mergeConfig(esmConfig, { plugins });
});