【问题标题】:gulp-typescript and outFile option in tsconfig.jsontsconfig.json 中的 gulp-typescript 和 outFile 选项
【发布时间】:2016-01-16 16:46:54
【问题描述】:

我正在使用 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 文件旁边。

我应该如何解决才能在不污染源文件夹的情况下创建所需的输出文件?

【问题讨论】:

    标签: typescript gulp asp.net-core typescript1.6


    【解决方案1】:

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

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

    如果问题不在于使用普通tsc -p . 的外部模块与使用gulp-typescript 的行为方式相同?

    【讨论】:

    • jquery.d.ts 在非外部模块中 - 这不是问题。问题是您是否使用 export 关键字(在 .ts 而不是 .d.ts 中)。有关详细信息,请参阅this
    • 我明白了,我不应该使用模块然后一切正常
    【解决方案2】:

    我建议为此使用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/'));
    });
    

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

      • 但是 outDir 如何帮助我输出单个文件?!
      • 您的outFile 应该像以前一样被发送,并且只有“临时.js”文件应该被发送到outDir。所以outDir 应该可以帮助你摆脱那些污染的 .js 文件。
      • 但是我的问题是我的输出文件是空的!我也需要改变它
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-05
      • 2020-05-07
      • 2016-07-09
      • 2016-10-14
      • 2016-12-29
      • 2018-05-27
      • 1970-01-01
      相关资源
      最近更新 更多