【babel】使用babel搭建编译ES6环境

gengboxb 459 0

1、全局安装babel-cli

npm install -g babel-cli

2、项目安装babel-preset-es2015 和 babel-cli

npm install --save-dev babel-preset-es2015 babel-cli

看一下我们的package.json文件,已经多了devDependencies选项

"devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-preset-es2015": "^6.24.1"
 }

3、在根目录下新建.babelrc文件,并编写

{
    "presets":[
        "es2015"
    ],
    "plugins":[]
}

4、配置命令npm run build打包

"build": "babel src/js/index.js -o dist/js/index.js"

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "babel src/js/index.js -o dist/js/index.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "jquery": "^1.8.3"
  },
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-preset-es2015": "^6.24.1"
  }
}

发表评论 取消回复
表情 图片 链接 代码

分享