vite.config.ts
2.7 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
101
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
import { OUTPUT_DIR, brotliSize, chunkSizeWarningLimit, terserOptions, rollupOptions } from './build/constant'
import viteCompression from 'vite-plugin-compression'
import { viteMockServe } from 'vite-plugin-mock'
import monacoEditorPlugin from 'vite-plugin-monaco-editor'
import { createProxy } from './build/external/vite/proxy'
import { getPublicPath, parseEnv } from './build/external/utils'
import { createVitePlugins } from './build/external/vite/plugins'
function pathResolve(dir: string) {
return resolve(process.cwd(), '.', dir)
}
// THINGS_KIT 构建方法变更
export default defineConfig(({ mode, command }) => {
const root = process.cwd()
const env = loadEnv(mode, root)
const viteEnv = parseEnv(env)
const isBuild = command === 'build'
return {
// THINGS_KIT 修改public path
base: getPublicPath({ command, mode }, viteEnv),
// 路径重定向
resolve: {
alias: [
{
find: /\/#\//,
replacement: pathResolve('types')
},
{
find: '@',
replacement: pathResolve('src')
},
{
find: 'vue-i18n',
replacement: 'vue-i18n/dist/vue-i18n.cjs.js', //解决i8n警告
}
],
dedupe: ['vue']
},
// 全局 css 注册
css: {
preprocessorOptions: {
scss: {
javascriptEnabled: true,
additionalData: `@import "src/styles/common/style.scss";`
}
}
},
plugins: [
vue(),
monacoEditorPlugin({
languageWorkers: ['editorWorkerService', 'typescript', 'json', 'html']
}),
viteMockServe({
mockPath: '/src/api/mock',
// 开发打包开关
localEnabled: true,
// 生产打包开关
prodEnabled: true,
// 打开后,可以读取 ts 文件模块。 请注意,打开后将无法监视.js 文件
supportTs: true,
// 监视文件更改
watchFiles: true
}),
// 压缩
viteCompression({
verbose: true,
disable: false,
threshold: 10240,
algorithm: 'gzip',
ext: '.gz'
}),
...createVitePlugins(viteEnv, isBuild)
],
build: {
target: 'es2015',
outDir: OUTPUT_DIR,
// minify: 'terser', // 如果需要用terser混淆,可打开这两行
terserOptions: terserOptions,
rollupOptions: rollupOptions,
brotliSize: brotliSize,
chunkSizeWarningLimit: chunkSizeWarningLimit
},
optimizeDeps: {
exclude: ["fsevents"],
},
server: {
port: viteEnv.VITE_DEV_PORT,
proxy: createProxy(viteEnv),
},
}
})