初学使用Babel

Babel

记住官网链接babeljs.io

Docs

General

Presets

env
options
useBuiltIns

看stackoverflow上关于对这个usage和entry用法的解释,使用usage的话,自己不需要添加import

"usage" | "entry" | false, defaults to false
This options configures how @babel/preset-env handles polyfills

when either the usage or entry options are used, @babel/preset-env will add direct reference to core-js modules as bare imports (or requires). This means core-js will be resolved relative to the file itself and needs to be accessible.

since @babel/polyfill was deprecated in 7.4.0, we recommend directly adding core-js and setting the version via the corejs option.

usageentry被使用的时候,@babel/preset-env会添加直接的引用给core-js模块作为赤裸裸的imports或是requires。就是说core-js会相对于这个文件自身而被解析,并且需要能被访问。

直接添加core-js并且通过corejs选项设置版本

注意,webpack中的这个选项

options: {
	presets: ["@babel/preset/env"]
}    

和.babelrc文件有冲突。 如果两个都使用了的话,通过我的测试,.babelrc里的很多选项都是想了,比如useBuiltIns, 可以通过修改名字来判断,即没有报错说明根本没用到这个选项。

此外,直接这在webpack里这样设置的话,并且没有使用Import的话,那么babel基本上没用上core-js。

那到底在webpack的babel-loader中怎么配置这个Options呢,这个就要看babel的教程了,就在Docs - Usage - Options里面。比如里面就提到哪些选项可以被用在babel的整合用户中,就像babel-loader这样的用户。能用的经测试,babelrc配置boolean,可以决定webpack是不是用.babelrc,经测试如果false那么基本上core-js没用上了。

useBuiltIns: 'entry'

This option enables a new plugin that replaces the import “core-js/stable”; and import “regenerator-runtime/runtime” statements (or require(“corejs”) and require(“regenerator-runtime/runtime”)) with individual requires to different core-js entry points based on environment.

这个选项会开启一个新的插件,这个插件会使用,独立的requires不同的基于环境的入口点,来代替上面提到的这些import或require。

Importing "core-js" loads polyfills for every possible ECMAScript feature: what if you know that you only need some of them? When using core-js@3, @babel/preset-env is able to optimize every single core-jsentrypoint and their combinations. For example, you might want to only polyfill array methods and new Math proposals:

导入core-js为每一个可能的ECMAScript特色加载polyfills。

这么说我是不是可以理解为,如果只使用import 'core-js',,那么会加载所有的polyfills,即使某些polyfills不需要呢?

看到这里可以去看一下core-js在github的repository

useBuiltIns: 'usage'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值