-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvite.config.ts
133 lines (122 loc) · 3.42 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
/* eslint-disable perfectionist/sort-imports */
import { join, resolve } from 'node:path'
import process from 'node:process'
import { defineConfig, loadEnv } from 'vite'
import uni from '@dcloudio/vite-plugin-uni'
// uni-helper
import { NutResolver } from 'nutui-uniapp'
import UniComponents from '@uni-helper/vite-plugin-uni-components'
import UniLayouts from '@uni-helper/vite-plugin-uni-layouts'
import UniManifest from '@uni-helper/vite-plugin-uni-manifest'
import UniPages from '@uni-helper/vite-plugin-uni-pages'
import UniTailwind from '@uni-helper/vite-plugin-uni-tailwind'
// postcss
import tailwindcss from 'tailwindcss'
import autoprefixer from 'autoprefixer'
// other
import AutoImport from 'unplugin-auto-import/vite'
import ViteRestart from 'vite-plugin-restart'
const uniPlugin = [
UniComponents({
extensions: ['vue', 'nvue'],
include: [/\.vue$/, /\.vue\?vue/, /\.nvue$/, /\.nvue\?nvue/],
directoryAsNamespace: true,
resolvers: [NutResolver()],
dts: 'types/components.d.ts',
}),
UniLayouts(),
UniPages({
dts: 'types/uni-pages.d.ts',
subPackages: ['src/pages-sub'],
exclude: ['**/components/**/*.*'],
minify: true,
routeBlockLang: 'json5',
}),
UniManifest({ minify: true }),
]
const vitePlugin = [
AutoImport({
include: [
/\.[tj]s?$/, // .ts, .js
/\.vue$/,
/\.vue\?vue/, // .vue
/\.nvue$/,
/\.nvue\?nvue/, // .nvue
/\.md$/, // .md
],
imports: [
'vue',
'pinia',
'uni-app',
{
'nutui-uniapp/composables': [
// 在这里添加需要自动导入的API
'useNotify',
'useToast',
],
},
],
dts: 'types/auto-import.d.ts',
dirs: ['src/hooks'], // 自动导入 hooks
}),
ViteRestart({
// 通过这个插件,在修改vite.config.js文件则不需要重新运行也生效配置
restart: ['vite.config.ts'],
}),
]
const postcssPlugins = [tailwindcss()]
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, resolve(process.cwd(), 'env'))
const { VITE_PORT, VITE_SHOW_SOURCEMAP, VITE_DELETE_CONSOLE } = env
const { UNI_PLATFORM } = process.env
const state = {
isMp: UNI_PLATFORM?.startsWith('mp') ?? false,
isH5: UNI_PLATFORM === 'h5',
isApp: UNI_PLATFORM === 'app',
}
const plugins = [...uniPlugin, ...vitePlugin, uni()]
if (state.isMp) {
plugins.push(UniTailwind())
}
if (state.isH5) {
postcssPlugins.push(autoprefixer())
}
return {
envDir: './env', // 自定义env目录
plugins,
resolve: {
alias: {
'@': join(process.cwd(), './src'),
},
},
css: {
postcss: {
plugins: postcssPlugins,
},
preprocessorOptions: {
scss: {
api: 'modern-compiler',
additionalData: `@import "nutui-uniapp/styles/variables.scss";`,
silenceDeprecations: ['legacy-js-api', 'global-builtin', 'import'], // 暂时隐藏scss报错
},
},
},
server: {
host: '0.0.0.0',
hmr: true,
open: state.isH5, // h5自动打开页面
port: Number.parseInt(VITE_PORT),
},
build: {
sourcemap: state.isH5 ? VITE_SHOW_SOURCEMAP === 'true' : false, // 默认是false
target: 'es6',
minify: mode === 'development' ? false : 'terser',
terserOptions: {
compress: {
drop_console: VITE_DELETE_CONSOLE === 'true',
drop_debugger: true,
},
},
},
}
})