vite.config.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import vue from '@vitejs/plugin-vue';
  2. import { resolve } from 'path';
  3. import { defineConfig, loadEnv, ConfigEnv } from 'vite';
  4. import vueSetupExtend from 'vite-plugin-vue-setup-extend-plus'; // setup语法糖设置name
  5. import viteCompression from 'vite-plugin-compression'; //开启gzip打包压缩
  6. import vueJsx from '@vitejs/plugin-vue-jsx'
  7. const pathResolve = (dir: string): any => {
  8. return resolve(__dirname, '.', dir);
  9. };
  10. const alias: Record<string, string> = {
  11. '/@': pathResolve('./src/'),
  12. };
  13. // @ts-ignore
  14. export default defineConfig((mode: ConfigEnv) => {
  15. const env = loadEnv(mode.mode, process.cwd());
  16. return {
  17. plugins: [
  18. vue(),
  19. vueSetupExtend(),
  20. viteCompression(),
  21. vueJsx({
  22. // options are passed on to @vue/babel-plugin-jsx
  23. }),
  24. ],
  25. root: process.cwd(),
  26. resolve: { alias },
  27. base: mode.command === 'serve' ? './' : env.VITE_PUBLIC_PATH,
  28. hmr: true,
  29. server: {
  30. host: '0.0.0.0',
  31. port: env.VITE_PORT as unknown as number,
  32. open: JSON.parse(env.VITE_OPEN), // 是否打开浏览器
  33. },
  34. build: {
  35. outDir: 'dist',
  36. sourcemap: false,
  37. chunkSizeWarningLimit: 1500,
  38. rollupOptions: {
  39. output: {
  40. entryFileNames: `assets/[name].[hash].js`,
  41. chunkFileNames: `assets/[name].[hash].js`,
  42. assetFileNames: `assets/[name].[hash].[ext]`,
  43. compact: true,
  44. manualChunks: {
  45. vue: ['vue', 'vue-router', 'pinia']
  46. },
  47. },
  48. },
  49. minify: 'terser',
  50. terserOptions: {
  51. //生产环境时移除console和debugger
  52. compress: {
  53. drop_console: false,
  54. drop_debugger: true
  55. }
  56. }
  57. },
  58. css: { preprocessorOptions: { css: { charset: false } } }
  59. };
  60. });