vite.config.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. import { fileURLToPath, URL } from 'node:url'
  2. import { defineConfig } from 'vite'
  3. import vue from '@vitejs/plugin-vue'
  4. import path from 'path'
  5. import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
  6. import vueJsx from '@vitejs/plugin-vue-jsx'
  7. import AutoImport from 'unplugin-auto-import/vite' // 自动导入ref等api
  8. // import Components from 'unplugin-vue-components/vite'
  9. // import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
  10. import rollupPluginVisualizer from 'rollup-plugin-visualizer'
  11. import vitePluginCompression from 'vite-plugin-compression'
  12. import vitePluginCdnImport, { autoComplete } from 'vite-plugin-cdn-import'
  13. import { ViteImageOptimizer } from 'vite-plugin-image-optimizer'
  14. // https://vitejs.dev/config/
  15. export default defineConfig(async ({ command, mode }) => {
  16. // 根据当前工作目录中的 `mode` 加载 .env 文件
  17. // 设置第三个参数为 '' 来加载所有环境变量,而不管是否有 `VITE_` 前缀。
  18. // const env = loadEnv(mode, process.cwd(), '')
  19. // const { ENV, VITE_BASE, VITE_BASE_API } = env
  20. process.env.VUE_APP_ENV = mode
  21. let { env } = await import('./env.js')
  22. // let env = envs[mode]
  23. // console.log(1, import.meta.env) // undefined
  24. // console.log(0, env)
  25. // process.env.VITE_APP_TITLE = env.SYSTEM_NAME_ALL
  26. return {
  27. define: {
  28. 'process.env.VUE_APP_ENV': JSON.stringify(mode),
  29. __PACK_DATETIME__: JSON.stringify(new Date().toLocaleString()),
  30. // __BASE_API__: JSON.stringify(env.BASE_API),
  31. },
  32. // 部署生产环境和开发环境下的URL。
  33. // 默认情况下,vite 会假设你的应用是被部署在一个域名的根路径上
  34. // 例如 https://www.admin.vip/。如果应用被部署在一个子路径上,你就需要用这个选项指定这个子路径。例如,如果你的应用被部署在 https://www.admin.vip/admin/,则设置 baseUrl 为 /admin/。
  35. // base: ENV === 'production' ? '/' : '/',
  36. base: env.BASE_URL || '/',
  37. resolve: {
  38. // https://cn.vitejs.dev/config/#resolve-alias
  39. alias: {
  40. '@': fileURLToPath(new URL('./src', import.meta.url)),
  41. '/@': fileURLToPath(new URL('./src', import.meta.url)),
  42. '~': fileURLToPath(new URL('./src/components', import.meta.url)),
  43. // vue: '@vue/compat',
  44. },
  45. // https://cn.vitejs.dev/config/#resolve-extensions
  46. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue'],
  47. },
  48. // vite 相关配置
  49. server: {
  50. port: { development: 1024, test: 1025 }[mode],
  51. host: true,
  52. open: true,
  53. proxy: {
  54. // https://cn.vitejs.dev/config/#server-proxy
  55. '/api': {
  56. target: env.BASE_API,
  57. changeOrigin: true,
  58. rewrite: (p) => p.replace(/^\/api/, ''),
  59. },
  60. },
  61. },
  62. // esbuild: {
  63. // jsxFactory: 'h',
  64. // jsxFragment: 'Fragment',
  65. // include: 'render.js',
  66. // exclude: '**/**.vue',
  67. // },
  68. build: {
  69. outDir: 'pc',
  70. minify: 'terser',
  71. reportCompressedSize: false, // 启用/禁用 gzip 压缩大小报告
  72. // modulePreload: false, // { polyfill: false }
  73. // modulePreload: { polyfill: false },
  74. terserOptions: {
  75. compress: {
  76. //生产环境时移除console
  77. drop_console: true,
  78. drop_debugger: true,
  79. },
  80. },
  81. rollupOptions: {
  82. output: {
  83. assetFileNames: 'assets/[ext]/[name]-[hash][extname]',
  84. chunkFileNames: 'assets/js/[name]-[hash].js',
  85. manualChunks: (id) => {
  86. // 可以在这里打印看一下id的值,这里将node_modules中的包打包为 chunk-libs 文件
  87. // console.log(id)
  88. // if (
  89. // id.includes('node_modules') &&
  90. // (id.includes('@vue/dist/vue') || id.includes('pinia') || id.includes('vue-router'))
  91. // ) {
  92. // return 'vue-modules'
  93. // } else
  94. if (id.includes('assets/icons/svg')) {
  95. return 'icons-svg'
  96. }
  97. // if (id.includes('benz-amr-recorder')) {
  98. // return 'benz-amr-recorder'
  99. // } else if (id.includes('cos-js-sdk-v5')) {
  100. // return 'cos-js-sdk-v5'
  101. // } else if (id.includes('aliyun-oss-sdk')) {
  102. // return 'aliyun-oss-sdk'
  103. // } else if (id.includes('fabric')) {
  104. // return 'fabric'
  105. // } else if (id.includes('echarts')) {
  106. // return 'echarts'
  107. // } else if (id.includes('element-plus')) {
  108. // return 'element-plus'
  109. // } else
  110. else if (['quill-image-resize-module', 'Quill'].some((e) => id.includes(e))) {
  111. return 'quill-image-resize-module'
  112. } else if (id.includes('node_modules')) {
  113. return id.split('node_modules/')[1].split('/')[0]
  114. }
  115. // else if (id.includes('src/components')) {
  116. // return 'chunk-components'
  117. // }
  118. // else if (id.includes('views/')) {
  119. // let first = id.split('views/')[1]
  120. // if (first.includes('/')) {
  121. // return first.match(/(\w+)\/(\w+)/)[0].replace('/', '-') // a/b/c... a-b
  122. // }
  123. // return first.split('.')[0] // a.vue a
  124. // }
  125. },
  126. },
  127. },
  128. },
  129. plugins: [
  130. // '@vue/babel-plugin-jsx',
  131. vue({
  132. // template: {
  133. // compilerOptions: {
  134. // compatConfig: {
  135. // MODE: 2,
  136. // },
  137. // },
  138. // },
  139. // reactivityTransform: true,
  140. }),
  141. createSvgIconsPlugin({
  142. iconDirs: [path.resolve(process.cwd(), 'src/assets/icons/svg')],
  143. symbolId: 'icon-[dir]-[name]',
  144. }),
  145. ViteImageOptimizer({
  146. png: {
  147. // https://sharp.pixelplumbing.com/api-output#png
  148. quality: 100,
  149. },
  150. jpeg: {
  151. quality: 100,
  152. },
  153. jpg: {
  154. quality: 100,
  155. },
  156. }),
  157. AutoImport({
  158. imports: [
  159. 'vue',
  160. 'vue-router',
  161. // {
  162. // vuex: ['useStore'],
  163. // },
  164. ],
  165. // // 可以选择auto-import.d.ts生成的位置
  166. // dts: 'src/auto-import.d.ts',
  167. // resolvers: [ElementPlusResolver()],
  168. }),
  169. // Components({
  170. // resolvers: [ElementPlusResolver()],
  171. // }),
  172. vueJsx({
  173. // include: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.vue'],
  174. }),
  175. vitePluginCompression({
  176. disable: !env._ISGZIP, // 是否禁用压缩,默认为 false
  177. threshold: 10000, // 启用压缩的文件大小限制,单位是字节,默认为 0, 对大于 10kb 的文件进行压缩
  178. // filter:过滤器,对哪些类型的文件进行压缩,默认为 ‘/.(js|mjs|json|css|html)$/i’
  179. // verbose: true:是否在控制台输出压缩结果,默认为 true
  180. // deleteOriginFile:压缩后是否删除原文件,默认为 false
  181. // algorithm:采用的压缩算法,默认是 gzip
  182. // ext:生成的压缩包后缀
  183. }),
  184. env._ISCDN &&
  185. vitePluginCdnImport({
  186. // prodUrl: 'https://unpkg.com/{name}@{version}/{path}',
  187. prodUrl: 'https://cdn.bootcdn.net/ajax/libs/{name}/{version}/{path}',
  188. modules: [
  189. // autoComplete('vue'),
  190. // autoComplete('axios'),
  191. {
  192. name: 'axios', // 包名
  193. var: 'axios', // 对应cdn包导出的变量,如jQuery导出的是$
  194. version: '1.4.0',
  195. path: 'axios.min.js',
  196. },
  197. {
  198. name: 'clipboard',
  199. var: 'ClipboardJS',
  200. version: '2.0.11',
  201. path: 'https://cdn.bootcdn.net/ajax/libs/clipboard.js/2.0.11/clipboard.min.js',
  202. },
  203. {
  204. name: 'jsencrypt',
  205. var: 'JSEncrypt',
  206. version: '3.3.2',
  207. path: 'jsencrypt.min.js',
  208. },
  209. {
  210. name: 'crypto-js',
  211. var: 'CryptoJS',
  212. version: '4.1.1',
  213. path: 'crypto-js.min.js',
  214. },
  215. {
  216. name: 'nprogress',
  217. var: 'NProgress',
  218. version: '0.2.0',
  219. path: 'nprogress.min.js',
  220. css: 'nprogress.min.css',
  221. },
  222. // {
  223. // name: 'element-plus',
  224. // var: 'ElementPlus',
  225. // version: '2.3.6',
  226. // path: 'dist/index.full.js',
  227. // css: 'dist/index.css',
  228. // },
  229. // {
  230. // name: '@element-plus/icons-vue',
  231. // var: 'ElementPlusIconsVue',
  232. // version: '2.1.0',
  233. // path: 'dist/index.iife.min.js',
  234. // },
  235. {
  236. name: 'ali-oss',
  237. var: 'OSS',
  238. path: 'https://gosspublic.alicdn.com/aliyun-oss-sdk-6.17.1.min.js',
  239. },
  240. {
  241. name: 'echarts',
  242. var: 'echarts',
  243. version: '5.4.2',
  244. path: 'echarts.min.js',
  245. },
  246. ],
  247. }),
  248. rollupPluginVisualizer({
  249. emitFile: false, //使用 emitFile 生成文件。 属性为 true,打包后的分析文件会出现在打包好的文件包下;设置为 false ,则会出现在项目根目录下
  250. filename: 'report.html', //生成分析网页文件名
  251. open: false, //在默认用户代理中打开生成的文件
  252. gzipSize: true, //从源代码中收集 gzip 大小并将其显示在图表中
  253. // brotliSize: true, //从源代码中收集 brotli 大小并将其显示在图表中
  254. }),
  255. ],
  256. // optimizedeps: {
  257. // esbuildoptions: {
  258. // target: 'es2020',
  259. // },
  260. // },
  261. // css: {
  262. // preprocessorOptions: {
  263. // scss: {
  264. // additionalData: `@use "@/styles/element-var.scss" as *;`,
  265. // },
  266. // },
  267. // },
  268. }
  269. })