babel的作用不用多說了,是個(gè)學(xué)Node.js的應(yīng)該都知道。在此做個(gè)標(biāo)記,記錄一下babel相關(guān)的npm包和配置方法。

禪城網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),禪城網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為禪城近1000家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)公司要多少錢,請找那個(gè)售后服務(wù)好的禪城做網(wǎng)站的公司定做!
截止到2018年5月2日,我見到了在我看來對(duì)babel講解最詳細(xì)的兩篇文章,分享給大家:
https://excaliburhan.com/post/babel-preset-and-plugins.html
https://github.com/jamiebuilds/babel-handbook/blob/master/translations/zh-Hans/user-handbook.md#toc-configuring-babel
看過這兩篇之后,下面的內(nèi)容各位就可以不用看了,下面是我在初學(xué)初用babel時(shí)做的筆記,不堪入目。
----------------------------------這是分割線---------------------------------------
babel包如下,各有各的作用:
transform-strict-mode (由于很多 ES 特性需要 嚴(yán)格模式才能打開, 添加這個(gè)插件就會(huì)自動(dòng)在所有文件上添加 'use strict';) transform-strict-mode (由于很多 ES 特性需要 嚴(yán)格模式才能打開, 添加這個(gè)插件就會(huì)自動(dòng)在所有文件上添加 'use strict';) transform-es2015-modules-commonjs (將 ES6 模塊標(biāo)準(zhǔn) 轉(zhuǎn)換成 Node.js 用的 CMD 模塊標(biāo)準(zhǔn)) transform-es2015-spread (支持 ES6 的 spread 操作符) transform-es2015-destructuring (支持 賦值解構(gòu)) transform-es2015-parameters (支持默認(rèn)參數(shù), 參數(shù)解構(gòu), 以及其他參數(shù))
安裝包:
# 安裝 core 和命令行工具 $ npm install --save-dev babel-core babel-cli # 安裝所有插件 $ npm install --save-dev babel-plugin-transform-strict-mode babel-plugin-transform-es2015-modules-commonjs babel-plugin-transform-es2015-spread babel-plugin-transform-es2015-destructuring babel-plugin-transform-es2015-parameters
添加配置文件:
需要在項(xiàng)目根目錄下建一個(gè).babelrc的文件。
.babelrc:
{
"plugins": [
"transform-strict-mode",
"transform-es2015-modules-commonjs",
"transform-es2015-spread",
"transform-es2015-destructuring",
"transform-es2015-parameters"
]
}文件組織
由于 Node.js本身有加載器, 所以不需要將所有文件打包成一個(gè)文件, 推薦的做法是, 添加一個(gè) src 目錄, 用于存放 ES6 腳本, 然后將整個(gè)目錄打包到 lib 目錄下, 對(duì)應(yīng)的腳本為
babel src --out-dir lib
開發(fā)調(diào)試的時(shí)候, 可以直接用 babel-cli 模塊提供 babel-node 代替 node 直接啟動(dòng) src 目錄下面的入口腳本。
babel-node src/index.js
最后, 將命令封裝到 package.json 里面:
{
"name": "xxxxx",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "babel-node src/index.js",
"build": "babel src --out-dir lib"
},
"author": "",
"license": "ISC",
"dependencies": {
"babel": "^6.5.2",
"redux": "^3.3.1"
},
"devDependencies": {
"babel-cli": "^6.5.1",
"babel-core": "^6.5.2",
"babel-plugin-transform-es2015-destructuring": "^6.5.0",
"babel-plugin-transform-es2015-modules-commonjs": "^6.5.2",
"babel-plugin-transform-es2015-parameters": "^6.5.0",
"babel-plugin-transform-es2015-spread": "^6.5.2",
"babel-plugin-transform-strict-mode": "^6.5.2"
}
}封裝命令的重點(diǎn)是:
"scripts": {
"start": "babel-node src/index.js",
"build": "babel src --out-dir lib"
},我們就可以使用下面的命令啟動(dòng)和編譯我們的代碼了
# npm run build 構(gòu)建腳本 # npm start 使用 babel-node 啟動(dòng)進(jìn)程
當(dāng)然,不用每次都執(zhí)行構(gòu)建腳本的命令,只需要 #npm start 這一個(gè)命令也是可以的。
本文參考http://taobaofed.org/blog/2016/01/07/find-back-the-lost-es6-features-in-nodejs/
分享題目:node.js學(xué)習(xí)筆記之babel使用
網(wǎng)站URL:http://chinadenli.net/article22/ghoecc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、建站公司、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)公司、、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)