简体   繁体   English

Typescript + Gulp + Sourcemaps错误的源路径

[英]Typescript + Gulp + Sourcemaps wrong source path

I have a nodejs app with the following folder structure: 我有一个具有以下文件夹结构的nodejs应用程序:

project
|-- src/
|   |-- controllers/
|   |   |`-- authorize-controller.ts
|   |`-- index.ts
|--dist/
|   |--controllers/
|   |   |`-- authorize-controller.js
|   |   |`-- authorize-controller.js.map
|   |`-- index.js
|   |`-- index.js.map
`-- gulpfile.js
`-- tsconfig.json

The sourcemaps are generated. 源图已生成。 The index.js.map points to "../src/index.ts" (correct). index.js.map指向“ ../ src / index.ts”(正确)。 The corresponding content of the map file is {"version":3,"sources":["../src/index.ts"],"names":[],"mappings" 映射文件的相应内容为{"version":3,"sources":["../src/index.ts"],"names":[],"mappings"

But the dist\\controllers\\authorize-controller.js.map points to the wrong directory. 但是dist \\ controllers \\ authorize-controller.js.map指向错误的目录。 It has {"version":3,"sources":["../src/controllers/authentication.controller.ts"],"names":[], . 它具有{"version":3,"sources":["../src/controllers/authentication.controller.ts"],"names":[], There is one ../ missing. 有一个../失踪了。

My gulpfile.js is: 我的gulpfile.js是:

gulp.task('compile', () => {
   var tsResult = tsProject.src()
     .pipe(sourcemaps.init())
     .pipe(tsProject());

   return tsResult.js
     .pipe(sourcemaps.write('.', { includeContent: false, sourceRoot: '/src' }))
     .pipe(gulp.dest('dist'));
});

My tsconfig.json is: 我的tsconfig.json是:

{
   "compilerOptions": {
      "module": "commonjs",
      "target": "es6",
      "noImplicitAny": false,
      "sourceMap": true,
      "outDir": "dist"
   },
   "include": [
      "src/**/*.ts"
   ],
   "exclude": [
      "node_modules",
      "dist",
      ".vscode"
   ]
}

What am I doing wrong? 我究竟做错了什么? It seems the sourceRoot is ignored by gulp-sourcemaps. 看来gulp-sourcemaps忽略了sourceRoot。

Ok I found a solution to get the breakpoints hit. 好的,我找到了解决问题的方法。 I looked on the wrong part of the sourcefile. 我查看了源文件的错误部分。 "sources":["../src/controllers/authentication.controller.ts"] is always the same and can be ignored. "sources":["../src/controllers/authentication.controller.ts"]始终相同,可以忽略。 If I change the gulp task to use sourceRoot: '../src' it works. 如果我将sourceRoot: '../src'任务更改为使用sourceRoot: '../src'则它可以工作。 At the end of the sourcemap-file, the sourceRoot is set. 在sourcemap文件的末尾,设置了sourceRoot。

This is my new gulp task: 这是我的新任务:

gulp.task('compile', () => {
   var tsResult = tsProject.src()
      .pipe(sourcemaps.init())
      .pipe(tsProject());

   return tsResult.js
      .pipe(sourcemaps.write('.', { includeContent: false, sourceRoot: '../src' }))
      .pipe(gulp.dest('dist'));
});

My source files are located at "src" folder at the project root folder and all output with corresponding folder structure goes into "dist" folder similar to yours. 我的源文件位于项目根文件夹的“ src”文件夹中,所有具有相应文件夹结构的输出都进入与您相似的“ dist”文件夹。 Here is my "compile" task that I think can help you : 这是我认为可以帮助您的“编译”任务:

var gulp = require("gulp");
var tsc  = require("gulp-typescript");
var sourcemaps = require('gulp-sourcemaps');

        gulp.task("compile", function () {
      var tsProject = tsc.createProject('tsconfig.json');
      return gulp
        .src("src/**/*.ts") 
        .pipe(sourcemaps.init())
        .pipe(tsProject())
        .pipe(sourcemaps.write(
          ".",
        {   
          sourceRoot: function(file) {
            var filePathSplit = file.sourceMap.file.split("/");
            var backTrack = '../'.repeat(filePathSplit.length-1) || '../' ;
            var filePath = backTrack+ "src/";
          return filePath;
        }}
         ))
        .pipe(gulp.dest("dist"));
    });

Hope this will help. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM