vite.config.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. 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
  47. }
  48. }
  49. },
  50. resolve: {
  51. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.scss', '.css'],
  52. alias: [
  53. {
  54. find: 'vue-i18n',
  55. replacement: 'vue-i18n/dist/vue-i18n.cjs.js'
  56. },
  57. {
  58. find: /\@\//,
  59. replacement: `${pathResolve('src')}/`
  60. }
  61. ]
  62. },
  63. build: {
  64. minify: 'terser',
  65. outDir: env.VITE_OUT_DIR || 'dist',
  66. sourcemap: env.VITE_SOURCEMAP === 'true' ? 'inline' : false,
  67. // brotliSize: false,
  68. terserOptions: {
  69. compress: {
  70. drop_debugger: env.VITE_DROP_DEBUGGER === 'true',
  71. drop_console: env.VITE_DROP_CONSOLE === 'true'
  72. }
  73. },
  74. rollupOptions: {
  75. output: {
  76. manualChunks: {
  77. echarts: ['echarts'] // 将 echarts 单独打包,参考 https://gitee.com/yudaocode/yudao-ui-admin-vue3/issues/IAB1SX 讨论
  78. }
  79. },
  80. },
  81. },
  82. optimizeDeps: {include, exclude}
  83. }
  84. }