vite.config.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* eslint-disable import/no-extraneous-dependencies */
  2. import { defineConfig, loadEnv } from 'vite'
  3. import vue from '@vitejs/plugin-vue'
  4. import eslint from 'vite-plugin-eslint'
  5. import viteCompression from 'vite-plugin-compression'
  6. import { resolve } from 'path'
  7. import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
  8. // https://vitejs.dev/config/
  9. export default ({ mode }) => {
  10. const env = loadEnv(mode, process.cwd())
  11. return defineConfig({
  12. base: './',
  13. css: {
  14. preprocessorOptions: {
  15. scss: {
  16. api: 'modern-compiler'
  17. }
  18. }
  19. },
  20. resolve: {
  21. alias: {
  22. /*
  23. 路径别名
  24. 若为文件系统路径必须是绝对路径的形式,否则将以别名原样呈现,不会解析为文件系统路径路径
  25. */
  26. '@': resolve(__dirname, './src')
  27. }
  28. },
  29. plugins: [
  30. vue(), viteCompression(),
  31. eslint({ fix: true, include: ['**/*.ts', '**/*.vue'], cache: true }),
  32. createSvgIconsPlugin({
  33. iconDirs: [resolve(__dirname, './src/assets/icons')],
  34. // Specify symbolId format
  35. symbolId: 'icon-[dir]-[name]'
  36. })
  37. ],
  38. server: {
  39. host: '0.0.0.0',
  40. port: 6888,
  41. open: true,
  42. strictPort: false,
  43. cors:true,
  44. proxy: {
  45. '/api': {
  46. target: env.VITE_PROXY_URL,
  47. changeOrigin: true,
  48. rewrite: (path) => path.replace(/^\/api/, ''),
  49. secure: false
  50. }
  51. }
  52. },
  53. esbuild: {
  54. drop: mode === 'development' ? ['debugger'] : ['debugger', 'console'] // build 移除打印
  55. },
  56. build: {
  57. rollupOptions: {
  58. input: {
  59. index: resolve(__dirname, 'index.html')
  60. },
  61. output: { // 静态资源分类打包
  62. chunkFileNames: 'js/[hash].js',
  63. entryFileNames: 'js/[hash].js',
  64. assetFileNames: 'assets/[ext]/[hash].[ext]',
  65. // 拆分node_modules包
  66. manualChunks: (id: any) => {
  67. if (id.includes('node_modules')) {
  68. return id.toString().split('node_modules/')[1].split('/')[0].toString()
  69. }
  70. return ''
  71. }
  72. }
  73. }
  74. },
  75. define: {
  76. __VUE_OPTIONS_API__: false
  77. }
  78. })
  79. }