vite.config.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. import vueDevTools from 'vite-plugin-vue-devtools'
  7. // 当前执行node命令时文件夹的地址(工作目录)
  8. const root = process.cwd()
  9. // 路径查找
  10. function pathResolve(dir: string) {
  11. return resolve(root, '.', dir)
  12. }
  13. // https://vitejs.dev/config/
  14. export default ({ command, mode }: ConfigEnv): UserConfig => {
  15. let env = {} as any
  16. const isBuild = command === 'build'
  17. if (!isBuild) {
  18. env = loadEnv((process.argv[3] === '--mode' ? process.argv[4] : process.argv[3]), root)
  19. } else {
  20. env = loadEnv(mode, root)
  21. }
  22. return {
  23. base: env.VITE_BASE_PATH,
  24. root: root,
  25. // 服务端渲染
  26. server: {
  27. port: env.VITE_PORT, // 端口号
  28. host: "0.0.0.0",
  29. open: env.VITE_OPEN === 'true',
  30. // 本地跨域代理. 目前注释的原因:暂时没有用途,server 端已经支持跨域
  31. // proxy: {
  32. // ['/admin-api']: {
  33. // target: env.VITE_BASE_URL,
  34. // ws: false,
  35. // changeOrigin: true,
  36. // rewrite: (path) => path.replace(new RegExp(`^/admin-api`), ''),
  37. // },
  38. // },
  39. },
  40. // 项目使用的vite插件。 单独提取到build/vite/plugin中管理
  41. plugins: [
  42. process.env.NODE_ENV === 'development' && vueDevTools(),
  43. createVitePlugins()
  44. ],
  45. css: {
  46. preprocessorOptions: {
  47. scss: {
  48. additionalData: '@use "./src/styles/variables.scss" as *;',
  49. javascriptEnabled: true
  50. }
  51. }
  52. },
  53. resolve: {
  54. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.scss', '.css'],
  55. alias: [
  56. {
  57. find: 'vue-i18n',
  58. replacement: 'vue-i18n/dist/vue-i18n.cjs.js'
  59. },
  60. {
  61. find: /\@\//,
  62. replacement: `${pathResolve('src')}/`
  63. }
  64. ]
  65. },
  66. build: {
  67. minify: 'terser',
  68. outDir: env.VITE_OUT_DIR || 'dist',
  69. sourcemap: env.VITE_SOURCEMAP === 'true' ? 'inline' : false,
  70. // brotliSize: false,
  71. terserOptions: {
  72. compress: {
  73. drop_debugger: env.VITE_DROP_DEBUGGER === 'true',
  74. drop_console: env.VITE_DROP_CONSOLE === 'true'
  75. }
  76. }
  77. },
  78. optimizeDeps: { include, exclude }
  79. }
  80. }