1.安装babel-cli babel-preset-env
首先通过npm init -y
命令生成package.json
然后命令行输入
npm install --save-dev babel-cli babel-preset-env
2.配置.babelrc文件
2.1.presets预设配置
{
"presets": [
"env"
]
}
2.2plugin插件配置
暂时不表
也可以通过package.json指定babel文件位置
{
"name": "my-package",
"version": "1.0.0",
"babel": {
// my babel config here
}
}
3增加scripts命令,进行编译
{
"name": "babel-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "babel src -d dist"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-preset-env": "^1.7.0"
}
}
然后只需要 npm run build就ok啦
所有src的js文件都会编译到dist中
比如
let a = res => {
return `ds${res}fsfd`
}
就会被编译成
"use strict";
var a = function a(res) {
return "ds" + res + "fsfd";
};