vite5 打包项目兼容ie和低版本chrome

背景: vite打包后的项目 在低版本chrome无法使用

直接打包项目在69版本的chrome上无法加载 报错

解决方法:

使用vite官方推荐的插件 @vitejs/plugin-legacy

1、下载

复制代码
npm i @vitejs/plugin-legacy -D

2、vite.config.js

复制代码
import legacy from "@vitejs/plugin-legacy"

plugins: [
  legacy({
         targets: ["defaults", "ie >= 11", "chrome 69", "Chrome >= 49"], //需要兼容的目标列表,可以设置多个
        additionalLegacyPolyfills: ["regenerator-runtime/runtime"],
        renderLegacyChunks: true,
        polyfills: [
          "es.promise.all-settled",
          "es.object.entries",
          "es.symbol",
          "es.array.filter",
          "es.promise",
          "es.promise.finally",
          "es/map",
          "es/set",
          "es.array.for-each",
          "es.object.define-properties",
          "es.object.define-property",
          "es.object.get-own-property-descriptor",
          "es.object.get-own-property-descriptors",
          "es.object.keys",
          "es.object.to-string",
          "web.dom-collections.for-each",
          "esnext.global-this",
          "esnext.string.match-all"
        ],
        modernPolyfills: ["es.promise.all-settled", "es.object.entries"]
      })
]

配置好后npm run build:prod打包看下效果

报错如下

修改vite.config.js中 legacy配置项

复制代码
 polyfills: [
          "es.promise.all-settled",
          "es.object.entries",]
modernPolyfills: ["es.promise.all-settled", "es.object.entries"]

重新打包 可以了

相关推荐
heiqizero几秒前
spark01-创建RDD
linux·前端·python
水木流年追梦2 分钟前
CodeTop Top 300 热门题目8-字符串解码
linux·运维·服务器·前端·算法·leetcode
Coffeeee5 分钟前
用了一段时间的AI了,忍不住想吐点槽,你的AI帮你提效了吗?
前端·人工智能·程序员
java1234_小锋11 分钟前
FastAPI + Vue 3 前后端分离:项目设计与工程实践(偏“能落地”的最佳实践)
前端·vue.js·fastapi
秦歌66612 分钟前
RAG-6-高级RAG实战案例:自适应路由 + 自评估重写 + 网络回退
java·服务器·前端·人工智能·python
\xin4 小时前
pikachu自编exp,xss反射性get,post,存储型xss,dom,dom-x
前端·javascript·xss
是烟花哈9 小时前
【前端】React框架学习
前端·学习·react.js
qq4356947019 小时前
JavaWeb08
前端
2401_8784545310 小时前
html和css的复习(1)
前端·css·html
@PHARAOH11 小时前
WHAT - git worktree 概念
前端·git