vite.config.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. // 是否开启 https
  27. https: false,
  28. // 端口号
  29. port: env.VITE_PORT,
  30. host: "0.0.0.0",
  31. open: env.VITE_OPEN === 'true',
  32. // 本地跨域代理. 目前注释的原因:暂时没有用途,server 端已经支持跨域
  33. // proxy: {
  34. // ['/admin-api']: {
  35. // target: env.VITE_BASE_URL,
  36. // ws: false,
  37. // changeOrigin: true,
  38. // rewrite: (path) => path.replace(new RegExp(`^/admin-api`), ''),
  39. // },
  40. // },
  41. },
  42. // 项目使用的vite插件。 单独提取到build/vite/plugin中管理
  43. plugins: createVitePlugins(),
  44. css: {
  45. preprocessorOptions: {
  46. scss: {
  47. additionalData: '@import "./src/styles/variables.scss";',
  48. javascriptEnabled: true
  49. }
  50. }
  51. },
  52. resolve: {
  53. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.scss', '.css'],
  54. alias: [
  55. {
  56. find: 'vue-i18n',
  57. replacement: 'vue-i18n/dist/vue-i18n.cjs.js'
  58. },
  59. {
  60. find: /\@\//,
  61. replacement: `${pathResolve('src')}/`
  62. }
  63. ]
  64. },
  65. build: {
  66. minify: 'terser',
  67. outDir: env.VITE_OUT_DIR || 'dist',
  68. sourcemap: env.VITE_SOURCEMAP === 'true' ? 'inline' : false,
  69. // brotliSize: false,
  70. terserOptions: {
  71. compress: {
  72. drop_debugger: env.VITE_DROP_DEBUGGER === 'true',
  73. drop_console: env.VITE_DROP_CONSOLE === 'true'
  74. }
  75. }
  76. },
  77. optimizeDeps: { include, exclude }
  78. }
  79. }