javascript – 使用Bable将一个Typescript项目配置为从ES6转换到ES5

我刚刚开始一个新项目,我真的想使用最近为typescript发布的Async和Await东西.

但是,如果你的目标是ES6,它只会起作用(现在).

所以我想知道是否有办法配置Visual Studio(2015 Update 1)来获取由Typescript输出的ES6 java脚本并将其转换为es5?

所以,我会有Typescript – > ES6 – > ES5. (这将在Typescript支持Async / Await目标ES5之前实现.)

最佳答案 可能是,这不完全是你问的问题,但它是一种做同样事情的方法.我希望,它可能是有用的.首先,如此处所述 http://docs.asp.net/en/latest/client-side/using-gulp.html,您可以在VS2015中使用gulp.然后,在tsconfig.json文件中,您应该将typescript编译器选项设置为如下所示:

//tsconfig.json

{
    "compilerOptions": {
        "target": "ES6",
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "module": "commonjs",
        "noImplicitAny": false,
        "removeComments": true,
        "preserveConstEnums": true
    },
    "exclude": [
        ".vscode",
        "node_modules",
        "typings",
        "public"
    ]
}

最后,gulp文件 – 例如我的一个项目 – 用于转换ES6到ES5:

// gulpfile.js

'use strict';

var gulp = require("gulp"),
    ts = require("gulp-typescript"),
    babel = require("gulp-babel");

var tsSrc = [
    '**/*.ts',
    '!./node_modules/**',
    '!./typings/**',
    '!./vscode/**',
    '!./public/**'
];
gulp.task("ts-babel", function () {
    var tsProject = ts.createProject('tsconfig.json');
    return gulp.src(tsSrc)
        .pipe(tsProject())
        .pipe(babel({
            presets: ['es2015'],
            plugins: [
                'transform-runtime'
            ]
        }))
        .pipe(gulp.dest((function (f) { return f.base; })));
});

现在,您可以使用命令gulp ts-babel来转换文件.并且不要忘记安装所需的npm-packages,如babel-preset-es2015和babel-plugin-transform-runtime.

UPD.感谢Ashok M A的关注.将管道(ts())更改为管道(tsProject())

点赞