vite.config.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. find: 'vue-i18n',
  27. replacement: 'vue-i18n/dist/vue-i18n.cjs.js', //解决i8n警告
  28. }
  29. ],
  30. dedupe: ['vue']
  31. },
  32. // 全局 css 注册
  33. css: {
  34. preprocessorOptions: {
  35. scss: {
  36. javascriptEnabled: true,
  37. additionalData: `@import "src/styles/common/style.scss";`
  38. }
  39. }
  40. },
  41. // 开发服务器配置
  42. server: {
  43. host: true,
  44. open: true,
  45. port: 3000,
  46. proxy: {
  47. [axiosPre]: {
  48. // @ts-ignore
  49. target: loadEnv(mode, process.cwd()).VITE_DEV_PATH,
  50. changeOrigin: true,
  51. ws: true,
  52. secure: true,
  53. }
  54. }
  55. },
  56. plugins: [
  57. vue(),
  58. monacoEditorPlugin({
  59. languageWorkers: ['editorWorkerService', 'typescript', 'json', 'html']
  60. }),
  61. viteMockServe({
  62. mockPath: '/src/api/mock',
  63. // 开发打包开关
  64. localEnabled: true,
  65. // 生产打包开关
  66. prodEnabled: true,
  67. // 打开后,可以读取 ts 文件模块。 请注意,打开后将无法监视.js 文件
  68. supportTs: true,
  69. // 监视文件更改
  70. watchFiles: true
  71. }),
  72. // 压缩
  73. viteCompression({
  74. verbose: true,
  75. disable: false,
  76. threshold: 10240,
  77. algorithm: 'gzip',
  78. ext: '.gz'
  79. })
  80. ],
  81. build: {
  82. target: 'es2015',
  83. outDir: OUTPUT_DIR,
  84. // minify: 'terser', // 如果需要用terser混淆,可打开这两行
  85. // terserOptions: terserOptions,
  86. rollupOptions: rollupOptions,
  87. brotliSize: brotliSize,
  88. chunkSizeWarningLimit: chunkSizeWarningLimit
  89. }
  90. })