2016-04-03 50 views
1

我一直在尝试使用gulp-typescript取得某种程度的成功,但我有一个小问题。我所有的代码都存储在'src'下,我希望这些代码被编译为'.tmp',但不包含'src'。gulp-typescript:使用createProject的问题

这里是我的代码,我认为这个问题是传递一个值(水珠)到tsProject.src不支持,所以我得到例如

此代码我/.tmp/src/aTypescriptFile.js直接从github回购,我真的不明白为什么gulp.src被替换tsProject.src

任何想法?我确实需要合并我的tsconfig.json文件。

let tsProject = plugins.typescript.createProject('./tsconfig.json'); 
    return tsProject.src('/src/**/*.ts') 
     .pipe(plugins.typescript(tsProject)) 
     .pipe(plugins.sourcemaps.init()) 
     .pipe(plugins.sourcemaps.write('.')) 
     .pipe(gulp.dest('.tmp')); 

**编辑**

更多信息,我已成功使用水珠它与

   return gulp.src('/src/**/*.ts') 

问题更换

   return tsProject.src('/src/**/*.ts') 

来限制是现在我收到关于缺少类型的错误。

 src/testme.ts(4,10): error TS2304: Cannot find name 'require'. 

我的TSCONFIG.JSON文件在这里,它在那里有类型。

{ 
    "compilerOptions": { 
    "target": "ES6", 
    "module": "commonjs", 
    "moduleResolution": "node", 
    "sourceMap": true, 
    "emitDecoratorMetadata": true, 
    "experimentalDecorators": true, 
    "removeComments": false, 
    "noImplicitAny": false 
    }, 
    "files": [ 
    "typings/main.d.ts", 
    "src/testme.ts" 
    ] 
} 

回答

1

所有的路径都应该传递给gulp.src--来源和类型。

让我们有一些路径:

var paths = { 
    lib: "./wwwroot/", 
    ts: ["./sources/**/*.ts"], 
    styles: ["./sources/**/*.scss"], 
    templates: ["./sources/**/*.html"], 
    typings: "./typings/**/*.d.ts", 
    //svg: "./sources/**/*.svg", 
}; 

我们可以通过的源路径的数组吞掉,打字稿:

gulp.task("?typescript:demo:debug", function() { 
    var tsResult = gulp.src([ 
      paths.typings, 
      // <...some other paths...> 
     ].concat(paths.ts)) 
     .pipe(sourcemaps.init()) 
     .pipe(ts({ 
      target: "ES5", 
      experimentalDecorators: true, 
      noImplicitAny: false 
     })); 

    return tsResult.js 
     .pipe(concat(package.name + ".js")) 
     .pipe(sourcemaps.write({ sourceRoot: "" })) 
     .pipe(gulp.dest(paths.lib)); 
}) 

我传递

gulp.src([paths.typings, <...some other paths...>].concat(paths.ts)) 

,但当然,它也可以以更简单的方式完成:

gulp.src([paths.typings, paths.ts]) 
+0

谢谢我接受这个,因为它帮助了我很多,但最后我设法通过保留我的原始代码并向tsconfig.json添加“rootDir”:“src”来解决问题。所以如果有人发现这一点,有两种解决方案。 – Martin