Lebab的ES5至ES6

Lebab是一个工具,它可以将传统的JavaScript代码转换为ES6语法,简化维护工作。通过命令行或编程API,可以将文件或文件模式进行转换。尽管不能一次性执行多个转换,但它仍能为开发者带来便利。
摘要由CSDN通过智能技术生成
Lebab

We all love the goodies that come with ES6, many of them which you can see in Six Tiny But Awesome ES6 Features and Six More Tiny But Awesome ES6 Features, like native class support, arrow functions, and other language improvements.  Now that browsers support most of these syntax additions, many of us are rushing to write ES6 code while cringing at the thought of updating older code.  Maintenance....ain't it a pain?!  Enter Lebab:  a project which transpiles JavaScript written in traditional JavaScript syntax to bright, shiny ES6 syntax!

我们都喜欢ES6附带的优点,您可以在“ 六小但很棒的ES6功能”和“ 六小而出色的ES6功能”中看到许多优点,例如本机类支持,箭头功能和其他语言改进。 现在,浏览器支持这些语法中的大多数,我们中的许多人都急于编写ES6代码,同时渴望更新旧代码。 维护...。不是很痛苦吗?! Enter Lebab :一个将传统JavaScript语法编写JavaScript转换为明亮,闪亮的ES6语法的项目!

Lebab, whose task is the opposite of Babel, is an easy to use command line utility.  Install and then use the command like any other module:

Lebab的任务与Babel相反,它是一个易于使用的命令行实用程序。 安装该命令,然后像使用其他模块一样使用该命令:


$ npm install -g lebab


With Lebab installed you can start to transform your old JavaScript into ES6 beauty.  You can transform a single file or an entire pattern of files:

安装了Lebab之后,您就可以开始将旧JavaScript转换为ES6 beauty。 您可以转换单个文件或整个文件模式:


# single file
$ lebab main.js -o main-es6.js --transform arrow

# pattern: .js files in `src/js`
$ lebab --replace src/js/ --transform arrow 

# pattern: used for any type of matching
$ lebab --replace 'src/js/**/*.jsx' --transform arrow


You must specify one transformation to apply to your legacy JavaScript file:

您必须指定一种转换以应用于旧版JavaScript文件:


# Use arrow functions instead of `function` keyword when possible
$ lebab main.js -o main-es6.js --transform arrow

# Use `let` and `const` instead of `var` when possible
$ lebab main-es6.js -o main-es6.js --transform let

# Use template strings instead of string concatenation
$ lebab main-es6.js -o main-es6.js --transform template


Here's a quick before and after of JavaScript transformed by Lebab:

这是Lebab转换JavaScript前后的快速介绍:


/*
    BEFORE:
*/

// Let/const
var name = 'Bob', time = 'yesterday';
time = 'today';

// Template string
console.log('Hello ' + name + ', how are you ' + time + '?');

var bob = {
  // Object shorthand
  name: name,
  // Object method
  sayMyName: function () {
    console.log(this.name);
  }
};


/*
    AFTER:
*/
// Let/const
const name = 'Bob';

let time = 'yesterday';
time = 'today';

// Template string
console.log(`Hello ${name}, how are you ${time}?`);

const bob = {
  // Object shorthand
  name,
  // Object method
  sayMyName() {
    console.log(this.name);
  }
};


It's frustrating that you can only perform one transformation at a time via the command line, so if you're looking to make things faster, you could use the programmatic API:

令人沮丧的是,您一次只能通过命令行执行一次转换,因此,如果您想使事情变得更快,可以使用编程API:


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


For a list of transformations, their reliability, or even to contribute, check out the Lebab GitHub page.

有关转换的列表,其可靠性甚至可以做出贡献,请查看Lebab GitHub页面

Lebab is an amazing project that could save us all a lot of manual maintenance.  Should you blindly trust everything that comes out of Lebab?  Probably not.  Will even the simplest of Lebab's transformations make our lives easier?  Yes!

Lebab是一个了不起的项目,可以为我们节省很多人工维护。 您是否应该盲目地相信Lebab带来的一切? 可能不是。 即使是最简单的Lebab转型也能使我们的生活更轻松吗? 是!

翻译自: https://davidwalsh.name/lebab

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值