vite.config.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import { defineConfig, loadEnv } from 'vite';
  2. import { viteMockServe } from 'vite-plugin-mock';
  3. import externalGlobals from 'rollup-plugin-external-globals';
  4. import { visualizer } from 'rollup-plugin-visualizer';
  5. import type { UserConfig, ConfigEnv } from 'vite';
  6. import { fileURLToPath } from 'url';
  7. import AutoImport from 'unplugin-auto-import/vite';
  8. import Icons from 'unplugin-icons/vite';
  9. import Components from 'unplugin-vue-components/vite';
  10. import IconsResolver from 'unplugin-icons/resolver';
  11. import ElementPlus from 'unplugin-element-plus/vite';
  12. import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
  13. import vue from '@vitejs/plugin-vue';
  14. import vueJsx from '@vitejs/plugin-vue-jsx';
  15. import ViteCompression from 'vite-plugin-compression';
  16. import brotli from 'rollup-plugin-brotli';
  17. import { createHtmlPlugin } from 'vite-plugin-html';
  18. const globals = externalGlobals({
  19. moment: 'moment',
  20. 'video.js': 'videojs',
  21. jspdf: 'jspdf',
  22. xlsx: 'XLSX',
  23. echart: 'echart'
  24. });
  25. export default defineConfig(({ mode }: ConfigEnv): UserConfig => {
  26. // 获取当前工作目录
  27. const root = process.cwd();
  28. // 获取环境变量
  29. const env = loadEnv(mode, root);
  30. return {
  31. // 项目根目录
  32. root,
  33. // 项目部署的基础路径
  34. base: './',
  35. publicDir: fileURLToPath(new URL('./public', import.meta.url)), // 无需处理的静态资源位置
  36. assetsInclude: fileURLToPath(new URL('./src/assets', import.meta.url)), // 需要处理的静态资源位置
  37. css: {
  38. preprocessorOptions: {
  39. less: {
  40. additionalData: `@import '@/styles/variable.less';`
  41. }
  42. }
  43. },
  44. plugins: [
  45. createHtmlPlugin({
  46. inject: {
  47. data: {
  48. // <script src="https://cdn.jsdelivr.net/npm/moment@2.29.1/min/moment.js"></script>
  49. // <script src="https://cdn.jsdelivr.net/npm/echarts@5.2.1/echarts"></script>
  50. // <script src="https://cdn.jsdelivr.net/npm/jspdf@2.5.1/dist/pdf.js"></script>
  51. monentscript: '',
  52. videoscript:
  53. '<script src="https://cdn.jsdelivr.net/npm/video.js@7.14.3/dist/video.min.js"></script>',
  54. echartscript: '',
  55. jspdfscript: '',
  56. xlsxscript:
  57. '<script src="https://cdn.jsdelivr.net/npm/xlsx@0.17.4/dist/xlsx.full.min.js"></script>'
  58. }
  59. }
  60. }),
  61. // brotli({}),
  62. // ViteCompression({
  63. // threshold: 1024 * 20, // 超过20kb才进行压缩
  64. // ext: '.gz', // 压缩后缀
  65. // algorithm: 'gzip' // 压缩算法
  66. // }),
  67. // Vue模板文件编译插件
  68. vue(),
  69. // jsx文件编译插件
  70. vueJsx(),
  71. // 开启mock服务器
  72. viteMockServe({
  73. // 如果接口为 /mock/xxx 以 mock 开头就会被拦截响应配置的内容
  74. mockPath: 'mock', // 数据模拟需要拦截的请求起始 URL
  75. enable: true // 本地环境是否开启 mock 功能
  76. }),
  77. // 开启ElementPlus自动引入CSS
  78. ElementPlus({}),
  79. // 自动导入组件
  80. AutoImport({
  81. // 定义需要自动引入的框架
  82. imports: ['vue', 'vue-router', 'pinia'],
  83. // 处理eslint
  84. eslintrc: {
  85. enabled: true
  86. },
  87. resolvers: [ElementPlusResolver(), IconsResolver()],
  88. dts: fileURLToPath(new URL('./types/auto-imports.d.ts', import.meta.url))
  89. }),
  90. // 自动注册组件
  91. Components({
  92. resolvers: [ElementPlusResolver(), IconsResolver()],
  93. dts: fileURLToPath(new URL('./types/components.d.ts', import.meta.url)),
  94. dirs: [fileURLToPath(new URL('./src/components/auto', import.meta.url))]
  95. }),
  96. Icons({
  97. autoInstall: true
  98. })
  99. ],
  100. // 运行后本地预览的服务器
  101. server: {
  102. // 是否开启https
  103. https: false,
  104. // 指定服务器应该监听哪个 IP 地址。 如果将此设置为 0.0.0.0 或者 true 将监听所有地址,包括局域网和公网地址。
  105. host: true,
  106. // 开发环境预览服务器端口
  107. port: 9999,
  108. // 启动后是否自动打开浏览器
  109. open: false,
  110. // 是否开启CORS跨域
  111. cors: true,
  112. // 代理服务器
  113. // 帮助我们开发时解决跨域问题
  114. proxy: {
  115. // 这里的意思是 以/api开头发送的请求都会被转发到 http://xxx:9000
  116. [env.VITE_APP_API_BASEURL]: {
  117. target: 'http://192.168.50.108:9999',
  118. // target: 'http://localhost:9999',
  119. // 改变 Host Header
  120. changeOrigin: true,
  121. rewrite: (path) => path.replace(new RegExp(`^${env.VITE_APP_API_BASEURL}`), '')
  122. // 发起请求时将 '/api' 替换为 ''
  123. //rewrite: (path) => path.replace(/^\/api/, ""),
  124. }
  125. // [env.VITE_APP_MOCK_BASEURL]: {
  126. // target: 'http://localhost:9001',
  127. // // 改变 Host Header
  128. // changeOrigin: true
  129. // // 发起请求时将 '/api' 替换为 ''
  130. // //rewrite: (path) => path.replace(/^\/api/, ""),
  131. // }
  132. }
  133. },
  134. // 打包配置
  135. build: {
  136. // 关闭 sorcemap 报错不会映射到源码
  137. sourcemap: false,
  138. // 打包大小超出 400kb 提示警告
  139. chunkSizeWarningLimit: 400,
  140. rollupOptions: {
  141. // 打包入口文件 根目录下的 index.html
  142. // 也就是项目从哪个文件开始打包
  143. input: {
  144. index: fileURLToPath(new URL('./index.html', import.meta.url))
  145. },
  146. external: ['moment', 'video.js', 'jspdf', 'xlsx', 'echart'],
  147. plugins: [visualizer({ open: true }), globals],
  148. experimentalLogSideEffects: true,
  149. treeshake: {
  150. preset: 'recommended'
  151. // propertyReadSideEffects: true
  152. },
  153. output: {
  154. experimentalMinChunkSize: 20 * 1024,
  155. manualChunks: (id: string) => {
  156. if (id.includes('html-canvans')) {
  157. return 'html-canvans';
  158. }
  159. if (id.includes('node_modules')) {
  160. return 'vendor';
  161. }
  162. // return 'index';
  163. },
  164. chunkFileNames: 'static/js/[name]-[hash].js', // 代码分割后文件名
  165. entryFileNames: 'static/js/[name]-[hash:6].js', // 入口文件名
  166. assetFileNames: 'static/[ext]/[name]-[hash].[ext]' // 静态资源文件名
  167. }
  168. // 静态资源分类打包
  169. // output: {
  170. // format: 'esm',
  171. // chunkFileNames: 'static/js/[name]-[hash].js',
  172. // entryFileNames: 'static/js/[name]-[hash].js',
  173. // assetFileNames: 'static/[ext]/[name]-[hash].[ext]'
  174. // }
  175. }
  176. },
  177. // 配置别名
  178. resolve: {
  179. alias: {
  180. '@': fileURLToPath(new URL('./src', import.meta.url)),
  181. '#': fileURLToPath(new URL('./types', import.meta.url))
  182. }
  183. }
  184. };
  185. });