vite.config.ts 2.2 KB

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