vite.config.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { defineConfig, loadEnv } from 'vite'
  2. import vue from '@vitejs/plugin-vue'
  3. import { resolve } from 'path'
  4. import { OUTPUT_DIR, brotliSize, chunkSizeWarningLimit, terserOptions, rollupOptions } from './build/constant'
  5. import viteCompression from 'vite-plugin-compression'
  6. import { viteMockServe} from "vite-plugin-mock";
  7. import { axiosPre } from './src/settings/httpSetting'
  8. function pathResolve(dir: string) {
  9. return resolve(process.cwd(), '.', dir)
  10. }
  11. export default ({ mode }) => defineConfig({
  12. base: process.env.NODE_ENV === 'production' ? '/' : '/',
  13. // 路径重定向
  14. resolve: {
  15. alias: [
  16. {
  17. find: /\/#\//,
  18. replacement: pathResolve('types') + '/'
  19. },
  20. {
  21. find: '@',
  22. replacement: pathResolve('src') + '/'
  23. }
  24. ],
  25. dedupe: ['vue']
  26. },
  27. // 全局 css 注册
  28. css: {
  29. preprocessorOptions: {
  30. scss: {
  31. javascriptEnabled: true,
  32. additionalData: `@import "src/styles/common/style.scss";`
  33. }
  34. }
  35. },
  36. // 开发服务器配置
  37. server: {
  38. host: true,
  39. open: true,
  40. port: 3000,
  41. proxy: {
  42. [axiosPre]: {
  43. // @ts-ignore
  44. target: loadEnv(mode, process.cwd()).VITE_DEV_PATH,
  45. changeOrigin: true,
  46. ws: true,
  47. secure: true,
  48. }
  49. }
  50. },
  51. plugins: [
  52. vue(),
  53. viteMockServe({
  54. mockPath: "/src/api/mock",
  55. // 开发打包开关
  56. localEnabled: true,
  57. // 生产打包开关
  58. prodEnabled: true,
  59. // 打开后,可以读取 ts 文件模块。 请注意,打开后将无法监视.js 文件
  60. supportTs: true,
  61. // 监视文件更改
  62. watchFiles: true,
  63. }),
  64. // 压缩
  65. viteCompression({
  66. verbose: true,
  67. disable: false,
  68. threshold: 10240,
  69. algorithm: 'gzip',
  70. ext: '.gz'
  71. })
  72. ],
  73. build: {
  74. target: 'es2015',
  75. outDir: OUTPUT_DIR,
  76. terserOptions: terserOptions,
  77. rollupOptions: rollupOptions,
  78. brotliSize: brotliSize,
  79. chunkSizeWarningLimit: chunkSizeWarningLimit
  80. }
  81. })