Babel-StandaloneBabel 开源类库

联合创作 · 2023-09-22

babel-standalone 的设计初衷就是基于非 node.js 环境下使用 #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.

它已经包含了Babel所有的插件,虽然本体容量很大(目前版本 6.7.7,未压缩的js文件1.6MB,压缩了则为641kb),不过这阻挡不了我们的对es6的热爱,使用这个类库,可以让你实时在线转换es6为js,而且支持babel提供的各种插件,而且最关键的是全特性支持,包括amd包裹,将import转换为amd的require等等。比如,如下的es6代码:

import hello from "any!./hello.coffee";

console.log(hello);

let fun = () => console.log('hello es6')

class Test {

}

module.exports = Test;

他会转换出以下的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;
});

怎么样,看着是不是觉得有点小激动呢?从此可以彻底摆脱old school的javascript,让我们更优雅的编写js吧!

安装使用:bower install babel-standalone --save

浏览 3
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报