1.安装babel依赖
npm install --save-dev @babel/core @babel/cli @babel/preset-env npm install --save @babel/polyfill
2.用node初始化一个项目
node init -y
初始化后的package.json长这样
{ "name": "learn2", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo "Error: no test specified" && exit 1" }, "keywords": [], "author": "", "license": "ISC" }
在package.json中增加scripts命令
{ "name": "learn2", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo "Error: no test specified" && exit 1", "build": "babel src -d dest" }, "keywords": [], "author": "", "license": "ISC" }
3.配置 babel.config.js
const presets = [ [ "@babel/env", { targets: { edge: "9", firefox: "60", chrome: "67", safari: "11.1", }, useBuiltIns: "usage", }, ], ]; module.exports = { presets };
4.运行
npm run build