vite.config.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. import vue from '@vitejs/plugin-vue'
  2. import vueJsx from '@vitejs/plugin-vue-jsx'
  3. import { UserConfig, ConfigEnv, loadEnv, defineConfig } from 'vite'
  4. import AutoImport from 'unplugin-auto-import/vite'
  5. import Components from 'unplugin-vue-components/vite'
  6. import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
  7. import Icons from 'unplugin-icons/vite'
  8. import IconsResolver from 'unplugin-icons/resolver'
  9. import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
  10. import mockDevServerPlugin from 'vite-plugin-mock-dev-server'
  11. import UnoCSS from 'unocss/vite'
  12. import { resolve } from 'path'
  13. import os from 'os'
  14. import { name, version, engines, dependencies, devDependencies } from './package.json'
  15. // https://devtools-next.vuejs.org/
  16. import VueDevTools from 'vite-plugin-vue-devtools'
  17. /** 平台的名称、版本、运行所需的`node`版本、依赖、构建时间的类型提示 */
  18. const __APP_INFO__ = {
  19. pkg: { name, version, engines, dependencies, devDependencies },
  20. buildTimestamp: Date.now()
  21. }
  22. const pathSrc = resolve(__dirname, 'src')
  23. // https://cn.vitejs.dev/config
  24. export default defineConfig(({ mode }: ConfigEnv): UserConfig => {
  25. // 设置scss的api类型为modern-compiler
  26. const env = loadEnv(mode, process.cwd())
  27. return {
  28. resolve: {
  29. alias: {
  30. '@': pathSrc
  31. }
  32. },
  33. css: {
  34. // CSS 预处理器
  35. preprocessorOptions: {
  36. // 定义全局 SCSS 变量
  37. scss: {
  38. api: 'modern-compiler',
  39. // silenceDeprecations: ['legacy-js-api'],
  40. javascriptEnabled: true,
  41. additionalData: `
  42. @use "@/styles/variables.scss" as *;
  43. `
  44. }
  45. }
  46. },
  47. server: {
  48. // 允许IP访问
  49. host: '0.0.0.0',
  50. // 应用端口 (默认:3000)
  51. port: Number(env.VITE_APP_PORT),
  52. // 运行是否自动打开浏览器
  53. open: true,
  54. proxy: {
  55. /** 代理前缀为 /dev-api 的请求 */
  56. [env.VITE_APP_BASE_API]: {
  57. changeOrigin: true,
  58. // 接口地址
  59. target:
  60. env.NODE_ENV === 'production' ? JSON.stringify(`http://${getNetworkIp()}:8107`) : env.VITE_APP_API_URL,
  61. rewrite: (path) => path.replace(new RegExp('^' + env.VITE_APP_BASE_API), ''),
  62. bypass(req, res, options: any) {
  63. const realUrl = options.target + (options.rewrite ? options.rewrite(req.url) : '')
  64. res.setHeader('A-Real-Url', realUrl) // 添加响应标头(A-Real-Url为自定义命名),在浏览器中显示
  65. }
  66. }
  67. }
  68. },
  69. plugins: [
  70. vue(),
  71. // jsx、tsx语法支持
  72. vueJsx(),
  73. // MOCK 服务
  74. env.VITE_MOCK_DEV_SERVER === 'true' ? mockDevServerPlugin() : null,
  75. UnoCSS({
  76. hmrTopLevelAwait: false
  77. }),
  78. // 自动导入参考: https://github.com/sxzz/element-plus-best-practices/blob/main/vite.config.ts
  79. AutoImport({
  80. // 自动导入 Vue 相关函数,如:ref, reactive, toRef 等
  81. imports: ['vue', '@vueuse/core', 'pinia', 'vue-router', 'vue-i18n'],
  82. resolvers: [
  83. // 自动导入 Element Plus 相关函数,如:ElMessage, ElMessageBox... (带样式)
  84. ElementPlusResolver(),
  85. // 自动导入图标组件
  86. IconsResolver({})
  87. ],
  88. eslintrc: {
  89. // 是否自动生成 eslint 规则,建议生成之后设置 false
  90. enabled: false,
  91. // 指定自动导入函数 eslint 规则的文件
  92. filepath: './.eslintrc-auto-import.json',
  93. globalsPropValue: true
  94. },
  95. // 是否在 vue 模板中自动导入
  96. vueTemplate: true,
  97. // 指定自动导入函数TS类型声明文件路径 (false:关闭自动生成)
  98. dts: false
  99. // dts: 'src/types/auto-imports.d.ts'
  100. }),
  101. Components({
  102. resolvers: [
  103. // 自动导入 Element Plus 组件
  104. ElementPlusResolver(),
  105. // 自动注册图标组件
  106. IconsResolver({
  107. // element-plus图标库,其他图标库 https://icon-sets.iconify.design/
  108. enabledCollections: ['ep']
  109. })
  110. ],
  111. // 指定自定义组件位置(默认:src/components)
  112. dirs: ['src/components', 'src/**/components'],
  113. // 指定自动导入组件TS类型声明文件路径 (false:关闭自动生成)
  114. dts: false
  115. // dts: 'src/types/components.d.ts'
  116. }),
  117. Icons({
  118. // 自动安装图标库
  119. autoInstall: true
  120. }),
  121. createSvgIconsPlugin({
  122. // 指定需要缓存的图标文件夹
  123. iconDirs: [resolve(pathSrc, 'assets/icons')],
  124. // 指定symbolId格式
  125. symbolId: 'icon-[dir]-[name]'
  126. })
  127. /* VueDevTools({
  128. openInEditorHost: `http://localhost:${env.VITE_APP_PORT}`,
  129. }), */
  130. ],
  131. // 预加载项目必需的组件
  132. optimizeDeps: {
  133. include: [
  134. 'vue',
  135. 'vue-router',
  136. 'pinia',
  137. 'axios',
  138. '@vueuse/core',
  139. 'sortablejs',
  140. 'path-to-regexp',
  141. 'echarts',
  142. '@wangeditor/editor',
  143. '@wangeditor/editor-for-vue',
  144. 'vue-i18n',
  145. 'path-browserify',
  146. 'element-plus/es/components/form/style/css',
  147. 'element-plus/es/components/form-item/style/css',
  148. 'element-plus/es/components/button/style/css',
  149. 'element-plus/es/components/input/style/css',
  150. 'element-plus/es/components/input-number/style/css',
  151. 'element-plus/es/components/switch/style/css',
  152. 'element-plus/es/components/upload/style/css',
  153. 'element-plus/es/components/menu/style/css',
  154. 'element-plus/es/components/col/style/css',
  155. 'element-plus/es/components/icon/style/css',
  156. 'element-plus/es/components/row/style/css',
  157. 'element-plus/es/components/tag/style/css',
  158. 'element-plus/es/components/dialog/style/css',
  159. 'element-plus/es/components/loading/style/css',
  160. 'element-plus/es/components/radio/style/css',
  161. 'element-plus/es/components/radio-group/style/css',
  162. 'element-plus/es/components/popover/style/css',
  163. 'element-plus/es/components/scrollbar/style/css',
  164. 'element-plus/es/components/tooltip/style/css',
  165. 'element-plus/es/components/dropdown/style/css',
  166. 'element-plus/es/components/dropdown-menu/style/css',
  167. 'element-plus/es/components/dropdown-item/style/css',
  168. 'element-plus/es/components/sub-menu/style/css',
  169. 'element-plus/es/components/menu-item/style/css',
  170. 'element-plus/es/components/divider/style/css',
  171. 'element-plus/es/components/card/style/css',
  172. 'element-plus/es/components/link/style/css',
  173. 'element-plus/es/components/breadcrumb/style/css',
  174. 'element-plus/es/components/breadcrumb-item/style/css',
  175. 'element-plus/es/components/table/style/css',
  176. 'element-plus/es/components/tree-select/style/css',
  177. 'element-plus/es/components/table-column/style/css',
  178. 'element-plus/es/components/select/style/css',
  179. 'element-plus/es/components/option/style/css',
  180. 'element-plus/es/components/pagination/style/css',
  181. 'element-plus/es/components/tree/style/css',
  182. 'element-plus/es/components/alert/style/css',
  183. 'element-plus/es/components/radio-button/style/css',
  184. 'element-plus/es/components/checkbox-group/style/css',
  185. 'element-plus/es/components/checkbox/style/css',
  186. 'element-plus/es/components/tabs/style/css',
  187. 'element-plus/es/components/tab-pane/style/css',
  188. 'element-plus/es/components/rate/style/css',
  189. 'element-plus/es/components/date-picker/style/css',
  190. 'element-plus/es/components/notification/style/css',
  191. 'element-plus/es/components/image/style/css',
  192. 'element-plus/es/components/statistic/style/css',
  193. 'element-plus/es/components/watermark/style/css',
  194. 'element-plus/es/components/config-provider/style/css',
  195. 'element-plus/es/components/text/style/css',
  196. 'element-plus/es/components/drawer/style/css',
  197. 'element-plus/es/components/color-picker/style/css',
  198. 'element-plus/es/components/backtop/style/css'
  199. ]
  200. },
  201. // 构建配置
  202. build: {
  203. chunkSizeWarningLimit: 2000, // 消除打包大小超过500kb警告
  204. minify: 'terser', // Vite 2.6.x 以上需要配置 minify: "terser", terserOptions 才能生效
  205. terserOptions: {
  206. compress: {
  207. keep_infinity: true, // 防止 Infinity 被压缩成 1/0,这可能会导致 Chrome 上的性能问题
  208. drop_console: false, // 生产环境去除 console
  209. drop_debugger: true // 生产环境去除 debugger
  210. },
  211. format: {
  212. comments: false // 删除注释
  213. }
  214. },
  215. rollupOptions: {
  216. output: {
  217. // manualChunks: {
  218. // "vue-i18n": ["vue-i18n"],
  219. // },
  220. // 用于从入口点创建的块的打包输出格式[name]表示文件名,[hash]表示该文件内容hash值
  221. entryFileNames: 'js/[name].[hash].js',
  222. // 用于命名代码拆分时创建的共享块的输出命名
  223. chunkFileNames: 'js/[name].[hash].js',
  224. // 用于输出静态资源的命名,[ext]表示文件扩展名
  225. assetFileNames: (assetInfo: any) => {
  226. const info = assetInfo.name.split('.')
  227. let extType = info[info.length - 1]
  228. // console.log('文件信息', assetInfo.name)
  229. if (/\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/i.test(assetInfo.name)) {
  230. extType = 'media'
  231. } else if (/\.(png|jpe?g|gif|svg)(\?.*)?$/.test(assetInfo.name)) {
  232. extType = 'img'
  233. } else if (/\.(woff2?|eot|ttf|otf)(\?.*)?$/i.test(assetInfo.name)) {
  234. extType = 'fonts'
  235. }
  236. return `${extType}/[name].[hash].[ext]`
  237. }
  238. }
  239. }
  240. },
  241. define: {
  242. __APP_INFO__: JSON.stringify(__APP_INFO__)
  243. }
  244. }
  245. })
  246. function getNetworkIp() {
  247. let needHost = ''
  248. try {
  249. const network = os.networkInterfaces()
  250. for (const dev in network) {
  251. const iFace = network[dev]
  252. if (iFace) {
  253. // 添加这个条件检查,确保iface有被定义
  254. for (let i = 0; i < iFace.length; i++) {
  255. const alias = iFace[i]
  256. if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
  257. needHost = alias.address
  258. }
  259. }
  260. }
  261. }
  262. } catch (e) {
  263. needHost = 'localhost'
  264. }
  265. console.log('needHost: ', needHost)
  266. return needHost
  267. }