5to6 中文文档教程
Note: Experimental
5to6
将(部分)ES5 代码转换为 ES6。 转换后的文件预计将与 ES6 转译器(如 Babel)一起使用。
使用 recast 获取代码的 AST 并检测然后将某些语法修改为 ES6 中的等效语法。 语义不会改变,只会改变语法。
Supported conversions
var b = {
abc: abc
}
=>
var b = {
abc
}
{
abc: function() {
console.log('a')
}
}
=>
{
abc() {
console.log('a')
}
}
module.exports = Component
=>
export default Component
var Foo = require('foo')
=>
import Foo from 'foo'
var Bar = require('foo').Bar
=>
import {Bar} from 'foo'
require('foo')
=>
import 'foo'
var Foo = require('foo')
var Bar = Foo.Bar
var Baz = Foo.Baz
=>
import Foo, {Bar, Baz} from 'foo'
Install
sudo npm install 5to6 -g
Usage
5to6 -s src # converts all js or jsx files in "src" folder (relative to current directory)
5to6 -s . # converts all js or jsx files in current directory
5to6 -s . -v # verbose mode
Caveats
这个库最初是为了将特定项目的代码库转换为 ES6 而创建的,因此它采用了特定的代码结构。 如果你的代码库使用的是 commonjs 风格的模块结构,它应该可以工作。 将所有内容都放在一个大闭包中的代码库将无法正常工作。 同样,这个库是实验性的。
5to6转换后直接写入文件。 所以它依赖于 git,不是为了转换,而是为了在输出不符合预期等情况下进行
恢复。要恢复转换,只需运行
git reset --hard
Credits
Note: Experimental
5to6
Converts (partial) ES5 code to ES6. Converted files are expected to be used with ES6 transpilers like Babel.
Uses recast to get code's AST and detect then modify certain syntax to the equivalent in ES6. Semantics don't change, only the syntax.
Supported conversions
var b = {
abc: abc
}
=>
var b = {
abc
}
{
abc: function() {
console.log('a')
}
}
=>
{
abc() {
console.log('a')
}
}
module.exports = Component
=>
export default Component
var Foo = require('foo')
=>
import Foo from 'foo'
var Bar = require('foo').Bar
=>
import {Bar} from 'foo'
require('foo')
=>
import 'foo'
var Foo = require('foo')
var Bar = Foo.Bar
var Baz = Foo.Baz
=>
import Foo, {Bar, Baz} from 'foo'
Install
sudo npm install 5to6 -g
Usage
5to6 -s src # converts all js or jsx files in "src" folder (relative to current directory)
5to6 -s . # converts all js or jsx files in current directory
5to6 -s . -v # verbose mode
Caveats
This lib was initially created to convert a particular project's codebase to ES6, so it assumes certain code structure. If your codebase is using the commonjs style modules structure, it should work. Codebase with everything in one big closure will not work. Again, this lib is experimental.
5to6 directly writes to file after conversion. So it depends on git, not for conversion, but for reversion in case output is not as expected etc.
To revert conversion, simply run
git reset --hard