当前位置:网站首页>Vite2兼容低版本chrome(如搜狗80),通过polyfills处理部分需求高版本的语法
Vite2兼容低版本chrome(如搜狗80),通过polyfills处理部分需求高版本的语法
2022-06-30 20:23:00 【Program W】
初步处理
- 引入处理兼容问题的插件
npm install @vitejs/plugin-legacy -D
- 配置
vite.config.js
targets
:默认是package.json
中的browserslist
建议的值additionalLegacyPolyfills
polyfills
modernPolyfills
:启用此选项将为新式构建生成单独的polyfills
块,官方不推荐使用,因为其自动检测的特性,配置后,即时浏览器支持,依然会使用nomodule
进行加载polyfills
的脚本(我这里是replaceAll()
配置在polyfills
,不生效,估计算前沿功能了,所以要么改写法,要么就得配在这里)
import legacy from '@vitejs/plugin-legacy'
plugins: [
vue(),
vueSetupExtend(),
AutoImport({
imports: ['vue', 'vue-router', 'vuex'],
}),
legacy({
// 尝试兼容低版本浏览器(不包括ie11)
targets: ['chrome 80'],
additionalLegacyPolyfills: ['regenerator-runtime/runtime'] //配置了对ie11也没用
})
]
配置后,会根据浏览器的支持动态加载不同的脚本,
<script nomodule>
在不支持本机 ESM 的浏览器中有条件地加载polyfills
和旧版捆绑包。
问题定位(语法问题)
vite
打包后还是es6
语法,有些语法要求版本比较高,需要通过polyfills
(polyfills
是通过在低版本浏览器中模拟等效的代码,来实现高版本语法的行为)进行处理
replaceAll ()
支持的至少chrome85
TypeError: wye(...).replaceAll is not a function
兼容语法
polyfill
无法直接使用,需要在plugin-legacy
中使用,
legacy({
targets: ['chrome 80', 'ie >= 11'],
additionalLegacyPolyfills: ['regenerator-runtime/runtime'], // regenerator-runtime/runtime @dian/polyfill
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',
// 这个无法处理
'es.string.replace-all'
]
})
- 换
modernPolyfills
(成功处理)
legacy({
targets: ['chrome 80', 'ie >= 11'],
additionalLegacyPolyfills: ['regenerator-runtime/runtime'], // regenerator-runtime/runtime @dian/polyfill
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'
],
modernPolyfills: ['es.string.replace-all']
})
modernPolyfills
和polyfills
的差异还不太清楚,慢慢再完善。。。
- 主流的
polyfills
库为 core-js
边栏推荐
- Binary search tree (1) - concept and C language implementation
- 杰理之关于长按开机检测抬起问题【篇】
- SQL优化
- 北京大学ACM Problems 1004:Financial Management
- Lumiprobe nucleic acid quantitative qudye dsDNA br detection kit
- 好高的佣金,《新程序员》合伙人计划来袭,人人皆可参与
- C file pointer
- DEX file parsing - Method_ IDS resolution
- B_QuRT_User_Guide(35)
- 基于开源流批一体数据同步引擎ChunJun数据还原—DDL解析模块的实战分享
猜你喜欢
PHP obtains opcode and C source code
Maya House Modeling
Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasource could
SQL优化
Lumiprobe 聚乙二醇化和 PEG 接头丨碘-PEG3-酸研究
Build your own website (20)
毕业设计
Binary search tree (1) - concept and C language implementation
B_QuRT_User_Guide(31)
Study on PEGylation of lumiprobe and PEG linker - iodine-peg3-acid
随机推荐
AVL平衡二叉树(一) - 概念与C语言实现
What are database OLAP and OLTP? Same and different? Applicable scenarios
我想知道股票开户要认识谁?另外,手机开户安全么?
B_QuRT_User_Guide(33)
C file pointer
Common questions and answering skills of project manager interview
Lumiprobe biotin phosphimide (hydroxyproline) instructions
Jerry's determination of detection sensitivity level [chapter]
Black apple server system installation tutorial, black apple installation tutorial, teach you how to install black apple in detail [easy to understand]
Scene 299
Installation and use of securecrtportable
Huffman tree (I) basic concept and C language implementation
NLP skill tree learning route - (I) route overview
北京大学ACM Problems 1002:487-3279
MFC界面库BCGControlBar v33.0 - 桌面警报窗口、网格控件升级等
Testing principle and precautions of biovendor rage ELISA Kit
Informatics Olympiad 1362: family problems
Lumiprobe 聚乙二醇化和 PEG 接头丨碘-PEG3-酸研究
The newly born robot dog can walk by himself after rolling for an hour. The latest achievement of Wu Enda's first disciple
Description of the latest RTSP address rules for Hikvision camera, NVR, streaming media server, playback and streaming [easy to understand]