vite.config.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. '/weixin-article': {
  47. target: 'https://mp.weixin.qq.com/',
  48. secure: true,
  49. changeOrigin: true,
  50. rewrite: (path) => path.replace(new RegExp(`^/weixin-article`), '')
  51. }
  52. }
  53. },
  54. // 项目使用的vite插件。 单独提取到build/vite/plugin中管理
  55. plugins: createVitePlugins(),
  56. css: {
  57. preprocessorOptions: {
  58. scss: {
  59. additionalData: '@import "./src/styles/variables.scss";',
  60. javascriptEnabled: true
  61. }
  62. }
  63. },
  64. resolve: {
  65. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.scss', '.css'],
  66. alias: [
  67. {
  68. find: 'vue-i18n',
  69. replacement: 'vue-i18n/dist/vue-i18n.cjs.js'
  70. },
  71. {
  72. find: /\@\//,
  73. replacement: `${pathResolve('src')}/`
  74. }
  75. ]
  76. },
  77. build: {
  78. minify: 'terser',
  79. outDir: env.VITE_OUT_DIR || 'dist',
  80. sourcemap: env.VITE_SOURCEMAP === 'true' ? 'inline' : false,
  81. // brotliSize: false,
  82. terserOptions: {
  83. compress: {
  84. drop_debugger: env.VITE_DROP_DEBUGGER === 'true',
  85. drop_console: env.VITE_DROP_CONSOLE === 'true'
  86. }
  87. }
  88. },
  89. optimizeDeps: { include, exclude }
  90. }
  91. }