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.
usage
或entry
被使用的时候,@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 usingcore-js@3
,@babel/preset-env
is able to optimize every singlecore-js
entrypoint and their combinations. For example, you might want to only polyfill array methods and newMath
proposals:
导入core-js
为每一个可能的ECMAScript特色加载polyfills。
这么说我是不是可以理解为,如果只使用
import 'core-js'
,,那么会加载所有的polyfills,即使某些polyfills不需要呢?
看到这里可以去看一下core-js在github的repository
useBuiltIns: 'usage'