当前位置:   article > 正文

vite 打包配置优化,静态资源合并_vite2.0优化配置

vite2.0优化配置
静态资源的打包处理
  1. // vite.config.ts
  2. build: {
  3. rollupOptions: {
  4. output: {
  5. // 静态资源打包做处理
  6. chunkFileNames: 'static/js/[name]-[hash].js',
  7. entryFileNames: 'static/js/[name]-[hash].js',
  8. assetFileNames: 'static/[ext]/[name]-[hash].[ext]',
  9. manualChunks(id) {
  10. if (id.includes('node_modules')) {
  11. return id.toString().split('node_modules/')[1].split('/')[0].toString();
  12. }
  13. }
  14. }
  15. }
  16. }
提高静态资源的容量大小
  1. // vite.config.ts
  2. build: {
  3. chunkSizeWarningLimit: 1000,
  4. }
清除全局的 console.log 和 debug
  1. // vite.config.ts
  2. build: {
  3. terserOptions: {
  4. compress: {
  5. drop_console: true,
  6. drop_debugger: true,
  7. }
  8. },
  9. }
gzip 静态资源压缩

下载 gzip 压缩插件

npm i vite-plugin-compression -D

vite.config.ts 配置

  1. import viteCompression from 'vite-plugin-compression'
  2. plugins: [
  3. viteCompression({
  4. verbose: true,
  5. disable: false,
  6. threshold: 10240,
  7. algorithm: 'gzip',
  8. ext: '.gz',
  9. }),
  10. ],
低版本浏览器兼容

下载插件

npm i @vitejs/plugin-legacy -D

vite.config.ts 配置

  1. import legacyPlugin from '@vitejs/plugin-legacy'
  2. plugins: [
  3. legacyPlugin({
  4. targets: ['chrome 52'],
  5. additionalLegacyPolyfills: ['regenerator-runtime/runtime'] // 面向IE11时需要此插件
  6. })
  7. ],
完整配置项
  1. import { defineConfig } from 'vite'
  2. import vue from '@vitejs/plugin-vue'
  3. import viteCompression from 'vite-plugin-compression'
  4. import legacyPlugin from '@vitejs/plugin-legacy'
  5. import { resolve } from 'path'
  6. // https://vitejs.dev/config/
  7. export default defineConfig({
  8. mode: 'production',
  9. plugins: [
  10. vue(),
  11. viteCompression({
  12. // gzip静态资源压缩配置
  13. verbose: true,
  14. disable: false,
  15. threshold: 10240,
  16. algorithm: 'gzip',
  17. ext: '.gz',
  18. }),
  19. legacyPlugin({
  20. targets: ['chrome 52'], // 需要兼容的目标列表,可以设置多个
  21. additionalLegacyPolyfills: ['regenerator-runtime/runtime'], // 面向IE11时需要此插件
  22. }),
  23. ],
  24. server: {
  25. port: 4000, // 设置端口号
  26. host: true, // 开启本机端口地址
  27. open: true, // 自动打开浏览器
  28. proxy: {
  29. // 配置跨域
  30. '/api': {
  31. target: '', // 服务地址
  32. changeOrigin: true,
  33. },
  34. },
  35. },
  36. resolve: {
  37. alias: {
  38. '@': resolve(__dirname, 'src'), // 配置根目录
  39. },
  40. },
  41. build: {
  42. chunkSizeWarningLimit: 1000, // 提高超大静态资源警告大小
  43. terserOptions: {
  44. // 清除console和debugger
  45. compress: {
  46. drop_console: true,
  47. drop_debugger: true,
  48. },
  49. },
  50. rollupOptions: {
  51. input: 'index.html',
  52. output: {
  53. // 静态资源打包做处理
  54. chunkFileNames: 'static/js/[name]-[hash].js',
  55. entryFileNames: 'static/js/[name]-[hash].js',
  56. assetFileNames: 'static/[ext]/[name]-[hash].[ext]',
  57. manualChunks(id) {
  58. if (id.includes('node_modules')) {
  59. return id.toString().split('node_modules/')[1].split('/')[0].toString()
  60. }
  61. },
  62. },
  63. },
  64. },
  65. })

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/338732
推荐阅读
相关标签
  

闽ICP备14008679号