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"]

重新打包 可以了

相关推荐
Codebee13 小时前
Harness Engineering:AICode 的灵魂
前端·人工智能·前端框架
Highcharts.js13 小时前
线形比赛积分增长或竞赛图|Highcharts企业图表代码示列
开发语言·前端·javascript·折线图·highcharts·竞赛图
hpysirius14 小时前
在企业搭建一套完整的AI Agent系统
前端
追逐梦想永不停14 小时前
记录一个好用的excel判断数字格式的公式
前端·chrome·excel
hpysirius14 小时前
从零构建 Web 端视频剪辑器:技术实践与思考
前端
让学习成为一种生活方式14 小时前
大肠杆菌合成扑热息痛--对乙酰氨基酚--文献精读227
开发语言·前端·javascript
李白的天不白14 小时前
请求不到百度网址的原因
前端
Gary Studio14 小时前
Selinux编写
linux·服务器·前端
网络点点滴14 小时前
NPM的包版本管理
前端·npm·node.js
光影少年14 小时前
react性能优化比较好的办法有哪些?
前端·react.js·性能优化