vite.config.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. // 本地跨域代理
  30. proxy: env.VITE_PROXY_TARGET ? {
  31. '/admin-api': {
  32. target: env.VITE_PROXY_TARGET,
  33. ws: false,
  34. changeOrigin: true,
  35. },
  36. } : undefined,
  37. },
  38. // 项目使用的vite插件。 单独提取到build/vite/plugin中管理
  39. plugins: createVitePlugins(),
  40. css: {
  41. preprocessorOptions: {
  42. scss: {
  43. additionalData: '@import "./src/styles/variables.scss";',
  44. javascriptEnabled: true
  45. }
  46. }
  47. },
  48. resolve: {
  49. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.scss', '.css'],
  50. alias: [
  51. {
  52. find: 'vue-i18n',
  53. replacement: 'vue-i18n/dist/vue-i18n.cjs.js'
  54. },
  55. {
  56. find: /\@\//,
  57. replacement: `${pathResolve('src')}/`
  58. }
  59. ]
  60. },
  61. build: {
  62. minify: 'terser',
  63. outDir: env.VITE_OUT_DIR || 'dist',
  64. sourcemap: env.VITE_SOURCEMAP === 'true' ? 'inline' : false,
  65. // brotliSize: false,
  66. terserOptions: {
  67. compress: {
  68. drop_debugger: env.VITE_DROP_DEBUGGER === 'true',
  69. drop_console: env.VITE_DROP_CONSOLE === 'true'
  70. }
  71. }
  72. },
  73. optimizeDeps: { include, exclude }
  74. }
  75. }