【问题标题】:gulp-typescript is compiling all .ts files not just those from specific locationgulp-typescript 正在编译所有 .ts 文件,而不仅仅是来自特定位置的文件
【发布时间】:2016-11-01 19:25:30
【问题描述】:

我有一个 gulp 任务,负责在给定位置转换 .ts 文件并在另一个给定位置输出 .js。

我遇到的问题是,它不仅在发送到目的地之前从给定位置获取 .ts 并转换为 .js,而且对父文件夹和同级文件夹中的所有内容也是如此。

我的文件夹结构如下:

root
|__dist
|
|__source
|  |__bot
|  |__logon

这是我的 gulpfile.js 的简化版本:

var gulp = require("gulp"),
    ts = require("gulp-typescript");

var paths = {
  botScripts: 'source/bot/*.ts',
  releaseBot: 'dist/bot'
};

// Create typescript project
var tsBotProject = ts.createProject('tsconfig.json');

// Create the tasks
gulp.task('botscripts', function() {

  var tsResult = tsBotProject.src(paths.botScripts)
  .pipe(tsBotProject());

  return tsResult.js.pipe(gulp.dest(paths.releaseBot));
});

这是我的 tsconfig.json:

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs"
  },
  "exclude": [
      "node_modules"
  ]
}

正如我所说,它确实可以很好地转换为 .js,但我最终得到的是 all 的 typscript 文件,无论位置如何,都被转换为 .js 并复制到同一个文件夹中dist 下的结构:

root
|__dist
|  |__bot
|  |  |__*.js
|  |__logon
|     |__*.js
|
|__source
|  |__bot
|  |__logon

我在这里做错了什么?

【问题讨论】:

    标签: javascript node.js typescript gulp gulp-typescript


    【解决方案1】:

    tsBotProject.src() 不接受任何参数。您正在尝试将其传递给paths.botScripts,但该参数被简单地忽略了。相反,tsBotProject.src() 会发出您在tsconfig.json 中指定的那些文件。在您的情况下,除了node_modules 之外的所有内容。

    你有两个选择:

    选项 1:您可以像这样在 gulpfile.js 中使用 tsBotProject.src()

    var tsResult = tsBotProject.src()
      .pipe(tsBotProject());
    

    为了使其工作,您还必须将include 属性添加到您的tsconfig.json

    {
      "compilerOptions": {
        "target": "es6",
        "module": "commonjs"
      },
      "include": [
          "source/bot/*.ts"
      ],
      "exclude": [
          "node_modules"
      ]
    }
    

    选项 2: 在您的 gulpfile.js 中使用 gulp.src() 而不是 tsBotProject.src()

    var tsResult = gulp.src(paths.botScripts)
      .pipe(tsBotProject());
    

    【讨论】:

    • 嗨,斯文 - 感谢您的解释。考虑到这一点并再次查看我的 gulpfile.js,这种行为确实有意义。使用我目前的方法,我需要为我想要转换的每一堆文件创建一个新的 tsproject.json 文件——这没有任何意义。所以我所做的是采用选项 2,并且效果很好 - 当然假设它仍然尊重 tsBotProject 中的设置(tsconfig.json 设置)再次感谢!
    猜你喜欢
    • 2022-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-16
    • 1970-01-01
    相关资源
    最近更新 更多