vite.config.ts 1.9 KB

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