vite.config.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. // proxy: {
  44. // '/api': {
  45. // target: env.VITE_PROXY_URL,
  46. // changeOrigin: true,
  47. // rewrite: (path) => path.replace(/^\/api/, ''),
  48. // secure: false,
  49. // headers: {
  50. // Referer: 'https://example.com'
  51. // }
  52. // }
  53. // }
  54. },
  55. esbuild: {
  56. drop: mode === 'development' ? ['debugger'] : ['debugger', 'console'] // build 移除打印
  57. },
  58. build: {
  59. rollupOptions: {
  60. input: {
  61. index: resolve(__dirname, 'index.html')
  62. },
  63. output: { // 静态资源分类打包
  64. chunkFileNames: 'js/[hash].js',
  65. entryFileNames: 'js/[hash].js',
  66. assetFileNames: 'assets/[ext]/[hash].[ext]',
  67. // 拆分node_modules包
  68. manualChunks: (id: any) => {
  69. if (id.includes('node_modules')) {
  70. return id.toString().split('node_modules/')[1].split('/')[0].toString()
  71. }
  72. return ''
  73. }
  74. }
  75. }
  76. },
  77. define: {
  78. __VUE_OPTIONS_API__: false
  79. }
  80. })
  81. }