vite.config.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. },
  39. // 项目使用的vite插件。 单独提取到build/vite/plugin中管理
  40. plugins: createVitePlugins(),
  41. css: {
  42. preprocessorOptions: {
  43. scss: {
  44. additionalData: '@import "./src/styles/variables.scss";',
  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: 'terser',
  64. outDir: env.VITE_OUT_DIR || 'dist',
  65. sourcemap: env.VITE_SOURCEMAP === 'true' ? 'inline' : false,
  66. // brotliSize: false,
  67. terserOptions: {
  68. compress: {
  69. drop_debugger: env.VITE_DROP_DEBUGGER === 'true',
  70. drop_console: env.VITE_DROP_CONSOLE === 'true'
  71. }
  72. }
  73. },
  74. optimizeDeps: { include, exclude }
  75. }
  76. }