2016-04-03 52 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