Gulp和typescript:node.d.ts没有编译

时间:2016-03-15 11:02:23

标签: node.js express typescript gulp tsd

我目前遇到打字稿问题。我正在尝试使用关于使用express构建服务器的简单教程(http://blog.edenmsg.com/angular2-typescript-gulp-and-expressjs/)。 所以我按照这些文件并使用tsd来安装express。然后我尝试按照此文件中的描述启动gulp构建:

gulpfile.js

/**
 * Created by alicia on 10/03/2016.
 */
var gulp = require('gulp');
var path = require('path');
var sourcemaps = require('gulp-sourcemaps');
var ts = require('gulp-typescript');
var del = require('del');
var concat = require('gulp-concat')
var runSequence = require('run-sequence');

// SERVER
gulp.task('clean', function(){
    return del('dist')
});

gulp.task('build:server', function () {
    var tsProject = ts.createProject('server/tsconfig.json');
    var tsResult = gulp.src('server/**/*.ts')
        .pipe(sourcemaps.init())
        .pipe(ts(tsProject))
    return tsResult.js
        .pipe(concat('server.js'))
        .pipe(sourcemaps.write())
        .pipe(gulp.dest('dist'))
});

// CLIENT

/*
 jsNPMDependencies
*/
var jsNPMDependencies = [
    'angular2/bundles/angular2-polyfills.js',
    'systemjs/dist/system.src.js',
    'rxjs/bundles/Rx.js',
    'angular2/bundles/angular2.dev.js',
    'angular2/bundles/router.dev.js'
]

gulp.task('build:index', function(){
    var mappedPaths = jsNPMDependencies.map(file => {return path.resolve('node_modules', file)})

    //Let's copy our head dependencies into a dist/libs
    var copyJsNPMDependencies = gulp.src(mappedPaths, {base:'node_modules'})
        .pipe(gulp.dest('dist/libs'));

    var copyCss = gulp.src('client/css/**/*.css')
        .pipe(gulp.dest('dist/css'));

    //Let's copy our index into dist
    var copyIndex = gulp.src('client/index.html')
        .pipe(gulp.dest('dist'));
    return [copyJsNPMDependencies, copyCss, copyIndex];
});

gulp.task('build:app', function(){
    var tsProject = ts.createProject('client/tsconfig.json');
    var tsResult = gulp.src('client/**/*.ts')
        .pipe(sourcemaps.init())
        .pipe(ts(tsProject))
    return tsResult.js
        .pipe(sourcemaps.write())
        .pipe(gulp.dest('dist/app'))

});
var typescript = require('gulp-tsc');

gulp.task('compile', function(){
    gulp.src(['client/**/*.ts'])
        .pipe(typescript())
        .pipe(gulp.dest('dist/'))
});

gulp.task('build', function(callback){
    runSequence('clean', 'build:server',
        'build:index',
        'build:app',
        callback);
});

gulp.task('default', ['build'])

除了build:server之外,每个任务都编译(clean,build:index ...)。 这是我得到的错误:

[12:07:43] Starting 'build:server'...
server/typings/node/node.d.ts(1759,52): error TS1005: '=' expected.
server/typings/node/node.d.ts(1760,76): error TS1005: '=' expected.
server/typings/node/node.d.ts(1761,52): error TS1005: '=' expected.
server/typings/node/node.d.ts(1761,96): error TS1005: '=' expected.
server/typings/node/node.d.ts(1771,54): error TS1005: '=' expected.
server/typings/node/node.d.ts(1772,74): error TS1005: '=' expected.
server/typings/node/node.d.ts(1773,54): error TS1005: '=' expected.
server/typings/node/node.d.ts(1773,94): error TS1005: '=' expected.

显然问题来自node.d.ts.我真的不知道它为什么不工作,即使它是绝对打字的版本。 你对如何提供帮助有什么想法吗?

编辑:我还包括我的tscongig.json

{
  "compilerOptions": {
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "moduleResolution": "node",
    "module": "commonjs",
    "target": "es6",
    "sourceMap": true,
    "outDir": "dist",
    "declaration": false,
    "jsx": "react",
    "noImplicitAny": false,
    "removeComments": false
  },
  "exclude": [
    "node_modules",
    "typings/browser.d.ts",
    "typings/browser"
  ]
}

2 个答案:

答案 0 :(得分:0)

尝试将您的gulp任务更改为:

gulp.task('build:server', function() 
{
    var tsProject = ts.createProject('server/tsconfig.json');

    var tsResult = tsProject.src()
        .pipe(sourcemaps.init())   
        .pipe(ts(tsProject));  

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

并确保使用'排除'排除tsconfig中的node_modules。像这个例子中的部分:

{ 
    "compilerOptions": { 
        "emitDecoratorMetadata": true, 
        "experimentalDecorators": true,
        "moduleResolution": "node",
        "module": "commonjs", 
        "target": "es6",
        "sourceMap": true,
        "outDir": "dist",
        "declaration": false,
        "jsx": "react"
    },
    "exclude": [
        "node_modules",
        "typings/browser.d.ts",
        "typings/browser"
    ]
} 

答案 1 :(得分:0)

我有同样的问题,我更新了我的package.json文件以包含所有最新版本(对于大多数情况,一些需要旧版本,因为它们是其他版本的依赖项,并且尚不支持更新版本)。这是我的最终package.json文件的样子:

"dependencies": {
  "angular2": "2.0.0-beta.17",
  "es6-promise": "3.2.1",
  "es6-shim": "0.35.1",
  "express": "4.13.4",
  "gulp-concat": "2.6.0",
  "reflect-metadata": "0.1.2",
  "rxjs": "5.0.0-beta.6",
  "systemjs": "0.19.29",
  "zone.js": "0.6.12",
  "typescript": "1.8.10"},
"devDependencies": {
  "del": "2.2.0",
  "gulp": "3.9.1",
  "gulp-concat": "2.6.0",
  "gulp-sourcemaps": "1.6.0",
  "gulp-typescript": "2.13.5",
  "run-sequence": "1.2.1"}

从我的研究中我可以推断它与该示例中使用的打字稿和gulp版本有关,不再受支持了。希望这可以帮助你解决Licia。