当前位置:网站首页>How vite is compatible with lower version browsers
How vite is compatible with lower version browsers
2022-07-02 05:50:00 【User experience officer Dalong】
One 、 problem
In the use of vue3.2 and vite2.0+ Develop a mobile terminal H5, During the test, it was found that many low version Android mobile browsers have white screens , and ios The model is basically good , as a result of Many lower version browsers do not support native ESM How to import , The solution is given below :
Two 、 Compatible with native ESM Browser
By default ,Vite The target browser of is able to Support native ESM script label and Support native ESM Dynamic import Of . As a reference ,Vite Use this browserslist As a query standard :
- Chrome >=87
- Firefox >=78
- Safari >=13
- Edge >=88
3、 ... and 、 Solution
Step one : vite.config.ts in build.target Configuration item Specify the build target as es2015
Step two : Through plug-ins @vitejs/plugin-legacy To automatically generate traditional browsers chunk And its corresponding ES Language characteristics polyfill. Compatible version chunk Only when native is not supported ESM Load on demand in your browser .
1、 install @vitejs/plugin-legacy
yarn add @vitejs/plugin-legacy -D
2、 To configure 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'
}
})
Four 、 Packaging inspection
Check the packaged dist Inside index.html You know ,script There are two ways to introduce , Check that the current browser supports native ESM when , load <script type="module">, For lower version browsers, it will load <script nomodule> Corresponding js.
( End )
reference :
Build the production version | Vite Official Chinese documents ;
Remember a vite2.x Packaging optimization process - SegmentFault Think no
https://github.com/vitejs/vite/tree/main/packages/plugin-legacy
边栏推荐
猜你喜欢
Fundamentals of software testing
记录sentry的踩坑之路
Minimum value ruler method for the length of continuous subsequences whose sum is not less than s
2022-2-15 learning xiangniuke project - Section 8 check login status
VSCode paste image插件保存图片路径设置
Technologists talk about open source: This is not just using love to generate electricity
"Simple" infinite magic cube
15 C language advanced dynamic memory management
Pytorch Basics
A collection of commonly used plug-ins for idea development tools
随机推荐
Résumé de la collection de plug - ins couramment utilisée dans les outils de développement idea
2022-2-14 learning xiangniuke project - Section 7 account setting
H5 jump applet
Thread pool overview
1036 Boys vs Girls
我所理解的DRM显示框架
Zzuli:1069 learn from classmate Z
File contains vulnerabilities (II)
KMP idea and template code
Win10 copy files, save files... All need administrator permission, solution
centos8安装mysql8.0.22教程
正则表达式总结
DRM display framework as I understand it
h5跳小程序
Typora installation (no need to enter serial number)
Visual studio import
PHP obtains some values in the string according to the specified characters, and reorganizes the remaining strings into a new array
15 C language advanced dynamic memory management
PHP read file (read the specified line containing a string in the file)
文件包含漏洞(一)