【问题标题】:How can I get gulp-typescript to output to the same directory as the source file?如何让 gulp-typescript 输出到与源文件相同的目录?
【发布时间】:2026-01-20 20:40:01
【问题描述】:

我有以下管道

  function typescripts() {
    return gulp.src(paths.watchedFiles.ts)
        .pipe(cached('typescripts'))
        .pipe(plumber())
        .pipe(addsrc(paths.ts.include))
      //TODO: Need to eliminate the original source path (ex. /users/userName) from the sourcemap file.
        .pipe(sourcemaps.init())
        .pipe(ts(tsProjectMode, undefined, ts.reporter.fullReporter(true))).js
        .pipe(gulpIgnore.exclude(paths.ts.excludeFromPostCompilePipeline))
        .pipe(ngAnnotate({
          remove: false,
          add: true,
          gulpWarnings: false //typescript removes base path for some reason.  Warnings result that we don't want to see.
        }))
        .pipe(sourcemaps.write('.', {includeContent: false}))
        .pipe(gulp.dest(paths.ts.basePath));
  }

我似乎必须根据 src 路径的根将“硬代码”设置为 dest 路径。如果我的 src 路径是 app/modules/**.ts 我的 dest 路径必须是 app/modules。这限制了我使用单个根 src 路径,并且我不能使用兄弟姐妹。

我希望能够使我的 src ['path1/**/*.ts', 'path2/**/*.ts] 并将转译后的输出写入源文件所在的同一文件夹。

【问题讨论】:

    标签: typescript gulp


    【解决方案1】:

    如果你有这样的源文件:

    gulp.src(['path1/**/*.ts', 'path2/**/*.ts])
    

    那就等于:

    gulp.src(['./path1/**/*.ts', './path2/**/*.ts], { base: '.' })
    

    也就是说,你可以把你的目的地放在:

    gulp.dest('.')
    

    因为这是最小的公分母。其余的由 Gulp 完成。

    【讨论】:

    • 对我来说,这种配置会导致输出文件被放置到根目录中。我必须添加base 选项才能获得所需的结果:gulp.src(paths.ts, { base: "." })
    • @DarkDaskin 我希望我事先阅读过此评论。现在我有 100 个文件要从我的根目录清理……我也需要这个解决方案。另一方面,您应该将此作为单独的答案。