vite.config.ts 2.1 KB

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