vite.config.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. // 当前执行node命令时文件夹的地址(工作目录)
  7. const root = process.cwd()
  8. // 路径查找
  9. function pathResolve(dir: string) {
  10. return resolve(root, '.', dir)
  11. }
  12. // https://vitejs.dev/config/
  13. export default ({command, mode}: ConfigEnv): UserConfig => {
  14. let env = {} as any
  15. const isBuild = command === 'build'
  16. if (!isBuild) {
  17. env = loadEnv((process.argv[3] === '--mode' ? process.argv[4] : process.argv[3]), root)
  18. } else {
  19. env = loadEnv(mode, root)
  20. }
  21. return {
  22. base: env.VITE_BASE_PATH,
  23. root: root,
  24. // 服务端渲染
  25. server: {
  26. port: env.VITE_PORT, // 端口号
  27. host: "0.0.0.0",
  28. open: env.VITE_OPEN === 'true',
  29. // 本地跨域代理. 目前注释的原因:暂时没有用途,server 端已经支持跨域
  30. // proxy: {
  31. // ['/admin-api']: {
  32. // target: env.VITE_BASE_URL,
  33. // ws: false,
  34. // changeOrigin: true,
  35. // rewrite: (path) => path.replace(new RegExp(`^/admin-api`), ''),
  36. // },
  37. // },
  38. // proxy:{
  39. // '/api': {
  40. // target: 'https://home.ynims.com:6060',
  41. // changeOrigin: true,
  42. // secure: true, // 因为目标是 https
  43. // pathRewrite: {
  44. // '^/api': '/api'
  45. // }
  46. // }
  47. // }
  48. },
  49. // 项目使用的vite插件。 单独提取到build/vite/plugin中管理
  50. plugins: createVitePlugins(),
  51. css: {
  52. preprocessorOptions: {
  53. scss: {
  54. additionalData: '@use "@/styles/variables.scss" as *;',
  55. javascriptEnabled: true,
  56. 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
  57. }
  58. }
  59. },
  60. resolve: {
  61. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.scss', '.css'],
  62. alias: [
  63. {
  64. find: 'vue-i18n',
  65. replacement: 'vue-i18n/dist/vue-i18n.cjs.js'
  66. },
  67. {
  68. find: /\@\//,
  69. replacement: `${pathResolve('src')}/`
  70. }
  71. ]
  72. },
  73. build: {
  74. minify: 'terser',
  75. outDir: env.VITE_OUT_DIR || 'dist',
  76. sourcemap: env.VITE_SOURCEMAP === 'true' ? 'inline' : false,
  77. // brotliSize: false,
  78. terserOptions: {
  79. compress: {
  80. drop_debugger: env.VITE_DROP_DEBUGGER === 'true',
  81. drop_console: env.VITE_DROP_CONSOLE === 'true'
  82. }
  83. },
  84. rollupOptions: {
  85. output: {
  86. manualChunks: {
  87. 'vue-ecosystem': ['vue', 'vue-router'],
  88. echarts: ['echarts'], // 将 echarts 单独打包,参考 https://gitee.com/yudaocode/yudao-ui-admin-vue3/issues/IAB1SX 讨论
  89. pinyin: ['pinyin'],
  90. 'vue-pdf-embed': ['vue-pdf-embed'],
  91. 'highlight.js': ['highlight.js'],
  92. '@vue-office/docx': ['@vue-office/docx'],
  93. '@vue-office/excel': ['@vue-office/excel'],
  94. '@vue-office/pptx': ['@vue-office/pptx'],
  95. 'benz-amr-recorder': ['benz-amr-recorder'],
  96. '@wangeditor/editor': ['@wangeditor/editor'],
  97. '@wangeditor/editor-for-vue': ['@wangeditor/editor-for-vue'],
  98. '@form-create/designer': ['@form-create/designer'],
  99. '@form-create/element-ui': ['@form-create/element-ui'],
  100. 'video.js': ['video.js'],
  101. 'xlsx-js-style': ['xlsx-js-style'],
  102. 'lunar-typescript': ['lunar-typescript'],
  103. 'vue-types': ['vue-types'],
  104. 'element-plus': ['element-plus'],
  105. 'segmentit': ['segmentit'],
  106. 'vue3-signature': ['vue3-signature'],
  107. 'bpmn-js': ['bpmn-js'],
  108. 'bpmn-js-properties-panel': ['bpmn-js-properties-panel'],
  109. },
  110. },
  111. },
  112. },
  113. optimizeDeps: {include, exclude}
  114. }
  115. }