当前位置:网站首页>Tradeoff and selection of SWC compatible Polyfill
Tradeoff and selection of SWC compatible Polyfill
2022-06-12 07:00:00 【Chenai】
background
Kaimen Jianshan ,swc Currently in polyfill There are some problems in :
es2022 I won't support it
When using es2022 characteristic ( Such as array.at) when ( Currently not released ), Whatever you use swc Provided polyfill Strategy (usage / entry), Can't provide es2022 Of polyfill ( among entry Policies are available up to es2021 ).
swc polyfill The query list is out of date
swc Of polyfill The strategy is to maintain a core-js-compat Of modules-by-versions.json list ( Deprecated ), Unable to instantly synchronize upstream core-js The latest polyfill change .
swc polyfill Not actively maintained
swc Not actively maintained for a long time polyfill Function module .
in summary , We currently use any swc Of polyfill None of the strategies are optimal .
Next, we will explore a relatively optimal swc polyfill solution .
solution
Fortunately, ,swc Provides the manual declaration of the polyfill Options , namely env.include ( Additional Options - include ), By specifying the desired polyfill modules The list is what you expect .
Install the required dependencies first :
pnpm add browserslist core-js core-js-compat
Achieve automatic acquisition polyfill modules :
// getPolyfills.ts
import browserslist from 'browserslist'
export const getPolyfills = () => {
// ↓ The root directory of the project , Will automatically find from here browserslistrc To configure
const root = process.cwd()
const env = process.env.NODE_ENV
const browsers = browserslist(undefined, {
path: root, env })
const {
list } = require('core-js-compat')({
targets: browsers,
// ↓ You must specify... Here core-js Of Top two Specific version , Such as `3.20` / `3.21` , Otherwise polyfill Incomplete !
version: '3.21',
})
// ['es.error.cause', 'es.aggregate-error', 'es.aggregate-error.cause', ...]
return list as string[]
}
notes :
If no automatic
browserslistrcTo configure , It can be manually specified insteadbrowsers( Such as['chrome 80']).When you can't read
browserslistrcWhen the configuration , By defaultdefaultsTo configure , See browserslist / browserslist .
Finally, it was introduced into swc The configuration can be :
// .swcrc fragment
import {
getPolyfills } from './getPolyfills'
{
"env": {
"include": getPolyfills()
}
}
Only this and nothing more ,swc It can automatically inject all the data we pass through browserslistrc Configured targets Corresponding polyfill, Note that it is full injection , Be similar to babel Of entry Strategy .
summary
Use swc Fast , There must be some side effect, We need to weigh and choose among them , It's essentially a trade off .
Product size tradeoffs
Because it is full import polyfill, Not an on-demand strategy , So the volume of the product will increase 100KB - 200KB , But in such a fast modern network environment , contrast swc It brings us the benefit of compilation speed , It's also acceptable .
Of course, we can also specify higher-order targets , To reduce polyfill The amount of , If you use :
// .browserslistrc
chrome 80
This higher targets A relatively small product volume will be obtained .
contrast esbuild
swc Of polyfill A great advantage of the strategy is that it can put the injection strategy into the framework layer , You don't need to make users feel , Because class babel Of entry Both strategies require “ Show ” In the project code core-js Import , This will make the user feel uncomfortable .
It is positioned as babel Alternative swc The relative ratio is positioned as esbuild ,esbuild This problem cannot be solved at the framework level (umijs And other frameworks that can provide middle tier code ).
other
core-js-compat:modules-by-versions.json
swc issue #1604:Direct usage of core-js-compat
swc issue #2607:swc not polyfill string.replaceAll
Finality
because swc It is the work of the author in his college days , And the author also thinks that its architecture is chaotic , Preparing to refactor , Is expected in the future , We will see better swc , You don't need so much workaround and trade off, Really move towards substitution babel The path of .
bye ,babel !
边栏推荐
- Install MySQL tutorial
- 4 expression
- Error mcrypt in php7 version of official encryption and decryption library of enterprise wechat_ module_ Open has no method defined and is discarded by PHP. The solution is to use OpenSSL
- CL210OpenStack操作的故障排除--章节实验
- [easyexcel] easyexcel checks whether the header matches the tool class encapsulated in easyexcel, including the field verification function. You can use validate to verify
- 六月集训 第二天——字符串
- 集合判断存在交集
- Oracle Database
- libprint2
- 12.13-12.19 summary
猜你喜欢

6 functions
![Set [list] to find out the subscript of repeated elements in the list (display the position of the subscript)](/img/95/67f435646f52646fc6cae8c680d589.jpg)
Set [list] to find out the subscript of repeated elements in the list (display the position of the subscript)

leetcode:剑指 Offer 67. 把字符串转换成整数【模拟 + 分割 +讨论】

报表工具的二次革命

Recommend 17 "wheels" to improve development efficiency

VSCode常用插件

Planning and design of 1000 person medium-sized campus / enterprise network based on ENSP and firewall (with all configuration commands)

Upload file (post form submission form data)

Some operations of MATLAB array

数据库语法相关问题,求解一个正确语法
随机推荐
SQL language
Leetcode: Sword finger offer 67 Convert string to integer [simulation + segmentation + discussion]
Network packet loss troubleshooting
Database syntax related problems, solve a correct syntax
Can official account also bring goods?
SQL injection - Union query
C language pointer
(14) The software version number is displayed in the flash window of blender source code analysis
Tomato learning notes -vscade configuring makefile (using task.jason and launch.jason)
Planning and design of 1000 person medium-sized campus / enterprise network based on ENSP and firewall (with all configuration commands)
leetcode:890. 查找和替换模式【两个dict记录双射(set)】
[data clustering] data set, visualization and precautions are involved in this column
8. form label
丢掉丑陋的 toast,会动的 toast 更有趣
Postman splice replacement parameter loop call interface
Leetcode: Sword finger offer 63 Maximum profit of stock [record prefix minimum and or no brain segment tree]
ConVIRT论文详解(医疗图片)
sql server 2019安装出现错误,如何解决
五、EL 表达式& JSTL 标签库
12.13-12.19 summary