Gulp不会从ts生成js文件

时间:2016-06-06 15:55:00

标签: node.js import typescript gulp

这是我的Gulp任务:

var gulp = require('gulp');
var typescript = require('gulp-typescript');
var sourcemaps = require('gulp-sourcemaps');
var tsProject = typescript.createProject('tsconfig.json');
var tsSources = [
    './typings/**/*.ts',
    'server/**/*.ts'
];

module.exports = function () {
    var tsResult = gulp.src(tsSources)
        .pipe(sourcemaps.init())
        .pipe(typescript(tsProject));

    return tsResult.js
        .pipe(sourcemaps.write())
        .pipe(gulp.dest('server/'));
};

这是我的tsconfig

{
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "sourceMap": true,
        "watch": true,
        "removeComments": true,
        "moduleResolution": "node",
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "declaration": false,
        "noImplicitAny": false,
        "noLib": false
    },
    "exclude": [
        "fonts",
        "images",
        "img",
        "libs",
        "navigation",
        "node_modules",
        "pagehtmls",
        "typings"
    ]
}

这是我的ts文件:

/// <reference path="../typings/_all.d.ts" />
'use strict';
import {Request, Response} from 'express';
var config = require('../../config/environment'),
    auth = require('../../auth/auth.service');
var mongodb = require('mongodb');
var MongoClient = mongodb.MongoClient;


exports.getBooking = (req: Request, res: Response) => {
    MongoClient.connect(config.mongoERP.uri, (err, db) => {
        if (err) { return res.send(err); }
        db.getCollection('customers').find({ email1: req.params.email }).toArray((err, customer) => {
            if (err) { return res.send(err); }
            return res.send(customer);
        });
    });
}

这是我的tsd.json

{
  "version": "v4",
  "repo": "borisyankov/DefinitelyTyped",
  "ref": "master",
  "path": "typings",
  "bundle": "typings/_all.d.ts",
  "installed": {
    "angularjs/angular.d.ts": {
      "commit": "2d4c4679bc2b509f27435a4f9da5e2de11258571"
    },
    "angular-ui-router/angular-ui-router.d.ts": {
      "commit": "c50b111ded0a3a18b87b5abffea3150d6aca94da"
    },
    "jquery/jquery.d.ts": {
      "commit": "2d4c4679bc2b509f27435a4f9da5e2de11258571"
    },
    "lodash/lodash.d.ts": {
      "commit": "2d4c4679bc2b509f27435a4f9da5e2de11258571"
    },
    "node/node.d.ts": {
      "commit": "2d4c4679bc2b509f27435a4f9da5e2de11258571"
    },
    "socket.io-client/socket.io-client.d.ts": {
      "commit": "2d4c4679bc2b509f27435a4f9da5e2de11258571"
    },
    "serve-static/serve-static.d.ts": {
      "commit": "b57c33fec0be3cba8f5e9cec642db873565923d0"
    },
    "mime/mime.d.ts": {
      "commit": "b57c33fec0be3cba8f5e9cec642db873565923d0"
    },
    "express/express.d.ts": {
      "commit": "470954c4f427e0805a2d633636a7c6aa7170def8"
    },
    "es6-shim/es6-shim.d.ts": {
      "commit": "dade4414712ce84e3c63393f1aae407e9e7e6af7"
    },
    "mongoose/mongoose.d.ts": {
      "commit": "6766ed1d0faf02ede9e2edf2e66bbb2388c825ec"
    }
  }
}

我正在使用gulp cli版本 3.9.0 和本地 3.9.1 它给出错误错误TS1128:声明或声明预期 对于导入 如果我从gulp任务和

中删除'./typings/**/*.ts',
path="../../server.d.ts" />
import {Request, Response} from 'express';
从我的ts文件

它的工作正常但在编译时给出diff错误消息。 我有很多ts文件所以我不能删除它。

2 个答案:

答案 0 :(得分:1)

您不应排除typings中的所有tsconfig文件夹,因为它包含编译器的定义,包括express的定义。您很可能只想排除typings\browser

另外,我建议您使用tsProject.src()代替gulp.src,以便只在一个地方定义项目结构 - tsconfig

希望这有帮助。

答案 1 :(得分:0)

我遇到了问题......

只需更新您的Gulp打字稿

这对我有用。