vite.config.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import {resolve} from 'path'
  2. import type {ConfigEnv, UserConfig} from 'vite'
  3. import {loadEnv} from 'vite'
  4. import {createVitePlugins} from './build/vite'
  5. import {exclude, include} from "./build/vite/optimize"
  6. const root = process.cwd()
  7. function pathResolve(dir: string) {
  8. return resolve(root, '.', dir)
  9. }
  10. // https://vitejs.dev/config/
  11. export default ({command, mode}: ConfigEnv): UserConfig => {
  12. let env = {} as any
  13. const isBuild = command === 'build'
  14. if (!isBuild) {
  15. env = loadEnv((process.argv[3] === '--mode' ? process.argv[4] : process.argv[3]), root)
  16. } else {
  17. env = loadEnv(mode, root)
  18. }
  19. return {
  20. base: env.VITE_BASE_PATH,
  21. root: root,
  22. // 服务端渲染
  23. server: {
  24. port: env.VITE_PORT, // 端口号
  25. host: "0.0.0.0",
  26. open: env.VITE_OPEN === 'true',
  27. // 本地跨域代理. 目前注释的原因:暂时没有用途,server 端已经支持跨域
  28. // proxy: {
  29. // ['/admin-api']: {
  30. // target: env.VITE_BASE_URL,
  31. // ws: false,
  32. // changeOrigin: true,
  33. // rewrite: (path) => path.replace(new RegExp(`^/admin-api`), ''),
  34. // },
  35. // },
  36. // proxy:{
  37. // '/api': {
  38. // target: 'https://home.ynims.com:6060',
  39. // changeOrigin: true,
  40. // secure: true, // 因为目标是 https
  41. // pathRewrite: {
  42. // '^/api': '/api'
  43. // }
  44. // }
  45. // }
  46. },
  47. // 项目使用的vite插件。 单独提取到build/vite/plugin中管理
  48. plugins: createVitePlugins(),
  49. css: {
  50. preprocessorOptions: {
  51. scss: {
  52. additionalData: '@use "@/styles/variables.scss" as *;',
  53. javascriptEnabled: true,
  54. silenceDeprecations: ["legacy-js-api"], // 参考自 https://stackoverflow.com/questions/78997907/the-legacy-js-api-is-deprecated-and-will-be-removed-in-dart-sass-2-0-0
  55. }
  56. }
  57. },
  58. resolve: {
  59. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.scss', '.css'],
  60. alias: [
  61. {
  62. find: 'vue-i18n',
  63. replacement: 'vue-i18n/dist/vue-i18n.cjs.js'
  64. },
  65. {
  66. find: /\@\//,
  67. replacement: `${pathResolve('src')}/`
  68. }
  69. ]
  70. },
  71. build: {
  72. target: 'es2015',
  73. minify: 'terser',
  74. outDir: env.VITE_OUT_DIR || 'dist',
  75. sourcemap: false,
  76. //sourcemap: env.VITE_SOURCEMAP === 'true' ? 'inline' : false,
  77. cssCodeSplit: true,
  78. chunkSizeWarningLimit: 1500,
  79. reportCompressedSize: false,
  80. terserOptions: {
  81. compress: {
  82. drop_debugger: env.VITE_DROP_DEBUGGER === 'true',
  83. drop_console: env.VITE_DROP_CONSOLE === 'true',
  84. pure_funcs: env.VITE_DROP_CONSOLE === 'true' ? ['console.log', 'console.info'] : []
  85. }
  86. },
  87. rollupOptions: {
  88. output: {
  89. chunkFileNames: (chunkInfo) => {
  90. // 只要 chunk 名字包含 pdf.worker,就直接抛到根目录并命名为 pdf.worker.js
  91. if (chunkInfo.name && chunkInfo.name.includes('pdf.worker')) {
  92. return 'pdf.worker.js';
  93. }
  94. return 'assets/js/[name]-[hash].js';
  95. },
  96. entryFileNames: 'assets/js/[name]-[hash].js',
  97. assetFileNames: (chunkInfo) => {
  98. // public 目录下的 pdf worker 保持原文件名,否则 pdfjs 找不到 worker
  99. if (chunkInfo.name && chunkInfo.name.includes('pdf.worker')) {
  100. return 'pdf.worker.mjs'
  101. }
  102. return 'assets/[ext]/[name]-[hash].[ext]'
  103. },
  104. manualChunks: (id) => {
  105. if (id.includes('node_modules')) {
  106. // 1. 业务相关大库单独分包
  107. if (id.includes('bpmn-js') || id.includes('bpmn-js-properties-panel') || id.includes('diagram-js') || id.includes('camunda-bpmn-moddle') || id.includes('bpmn-moddle') || id.includes('moddle-xml') || id.includes('bpmn-js-token-simulation')) {
  108. return 'bpmn'
  109. }
  110. if (id.includes('@form-create/designer')) {
  111. return 'form-designer'
  112. }
  113. if (id.includes('@wangeditor') || id.includes('/wangeditor/')) {
  114. return 'wangeditor'
  115. }
  116. return 'vendor'
  117. }
  118. },
  119. },
  120. },
  121. },
  122. optimizeDeps: {include, exclude}
  123. }
  124. }