4

我正在使用 gulpfile.js 以这种方式编译我的 tsconfig.json 项目:

var ts = require('gulp-typescript');

var tsProject = ts.createProject('Modeler/tsconfig.json',
    { typescript: require('typescript') });

gulp.task('build:ts', function () {
    var tsResult = tsProject.src()
        .pipe(ts(tsProject));
    return tsResult.pipe(gulp.dest('wwwroot/app'));
});

它工作正常并编译我的打字稿文件并将 js 文件放入 wwwroot/app 文件夹

问题出现了,然后我希望 TypeScript 编译器使用 outFile 属性输出单个连接文件

{
  "compilerOptions": {
    "noImplicitAny": false,
    "noEmitOnError": true,
    "removeComments": false,
    "sourceMap": true,
    "target": "es5",
    "module": "commonjs",
    "outFile": "modeler.js"
  },
  "exclude": [
    "node_modules",
    "wwwroot"
  ]  
}

gulp 任务确实会创建一个空的输出文件并创建 *.js 文件,其中的内容位于 *.ts 文件旁边。

我应该修复什么来创建所需的输出文件而不污染我的源文件夹?

4

3 回答 3

7

你确定你的 TypeScript 源代码中没有外部模块吗?

在这种情况下,我遇到了类似的行为。使用选项时不允许使用外部模块outFile,但编译器不会抱怨并发出空文件。此外,外部模块应该在“某处”(在 中outDir)发出。

如果问题不在于使用 plaintsc -p .的外部模块与使用gulp-typescript?

于 2015-10-18T16:26:03.110 回答
3

为此,我建议使用gulp-concat 。做不止一件事的 Gulp 包违背了 Gulp 的原则。

var concat = require('gulp-concat');

gulp.task('scripts', function() {
  return gulp.src('./lib/*.js')
    .pipe(concat('all.js'))
    .pipe(gulp.dest('./dist/'));
});
于 2015-10-19T06:57:25.573 回答
1

我相信这就是--outDirTypeScript 编译器的选择。请参阅https://github.com/Microsoft/TypeScript/wiki/Compiler-Options并查看 gulp-typescript文档,其中指定:

outDir (string) - 将输出移动到不同的(虚拟)目录。请注意,您仍然需要 gulp.dest 将输出写入磁盘。

于 2015-10-18T09:16:59.913 回答