vite.config.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. resolve: {
  14. alias: {
  15. /*
  16. 路径别名
  17. 若为文件系统路径必须是绝对路径的形式,否则将以别名原样呈现,不会解析为文件系统路径路径
  18. */
  19. '@': resolve(__dirname, './src')
  20. }
  21. },
  22. plugins: [
  23. vue(), viteCompression(),
  24. eslint({ fix: true, include: [ '**/*.ts', '**/*.vue' ] }),
  25. createSvgIconsPlugin({
  26. iconDirs: [ resolve(__dirname, './src/assets/img') ],
  27. // Specify symbolId format
  28. symbolId: 'icon-[dir]-[name]'
  29. })
  30. ],
  31. server: {
  32. host: '0.0.0.0',
  33. port: 6888,
  34. open: true,
  35. strictPort: false,
  36. // proxy: {
  37. // '/api': {
  38. // target: env.VITE_PROXY_URL,
  39. // changeOrigin: true,
  40. // rewrite: (path) => path.replace(/^\/api/, ''),
  41. // secure: false,
  42. // headers: {
  43. // Referer: 'https://example.com'
  44. // }
  45. // }
  46. // }
  47. },
  48. esbuild: {
  49. drop: mode === 'development' ? [ 'debugger' ] : [ 'debugger', 'console' ] // build 移除打印
  50. },
  51. build: {
  52. rollupOptions: {
  53. input: {
  54. index: resolve(__dirname, 'index.html')
  55. },
  56. output: { // 静态资源分类打包
  57. chunkFileNames: 'js/[hash].js',
  58. entryFileNames: 'js/[hash].js',
  59. assetFileNames: 'assets/[ext]/[hash].[ext]',
  60. // 拆分node_modules包
  61. manualChunks: (id: any) => {
  62. if (id.includes('node_modules')) {
  63. return id.toString().split('node_modules/')[1].split('/')[0].toString()
  64. }
  65. return ''
  66. }
  67. }
  68. }
  69. }
  70. })
  71. }