0

我目前有打字稿问题。我正在尝试使用有关使用 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"
  ]
}
4

2 回答 2

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"}

根据我的研究,我可以推断它与不再支持示例中使用的 typescript 和 gulp 版本有关。希望这可以帮助你摆脱莉西亚。

于 2016-05-27T20:19:16.833 回答
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'))        
});  

并确保使用 'exclude' 部分在 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"
    ]
} 
于 2016-03-15T11:26:18.737 回答