当前位置:网站首页>vite如何兼容低版本浏览器
vite如何兼容低版本浏览器
2022-07-02 05:41:00 【用户体验官大龙】
一、问题
在使用vue3.2和vite2.0+开发一个移动端H5,测试时发现很多低版本的安卓手机浏览器出现白屏的现象,而ios机型基本上是好的,原因是很多低版本浏览器并不支持原生ESM导入的方式,下面给出解决方案:
二、兼容原生ESM的浏览器
默认情况下,Vite 的目标浏览器是指能够 支持原生 ESM script 标签 和 支持原生 ESM 动态导入 的。作为参考,Vite 使用这个 browserslist 作为查询标准:
- Chrome >=87
- Firefox >=78
- Safari >=13
- Edge >=88
三、解决方案
步骤一: vite.config.ts里 build.target 配置项 指定构建目标为 es2015
步骤二: 通过插件 @vitejs/plugin-legacy 来自动生成传统浏览器的 chunk 及与其相对应 ES 语言特性方面的 polyfill。兼容版的 chunk 只会在不支持原生 ESM 的浏览器中进行按需加载。
1、安装@vitejs/plugin-legacy
yarn add @vitejs/plugin-legacy -D
2、配置 vite.config.ts
// vite.config.ts
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import legacy from '@vitejs/plugin-legacy'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [
vue(),
legacy({
targets: ['chrome 52'],
additionalLegacyPolyfills: ['regenerator-runtime/runtime'],
renderLegacyChunks: true,
polyfills: [
'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'
]
}
],
build: {
target: 'es2015'
}
})
四、打包检验
查看打包出来的dist里的index.html可知,script的引入方式有两种,检测当前浏览器支持原生ESM时,加载<script type="module">,而对于低版本的浏览器会加载<script nomodule>相应的js。
(完)
参考文献:
构建生产版本 | Vite 官方中文文档;
记一次vite2.x打包优化过程 - SegmentFault 思否
https://github.com/vitejs/vite/tree/main/packages/plugin-legacy
边栏推荐
- Zzuli:1067 faulty odometer
- Visual Studio导入
- Fabric. JS iText sets the color and background color of the specified text
- “簡單”的無限魔方
- "Original, excellent and vulgar" in operation and maintenance work
- 1036 Boys vs Girls
- Lingyunguang rushes to the scientific innovation board: the annual accounts receivable reaches 800million. Dachen and Xiaomi are shareholders
- Résumé de la collection de plug - ins couramment utilisée dans les outils de développement idea
- Pytorch Chinese document
- Determine whether there is an element in the string type
猜你喜欢
Lingyunguang rushes to the scientific innovation board: the annual accounts receivable reaches 800million. Dachen and Xiaomi are shareholders
Thunder on the ground! Another domestic 5g chip comes out: surpass Huawei and lead the world in performance?
Gee series: unit 10 creating a graphical user interface using Google Earth engine [GUI development]
Straighten elements (with transition animation)
A collection of commonly used plug-ins for idea development tools
Disable access to external entities in XML parsing
Lantern Festival gift - plant vs zombie game (realized by Matlab)
Fabric. JS iText sets the color and background color of the specified text
7.TCP的十一种状态集
Technologists talk about open source: This is not just using love to generate electricity
随机推荐
460. LFU 缓存 双向链表
Minimum value ruler method for the length of continuous subsequences whose sum is not less than s
Fabric. JS upload local image to canvas background
Matplotlib double Y axis + adjust legend position
5g market trend in 2020
all3dp. All Arduino projects in com website (2022.7.1)
[technical notes-08]
I want to understand the swift code before I learn it. I understand it
Gee: use of common mask functions in remote sensing image processing [updatemask]
Thread pool batch processing data
Installation du tutoriel MySQL 8.0.22 par centos8
Zzuli:1062 greatest common divisor
Zzuli:1060 numbers in reverse order
Conglin environmental protection rushes to the scientific and Technological Innovation Board: it plans to raise 2billion yuan, with an annual profit of more than 200million yuan
软件测试答疑篇
Fabric. JS iText set italics manually
How to write good code - Defensive Programming Guide
Balsamiq wireframes free installation
Here comes a new chapter in the series of data conversion when exporting with easyexcel!
Practice C language advanced address book design