vite.config.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { resolve } from 'path'
  2. import { loadEnv } from 'vite'
  3. import type { UserConfig, ConfigEnv } from 'vite'
  4. import { createVitePlugins } from './build/vite'
  5. import { include, exclude } 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: 'http://192.168.3.143:5500',
  41. // target: 'https://company.citupro.com:18183',
  42. secure: false, // 是否支持 https,默认 false
  43. changeOrigin: true, // 是否支持跨域
  44. rewrite: (path) => path.replace(new RegExp(`^/api`), '/api')
  45. }
  46. }
  47. },
  48. // 项目使用的vite插件。 单独提取到build/vite/plugin中管理
  49. plugins: createVitePlugins(),
  50. css: {
  51. preprocessorOptions: {
  52. scss: {
  53. additionalData: '@import "./src/styles/variables.scss";',
  54. javascriptEnabled: true
  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. minify: 'terser',
  73. outDir: env.VITE_OUT_DIR || 'dist',
  74. sourcemap: env.VITE_SOURCEMAP === 'true' ? 'inline' : false,
  75. // brotliSize: false,
  76. terserOptions: {
  77. compress: {
  78. drop_debugger: env.VITE_DROP_DEBUGGER === 'true',
  79. drop_console: env.VITE_DROP_CONSOLE === 'true'
  80. }
  81. }
  82. },
  83. optimizeDeps: { include, exclude }
  84. }
  85. }