vite.config.ts 10 KB

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