vite.config.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. },
  39. // 项目使用的vite插件。 单独提取到build/vite/plugin中管理
  40. plugins: createVitePlugins(),
  41. css: {
  42. preprocessorOptions: {
  43. scss: {
  44. additionalData: '@use "@/styles/variables.scss" as *;',
  45. javascriptEnabled: true
  46. }
  47. }
  48. },
  49. resolve: {
  50. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.scss', '.css'],
  51. alias: [
  52. {
  53. find: 'vue-i18n',
  54. replacement: 'vue-i18n/dist/vue-i18n.cjs.js'
  55. },
  56. {
  57. find: /\@\//,
  58. replacement: `${pathResolve('src')}/`
  59. }
  60. ]
  61. },
  62. build: {
  63. minify: 'esbuild',
  64. outDir: env.VITE_OUT_DIR || 'dist',
  65. sourcemap: env.VITE_SOURCEMAP === 'true' ? 'inline' : false,
  66. chunkSizeWarningLimit: 2000,
  67. reportCompressedSize: false,
  68. cssCodeSplit: true,
  69. emptyOutDir: true,
  70. target: 'esnext',
  71. assetsInlineLimit: 4096,
  72. rollupOptions: {
  73. output: {
  74. manualChunks: {
  75. 'element-plus': ['element-plus'],
  76. 'echarts': ['echarts'],
  77. 'vue': ['vue', 'vue-router', 'pinia', '@vueuse/core'],
  78. 'libs': ['axios', 'dayjs', '@wangeditor/editor'],
  79. },
  80. chunkFileNames: 'assets/js/[name]-[hash].js',
  81. entryFileNames: 'assets/js/[name]-[hash].js',
  82. assetFileNames: 'assets/[ext]/[name]-[hash].[ext]'
  83. },
  84. maxParallelFileOps: 20,
  85. }
  86. },
  87. optimizeDeps: {
  88. include,
  89. exclude,
  90. esbuildOptions: {
  91. target: 'esnext',
  92. minify: true,
  93. minifyIdentifiers: true,
  94. minifySyntax: true,
  95. minifyWhitespace: true,
  96. treeShaking: true,
  97. }
  98. },
  99. esbuild: {
  100. pure: env.VITE_DROP_CONSOLE === 'true' ? ['console.log', 'debugger'] : [],
  101. target: 'esnext',
  102. treeShaking: true,
  103. minifyIdentifiers: true,
  104. minifySyntax: true,
  105. minifyWhitespace: true,
  106. legalComments: 'none'
  107. }
  108. }
  109. }