1、@babel/corenode
var babel = require("@babel/core");
babel.transform(code, options, function(err, result) { result; // => { code, map, ast } });
2、@babel /cliandroid
babel src --out-dir lib
3、presetswebpack
preset 执行顺序从右向左ios
二、将 JS特性 跟 特定的babel插件 创建映射,映射关系能够参考 这里。git
三、stage-x 的插件不包括在内。es6
四、根据开发者的配置项,肯定至少须要包含哪些插件。好比声明了须要支持 IE8+、chrome62+,那么,全部IE8+须要的插件都会被包含进去。github
2、配置web
若是不写任何配置项,env 等价于 latest,也等价于 es2015 + es2016 + es2017 三个相加(不包含 stage-x 中的插件)。env 包含的插件列表维护在这里chrome
babel 默认只转换 js 语法,而不转换新的 API,好比 Iterator、Generator、Set、Maps、Proxy、Reflect、Symbol、Promise 等全局对象,以及一些定义在全局对象上的方法(好比 Object.assign
)都不会转码。浏览器
const presets = [ ['@babel/env', { // chrome, opera, edge, firefox, safari, ie, ios, android, node, electron // targets 和 browerslist 合并取最低版本 targets: { ie: 8, // 是否使用 esmodules esmodules: true, }, // 启用更符合规范的转换,但速度会更慢,默认为 `false`,从目前来看,是更严格的转化,包括一些代码检查。 spec: false, // 有两种模式:normal, loose。其中 normal 更接近 es6 loose 更接近 es5 loose: false, // "amd" | "umd" | "systemjs" | "commonjs" | "cjs" | false, defaults to "commonjs" modules: false, // 打印插件使用状况 debug: true, // 按需增长移除一些功能 // include: [], // exclude: [], // 增长 polyfills // 按需使用 // useBuiltIns: 'usage', // 引用一次 // useBuiltIns: 'entry', // 不引用,独自使用 // useBuiltIns: false, // 强制使用全部的插件 // forceAllTransforms: false, // 配置 browerslist 的位置 // configPath: , // 配置是否忽略 browerslist 文件 // ignoreBrowserslistConfig: false, // 是否跳过 proposal 的文件 // shippedProposals: false, }] ]; const plugins = [ [ // 重用把 babel 注入的帮助代码, 依赖 @babel/runtime "@babel/plugin-transform-runtime", { // false || 2, 变成全局或者局部 "corejs": false, // 生成器运行时的使用,变成全局或者局部 "regenerator": true, // 帮助函数是变成 inline, 仍是 lib "helpers": true, // helpers 切换成 esm "useESModules": true } ] ]; module.exports = { presets, plugins };
useBuiltIns:
"usage"
| "entry"
| false
, defaults to false
.
4、plugins
plugin 执行顺序 从左到右
插件在 Presets 前运行
5、@babel/plugin-transform-runtime
默认状况下babel 会在每一个文件中引入helper ,致使重复引入文件变大,@babel/plugin-transform-runtime,all of the helpers will reference the module @babel/runtime
to avoid duplication across your compiled output. The runtime will be compiled into your build.
@babel/runtime vs @babel/runtime-corejs2
.
默认配置:
{ "plugins": [ [ "@babel/plugin-transform-runtime", { "absoluteRuntime": false, "corejs": false, "helpers": true, "regenerator": true, "useESModules": false } ] ] }
@babel/runtime 默认 转化 不转换新的 API,好比 Iterator、Generator、Set、Maps、Proxy、Reflect、Symbol、Promise 等全局对象
若是要转化用 @babel/runtime-corejs2
. 同时开启 { corejs: 2 }
@babel/plugin-transform-runtime 不转化实例方法
5、@balbel/polyfill
Object.assign
)都不会转码。
When used alongside @babel/preset-env
,
If useBuiltIns: 'usage'
is specified in .babelrc
then do not include @babel/polyfill
in either webpack.config.js
entry array nor source. Note, @babel/polyfill
still needs to be installed.
If useBuiltIns: 'entry'
is specified in .babelrc
then include @babel/polyfill
at the top of the entry point to your application via require
or import
as discussed above.
If useBuiltIns
key is not specified or it is explicitly set with useBuiltIns: false
in your .babelrc, add @babel/polyfill
directly to the entry array in your webpack.config.js
.