-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvite.config.ts
175 lines (158 loc) · 5.53 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { resolve } from "path"
import { defineConfig, loadEnv } from "vite"
import minimist from "minimist"
import { viteStaticCopy } from "vite-plugin-static-copy"
import livereload from "rollup-plugin-livereload"
import { svelte } from "@sveltejs/vite-plugin-svelte"
import zipPack from "vite-plugin-zip-pack";
import fg from 'fast-glob';
import vitePluginYamlI18n from './yaml-plugin';
const args = minimist(process.argv.slice(2))
const isWatch = args.watch || args.w || false
const devDistDir = "dev"
const distDir = isWatch ? devDistDir : "dist"
console.log("isWatch=>", isWatch)
console.log("distDir=>", distDir)
const imgbedPrefix = 'https://gitlab.com/ypz.open/siyuan/siyuan-dailynote-today/-/raw/main';
/**
* 更换图片链接
*/
function transformMdFile(content: string, filename: string, prefix: string=imgbedPrefix): string {
//如果不是md文件,不做处理
if (!filename.endsWith(".md")) {
return content;
}
console.log("transform=>", filename);
let imgPat = /!\[.*?\]\(\.?\/?(.*?)\)/g
let imgurl = `${prefix}/$1`
let newReadme = content.replace(imgPat, `![](${imgurl})`);
// await fs.promises.writeFile(readmePath, newReadme)
return newReadme;
}
export default defineConfig({
resolve: {
alias: {
"@": resolve(__dirname, "src"),
}
},
plugins: [
svelte(),
vitePluginYamlI18n({
inDir: 'src/i18n',
outDir: isWatch ? 'dev/i18n' : 'dist/i18n',
}),
viteStaticCopy({
targets: [
{
//将 README 当中的图片路径替换为图床链接
src: "./README.md",
dest: "./",
rename: "README_zh_CN.md", //替换到根目录方便 bazzar 获取
transform: transformMdFile
},
{
//将 README 当中的图片路径替换为图床链接
src: "./README.md",
dest: "../",
rename: "README_zh_CN.md", //替换到根目录方便 bazzar 获取
transform: transformMdFile
},
{
src: ["./README.md", "./README_en_US.md"],
dest: "./",
},
{
src: "./icon.png",
dest: "./",
},
{
src: "./preview.png",
dest: "./",
},
{
src: "./plugin.json",
dest: "./",
},
{
src: "./src/i18n/*.json",
dest: "./i18n/"
},
{
src: "./src/i18n/*.md",
dest: "./i18n/",
transform: transformMdFile
},
],
}),
],
// https://github.com/vitejs/vite/issues/1930
// https://vitejs.dev/guide/env-and-mode.html#env-files
// https://github.com/vitejs/vite/discussions/3058#discussioncomment-2115319
// 在这里自定义变量
define: {
"process.env.DEV_MODE": `"${isWatch}"`,
},
build: {
// 输出路径
outDir: distDir,
emptyOutDir: false,
// 构建后是否生成 source map 文件
sourcemap: isWatch ? 'inline' : false,
// 设置为 false 可以禁用最小化混淆
// 或是用来指定是应用哪种混淆器
// boolean | 'terser' | 'esbuild'
// 不压缩,用于调试
minify: !isWatch,
// minify: false,
lib: {
// Could also be a dictionary or array of multiple entry points
entry: resolve(__dirname, "src/index.ts"),
// the proper extensions will be added
fileName: "index",
formats: ["cjs"],
},
rollupOptions: {
plugins: [
...(
isWatch ? [
livereload(devDistDir),
{
//监听静态资源文件
name: 'watch-external',
async buildStart() {
console.log('watch-external buildStart');
const files = await fg([
'src/i18n/*.json',
'src/i18n/*.yaml',
'./README.md',
'./plugin.json'
]);
for (let file of files) {
this.addWatchFile(file);
}
}
}
] : [
zipPack({
inDir: './dist',
outDir: './',
outFileName: 'package.zip'
})
]
)
],
// make sure to externalize deps that shouldn't be bundled
// into your library
external: ["siyuan", "process"],
output: {
entryFileNames: "[name].js",
assetFileNames: (assetInfo) => {
if (assetInfo.name === "style.css") {
return "index.css"
}
return assetInfo.name
},
},
},
}
});