1. 程式人生 > >babel 使用, 入坑必備 es6 轉 es 5

babel 使用, 入坑必備 es6 轉 es 5

前言, 命令的使用,要多摸索摸索就行 了,此時要保持足夠的耐心,其實很簡單!

加油!

初始化的專案路徑如下:

action.js

let c = (a,b)=>a+b

1 先安裝全域性的babel 腳手架

npm install  -g  --save-dev babel-cli

2, 專案下寫 .babelrc 檔案 , 並將其放在 dss (專案根目錄)路徑下!

{
  "presets": ["env"]
}

3, 配置下package.json

現在要用 babel 將 js 路徑下的 es6 檔案轉化成 es5, 

先看 package.json

{
  "name": "app",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
     "serv": "node server.js --slience --no-error --report=c:\\a.txt",
    "build": "babel js -d build"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
   "devDependencies": {
  
     "babel-cli": "^6.0.0"
  }
}

4, 安裝  env 

5,  npm run build 就行了

重點 是package.json 

{
  "name": "dss",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
      "serv": "node server.js --slience --no-error --report=c:\\a.txt",
    "build": "babel js -d build"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
      "babel-cli": "^6.0.0",
    "babel-preset-env": "^1.7.0"
  }
}

其中 有一句, 就是 將js 下的js 檔案編譯到build 路徑下!("build": "babel js -d build") 就是這句

  最後編譯好的 js 檔案

'use strict';

var a = 10;
var c = function c() {
	console.log('aa');
};

var obj = { a: a };

容易犯錯的幾點總結:

1, 專案沒有node_modules 會報錯!

2, package.json 檔案中, 有幾個重點要寫好!