Babel-StandaloneBabel 開源類庫
babel-standalone 的設(shè)計(jì)初衷就是基于非 node.js 環(huán)境下使用 #Babel#。這里是原文介紹:
babel-standalone is a standalone build of Babel for use in non-Node.js environments, including browsers. It's bundled with all the standard Babel plugins and presets.
它已經(jīng)包含了Babel所有的插件,雖然本體容量很大(目前版本 6.7.7,未壓縮的js文件1.6MB,壓縮了則為641kb),不過這阻擋不了我們的對(duì)es6的熱愛,使用這個(gè)類庫,可以讓你實(shí)時(shí)在線轉(zhuǎn)換es6為js,而且支持babel提供的各種插件,而且最關(guān)鍵的是全特性支持,包括amd包裹,將import轉(zhuǎn)換為amd的require等等。比如,如下的es6代碼:
import hello from "any!./hello.coffee";
console.log(hello);
let fun = () => console.log('hello es6')
class Test {
}
module.exports = Test;
他會(huì)轉(zhuǎn)換出以下的js:
define(["module", "any!./hello.coffee"], function (module, _hello) {
"use strict";
var _hello2 = _interopRequireDefault(_hello);
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
console.log(_hello2.default);
var fun = function fun() {
return console.log('hello es6');
};
var Test = function Test() {
_classCallCheck(this, Test);
};
module.exports = Test;
});
怎么樣,看著是不是覺得有點(diǎn)小激動(dòng)呢?從此可以徹底擺脫old school的javascript,讓我們更優(yōu)雅的編寫js吧!
安裝使用:bower install babel-standalone --save。
評(píng)論
圖片
表情
