第一步:新建基础项目结构,如图:
src目录下index.js内容:
let a = 1;
console.log(a);
第二步:初始化
npm init -y
第三步:安装babel-cli(-g表示全局安装,如已安装可直接进行第四步)
npm install -g babel-cli
第四步:安装es6转es5插件并且本地安装babel-cli
cnpm install --save-dev babel-preset-es2015 babel-cli
第五步:项目根目录下添加.babelrc文件
{
"presets":[
"es2015"
],
"plugins":[]
}
第六步:packsge.json里设置打包命令
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "babel src/index.js -o dist/index.js"
},
"keywords": [],
"author": "qiufujian",
"license": "ISC",
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-preset-es2015": "^6.24.1"
}
}
第七步:终端执行npm run build命令,查看dist文件夹下转换好的index.js文件,如下则表示搭建成功
"use strict";
var a = 1;
console.log(a);