参考:https://github.com/lebab/lebab#safe-transforms

作用

Turn your ES5 code into readable ES6 (sugar-syntax). It does the opposite of what Babel does.
将你的 ES5 代码转换为可读的 ES6,它的做法与 Babel 相反。

安装

  1. npm install -g lebab

使用

转换单个文件,输出转换后的代码

  1. // file: 需要转换的文件
  2. // transform: 需要转换的语法
  3. lebab <file> -t <transform>

转换单个文件,另存转换后的代码

  1. // out-file: 转换后保存代码的文件
  2. lebab <file> -o <out-file> -t <transform>

转换目录中的文件

  1. // dir: 需要转换的目录,可以使用正则匹配
  2. lebab --replace <dir> -t <transform>

在代码中使用

  1. // code: 需要转换的代码
  2. lebab.transform(code, transform);

示例

1.将es5.js里的回调函数转换为 => :

  1. lebab es5.js -t arrow

es5.js:

  1. $(window).scroll(function () {
  2. checkScroll();
  3. });

输出:

  1. $(window).scroll(() => {
  2. checkScroll();
  3. });

2.将es5.js里的var转换为let/const, 并保存到es6.js

  1. lebab es5.js -o es6.js -t let

es5.js:

  1. var a = '1', b = '2';

es6.js:

  1. const a = '1', b = '2';

3.将es5.js里的var转换为let/const, 回调函数转换为 =>, 并保存到es6.js:

  1. labeb es5.js -o es6.js -t 'arrow,let'

es5.js:

  1. var a = '1', b = '2';
  2. var c = function () {
  3. alert('3');
  4. };
  5. var d;
  6. d = '4';
  7. console.log(a, b, c(), d);

es6.js:

  1. const a = '1', b = '2';
  2. const c = () => {
  3. alert('3');
  4. };
  5. let d;
  6. d = '4';
  7. console.log(a, b, c(), d);

4.在代码中将ES5语法转换为ES6:

  1. import lebab from 'lebab';
  2. const {code, warnings} = lebab.transform('var f = function(){};', ['let', 'arrow']);
  3. console.log(code); // -> "const f = () => {};"

5.将es5.js里的prototype语法转换成class:

  1. lebab editor.js -o editor-es6.js --transform class

editor.js:

  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var promise_map_1 = require("./promise-map");
  4. var Editor = /** @class */ (function () {
  5. function Editor(config) {
  6. this.config = config;
  7. }
  8. Object.defineProperty(Editor.prototype, "width", {
  9. get: function () {
  10. return this.config.width;
  11. },
  12. enumerable: true,
  13. configurable: true
  14. });
  15. Object.defineProperty(Editor.prototype, "height", {
  16. get: function () {
  17. return this.config.height;
  18. },
  19. enumerable: true,
  20. configurable: true
  21. });
  22. Editor.prototype.addElement = function (data) {
  23. var payload = JSON.parse(JSON.stringify(data));
  24. var asyncId = Math.random();
  25. return this.postMessage('editor.addElement', payload, asyncId);
  26. };
  27. Editor.prototype.setWatermark = function (url) {
  28. var asyncId = Math.random();
  29. return this.postMessage('editor.setWatermark', url, asyncId);
  30. };
  31. Editor.prototype.postMessage = function (action, payload, asyncId) {
  32. var _this = this;
  33. var send = function () {
  34. _this.config.frame.contentWindow.postMessage({
  35. action: action,
  36. asyncId: asyncId,
  37. payload: payload
  38. }, '*');
  39. };
  40. if (asyncId) {
  41. return new Promise(function (resolve) {
  42. send();
  43. promise_map_1.default[asyncId] = resolve;
  44. });
  45. }
  46. send();
  47. };
  48. return Editor;
  49. }());
  50. exports.default = Editor;

editor-es6.js:

  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var promise_map_1 = require("./promise-map");
  4. var Editor = /** @class */ (function () {
  5. class Editor {
  6. constructor(config) {
  7. this.config = config;
  8. }
  9. get width() {
  10. return this.config.width;
  11. }
  12. get height() {
  13. return this.config.height;
  14. }
  15. addElement(data) {
  16. var payload = JSON.parse(JSON.stringify(data));
  17. var asyncId = Math.random();
  18. return this.postMessage('editor.addElement', payload, asyncId);
  19. }
  20. setWatermark(url) {
  21. var asyncId = Math.random();
  22. return this.postMessage('editor.setWatermark', url, asyncId);
  23. }
  24. postMessage(action, payload, asyncId) {
  25. var _this = this;
  26. var send = function () {
  27. _this.config.frame.contentWindow.postMessage({
  28. action: action,
  29. asyncId: asyncId,
  30. payload: payload
  31. }, '*');
  32. };
  33. if (asyncId) {
  34. return new Promise(function (resolve) {
  35. send();
  36. promise_map_1.default[asyncId] = resolve;
  37. });
  38. }
  39. send();
  40. }
  41. }
  42. return Editor;
  43. }());
  44. exports.default = Editor;

6.将es5.js里可能会出现的语法转换成es6,es7语法:

  1. lebab index.js -o index-es6.js --transform 'arrow,arrow-return,for-of,for-each,arg-rest,arg-spread,obj-method,obj-shorthand,no-strict,exponent,multi-var,class,let,commonjs,template,default-param,destruct-param,includes'

插件

Lebab 为 Atom 和 Sublime 提供了插件,方便在编辑器中进行操作。
Atom: https://github.com/ga2mer/atom-lebab
Sublime: https://github.com/inkless/lebab-sublime