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

重新打包 可以了

相关推荐
yuanyxh6 小时前
Mac 软件推荐
前端·javascript·程序员
万少6 小时前
AtomCode开发微信小程序《谁去呀》 全流程
前端·javascript·后端
某人辛木6 小时前
Web自动化测试
前端·python·pycharm·pytest
Kagol7 小时前
Superpowers GSD gstack AgentSkills深度测评
前端·人工智能
excel8 小时前
JavaScript 字符串与模板字面量:从表象到本质理解
前端
京东云开发者8 小时前
当AI成为导演-如何用AI创作动漫短剧
前端
李白的天不白9 小时前
使用 SmartAdmin 进行前后端开发
java·前端
乘风gg9 小时前
🤡PUA AI Coding 工具 的 10 条终极语录
前端·ai编程·claude
学Linux的语莫9 小时前
Vue 3 入门教程
前端·javascript·vue.js
怕浪猫9 小时前
第一章、Chrome DevTools Protocol (CDP) 详解
前端·javascript·chrome