提起babel,前端er大概都不陌生。可是为何要有babel呢?解决了什么问题?怎么使用babel呢?注意点在哪?如下就从这几个方面总结一下我关于babel学习的结果吧。前端
距离ES2015提出已经有几年了,各个浏览器厂商也在积极地支持着各个好用的ES6的新特性和新语法。可是还有许多的东西仍是不支持的。因此这个时候就须要有一个编译器,把ES6+的语法转换成<=ES5的语法。git
yarn add @babel/core @babel/cli @babel/preset-env -D yarn add @babel/polyfill
配置好babel.config.js或者.babelrc(只须要配置一个就行)
babel.config.jsgithub
module.exports = function(api) { api.cache(true); // 这句要加上 const presets = [ [ "@babel/env", { targets: { // ie: '9', edge: "17", firefox: "60", chrome: "67", safari: "11.1", }, useBuiltIns: "usage", }, ], ]; const plugins = [ ["@babel/plugin-transform-arrow-functions", { "spec": true }] ]; return { presets, plugins } };
const presets = [ [ "@babel/env", { targets: { // ie: '9', edge: "17", firefox: "60", chrome: "67", safari: "11.1", }, useBuiltIns: "usage", }, ], ]; const plugins = [ ["@babel/plugin-transform-arrow-functions", { "spec": true }] ];
module.exports = {
presets,
plugins
};chrome
.babelrc
js
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
// ie: '9',
"edge": "17",
"firefox": "60",
"chrome": "67",
"safari": "11.1",
},
"useBuiltIns": "usage",
},
]
],
"plugins": [
"@babel/plugin-transform-arrow-functions"
]
}
```api
Trying to build Jest is throwing “Caching was left unconfigured.”浏览器
Babel 插件手册bash