vite.config.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import type { UserConfig, ConfigEnv } from 'vite'
  2. import { loadEnv } from 'vite'
  3. import { resolve } from 'path'
  4. import { wrapperEnv } from './build/utils'
  5. import { createVitePlugins } from './build/vite/plugin'
  6. import { OUTPUT_DIR } from './build/constant'
  7. // import { createProxy } from './build/vite/proxy'
  8. import pkg from './package.json'
  9. import { format } from 'date-fns'
  10. const { dependencies, devDependencies, name, version } = pkg
  11. const __APP_INFO__ = {
  12. pkg: { dependencies, devDependencies, name, version },
  13. lastBuildTime: format(new Date(), 'yyyy-MM-dd HH:mm:ss')
  14. }
  15. function pathResolve(dir: string) {
  16. return resolve(process.cwd(), '.', dir)
  17. }
  18. const proxyUrl = 'https://dev.lexiaoya.cn/'
  19. // const proxyUrl = 'https://test.lexiaoya.cn'
  20. export default ({ command, mode }: ConfigEnv): UserConfig => {
  21. const root = process.cwd()
  22. const env = loadEnv(mode, root)
  23. const viteEnv = wrapperEnv(env)
  24. const { VITE_PUBLIC_PATH, VITE_DROP_CONSOLE, VITE_PORT, VITE_GLOB_PROD_MOCK } = viteEnv
  25. const prodMock = VITE_GLOB_PROD_MOCK
  26. const isBuild = command === 'build'
  27. return {
  28. base: VITE_PUBLIC_PATH,
  29. esbuild: {},
  30. // resolve: {
  31. // alias: [
  32. // {
  33. // find: /\/#\//,
  34. // replacement: pathResolve('types') + '/'
  35. // },
  36. // {
  37. // find: '@',
  38. // replacement: pathResolve('src') + '/'
  39. // }
  40. // ],
  41. // dedupe: ['vue']
  42. // },
  43. resolve: {
  44. alias: {
  45. '@': pathResolve('src') + '/',
  46. '@components': pathResolve('src/components') + '/',
  47. '@views': pathResolve('src/views') + '/'
  48. }
  49. },
  50. plugins: createVitePlugins(viteEnv, isBuild, prodMock),
  51. define: {
  52. __APP_INFO__: JSON.stringify(__APP_INFO__)
  53. },
  54. css: {
  55. preprocessorOptions: {
  56. less: {
  57. modifyVars: {},
  58. javascriptEnabled: true,
  59. additionalData: `@import "src/styles/var.less";`
  60. }
  61. }
  62. },
  63. server: {
  64. host: true,
  65. port: VITE_PORT,
  66. proxy: {
  67. '/cbs-app': {
  68. target: proxyUrl,
  69. changeOrigin: true
  70. }
  71. }
  72. },
  73. optimizeDeps: {
  74. include: [],
  75. exclude: ['vue-demi']
  76. },
  77. build: {
  78. target: 'es2015',
  79. cssTarget: 'chrome80',
  80. outDir: OUTPUT_DIR,
  81. chunkSizeWarningLimit: 2000
  82. }
  83. }
  84. }