-
Notifications
You must be signed in to change notification settings - Fork 13
/
vite.config.ts
274 lines (251 loc) · 7.86 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import { defineConfig, loadEnv } from "vite"
import vue from "@vitejs/plugin-vue"
import livereload from "rollup-plugin-livereload"
import minimist from "minimist"
import fg from "fast-glob"
import { createHtmlPlugin } from "vite-plugin-html"
import path from "path"
import AutoImport from "unplugin-auto-import/vite"
import Components from "unplugin-vue-components/vite"
import { ElementPlusResolver } from "unplugin-vue-components/resolvers"
import { nodePolyfills } from "vite-plugin-node-polyfills"
import Icons from "unplugin-icons/vite"
// methods start
const getAppBase = (isSiyuanBuild: boolean, isWidgetBuild: boolean, isNginxBuild): string => {
if (isSiyuanBuild) {
return "/plugins/siyuan-plugin-publisher/"
} else if (isWidgetBuild) {
return "/widgets/sy-post-publisher/"
} else if (isNginxBuild) {
return "/"
} else {
return "/"
}
}
const getDefineEnv = (isDevMode: boolean) => {
const mode = process.env.NODE_ENV
const isTest = mode === "test"
console.log("isServe=>", isServe)
console.log("mode=>", mode)
const defaultEnv = {
DEV_MODE: `${isDevMode || isTest}`,
APP_BASE: `${appBase}`,
NODE_ENV: "development",
VITE_DEFAULT_TYPE: `siyuan`,
}
const env = loadEnv(mode, process.cwd())
const processEnvValues = {
"process.env": Object.entries(env).reduce((prev, [key, val]) => {
return {
...prev,
[key]: val,
}
}, defaultEnv),
}
const defineEnv = {
...processEnvValues,
...{},
}
console.log("defineEnv=>", defineEnv)
return defineEnv
}
// methods end
// config
const args = minimist(process.argv.slice(2))
// 开启之后可以同eruda接管日志
const isServe = process.env.IS_SERVE
const isWatch = args.watch || args.w || false
const isDev = isServe || isWatch
const outDir = args.o || args.outDir
const buildType = process.env.BUILD_TYPE
const isSiyuanBuild = process.env.BUILD_TYPE === "siyuan"
const isWidgetBuild = process.env.BUILD_TYPE === "widget"
const isNginxBuild = process.env.BUILD_TYPE === "nginx"
const distDir = outDir || (isWidgetBuild ? "widget" : "./dist")
const appBase = getAppBase(isSiyuanBuild, isWidgetBuild, isNginxBuild)
console.log("isWatch=>", isWatch)
console.log("isDev=>", isDev)
console.log("distDir=>", distDir)
console.log("buildType=>", buildType)
// https://github.com/vuejs/vue-cli/issues/1198
// https://vitejs.dev/config/
// https://github.com/intlify/vue-i18n-next/issues/543
export default defineConfig(() => ({
plugins: [
vue(),
Icons({
autoInstall: true,
}),
AutoImport({
resolvers: [ElementPlusResolver()],
}),
Components({
resolvers: [ElementPlusResolver()],
}),
createHtmlPlugin({
minify: !isDev,
inject: {
// 在 body 标签底部插入指定的 JavaScript 文件
tags: isDev
? [
// {
// tag: "script",
// attrs: {
// src: "./libs/eruda/eruda.js",
// },
// injectTo: "head-prepend",
// },
{
tag: "script",
attrs: {
async: true,
src: "./libs/lute/lute-1.7.5-20230410.min.js",
},
injectTo: "head",
},
{
tag: "script",
attrs: {
async: true,
src: "./libs/alioss/aliyun-oss-sdk-6.16.0.min.js",
},
injectTo: "head",
},
]
: [
{
tag: "script",
attrs: {
async: true,
src: "./libs/lute/lute-1.7.5-20230410.min.js",
},
injectTo: "head",
},
{
tag: "script",
attrs: {
async: true,
src: "./libs/alioss/aliyun-oss-sdk-6.16.0.min.js",
},
injectTo: "head",
},
],
// data: {
// title: "eruda",
// injectScript: isDev ? `<script>eruda.init();</script>` : "",
// },
},
}),
{
name: "add-query-param",
transformIndexHtml(html) {
const timestamp = Date.now()
html = html.replace(/(<script.+src=")([^"]+\.js)"/g, `$1$2?v=${timestamp}"`)
html = html.replace(/(<link[^>]+href=")([^"]+(\.css|\.js))"/g, (match, p1, p2) => `${p1}${p2}?v=${timestamp}"`)
// html = html.replace(/(<link rel=")modulepreload(" crossorigin href=")([^"]+\.js)"/g, `$1preload$2$3?v=${timestamp}"`);
html = html.replace(
/(<link rel=")modulepreload(" crossorigin href=")([^"]+\.js)"/g,
`$1preload$2$3?v=${timestamp}" as="script"`
)
html = html.replace(/(<link[^>]+href=")([^"]+(\.css))"/g, (match, p1, p2) => `${p1}${p2}?v=${timestamp}"`)
html = html.replace(/(<link[^>]+href=")([^"]+\.svg)"/g, `$1$2?v=${timestamp}"`)
html = html.replace(/(<img[^>]+src=")([^"]+\.(jpe?g|gif|webp|bmp|png))"/g, `$1$2?v=${timestamp}"`)
return html
},
},
// 在浏览器中polyfill node
// https://github.com/davidmyersdev/vite-plugin-node-polyfills/blob/main/test/src/main.ts
nodePolyfills({
exclude: [],
globals: {
// can also be 'build', 'dev', or false
Buffer: true,
global: true,
process: true,
},
protocolImports: true,
}),
],
base: "",
// 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: getDefineEnv(isDev),
resolve: {
alias: {
"~": path.resolve(__dirname, "./"),
},
},
build: {
// 输出路径
outDir: distDir,
emptyOutDir: false,
// 构建后是否生成 source map 文件
sourcemap: false,
// 设置为 false 可以禁用最小化混淆
// 或是用来指定是应用哪种混淆器
// boolean | 'terser' | 'esbuild'
// 不压缩,用于调试
minify: !isDev,
rollupOptions: {
plugins: [
...(isWatch
? [
livereload(distDir),
{
//监听静态资源文件
name: "watch-external",
async buildStart() {
const files = await fg(["src/assets/*", "./README*.md", "./widget.json"])
for (const file of files) {
this.addWatchFile(file)
}
},
},
]
: []),
],
// make sure to externalize deps that shouldn't be bundled into your library
external: [],
output: {
// add a query parameter to all JS and CSS file URLs
chunkFileNames: "chunks/chunk.[name].js",
entryFileNames: "entry.[name].js",
assetFileNames: "assets/[name].[ext]",
manualChunks(id) {
if (id.includes("node_modules")) {
let arr = id.toString().split("node_modules/")[1].split("/")
// pnpm单独处理
if (id.includes(".pnpm")) {
arr = id.toString().split(".pnpm/")[1].split("/")
}
const dep = arr[0].split("@")[0].replace(/\./g, "-")
// console.log("id=>", id)
// console.log("dep=>", dep)
if (dep !== "") {
return "vendor_" + dep
}
return "vendor"
}
},
},
},
},
test: {
globals: true,
environment: "jsdom",
// environment: "node",
// environment: "happy-dom",
setupFiles: ["./src/setup.ts"],
include: [
"src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}",
"common/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}",
],
server: {
deps: {
inline: ["element-plus"],
},
},
},
}))