vite.config.ts 1.9 KB

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