gulp-typescript icon indicating copy to clipboard operation
gulp-typescript copied to clipboard

Source maps not created with tsconfig.json

Open bgever opened this issue 11 years ago • 12 comments

Running gulp with gulp-typescript does not create source maps, however, tsc creates the source map files with the following tsconfig.json file:

{
    "compilerOptions": {
        "module": "commonjs",
        "noImplicitAny": true,
        "sourceMap": true
    }
}

My gulpfile.js looks like this:

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

var tsProject = ts.createProject('tsconfig.json');

gulp.task('default', function () {
  var tsResult = tsProject
    .src()
    .pipe(ts(tsProject));

  return tsResult.js
    .pipe(gulp.dest('.'));
});

bgever avatar Sep 06 '15 10:09 bgever

You can use gulp-sourcemaps to get source maps. See the readme for more information.

ivogabe avatar Sep 06 '15 12:09 ivogabe

I've tried that with the following gulpfile.js per the docs. The source maps are created inline, however they do not work. The Visual Studio Code node debugger is not able to consume them. The external ones created by tsc work fine. Am I doing something wrong?

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

var tsProject = ts.createProject('tsconfig.json');

gulp.task('default', function () {
  var tsResult = tsProject
    .src()
    .pipe(sourcemaps.init())
    .pipe(ts(tsProject));

  return tsResult.js
    .pipe(sourcemaps.write())
    .pipe(gulp.dest('.'));
});

Is there a reason why gulp-typescript doesn't use the built-in source map support of tsc?

bgever avatar Sep 06 '15 12:09 bgever

You can use sourcemaps.write('.') to write source maps to .js.map files instead of inlining them.

Using gulp-sourcemaps is the standard way to use source maps in gulp, not using it would be a bad idea. The advantage of gulp-sourcemaps is that you can easily add other plugins that support source maps, like uglify or concat.

ivogabe avatar Sep 06 '15 13:09 ivogabe

I've been able to find that with the previous gulp-sourcemaps, the "sources" array that's embedded in the base64 url contained a file path, causing the debugger to break. When trying to use sourcemaps.write('.') it does create external .js.map files, but has also the incorrect base path included. Any idea how I can force the paths to be relative to the path where the file is located?

bgever avatar Sep 06 '15 13:09 bgever

I think you can use the sourceRoot option for that, you can see more info in the docs of gulp-sourcemaps.

ivogabe avatar Sep 06 '15 13:09 ivogabe

Thanks for the pointer and the quick replies @ivogabe!

I came across this related issue in gulp-sourcemaps, describing the relative root requirement.

The following gulpfile.js which creates inline source maps is now working great for me.

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

var tsProject = ts.createProject('tsconfig.json');

gulp.task('default', function () {
  var tsResult = tsProject
    .src()
    .pipe(sourcemaps.init())
    .pipe(ts(tsProject));

  return tsResult.js
    .pipe(sourcemaps.write({
      // Return relative source map root directories per file.
      sourceRoot: function (file) {
        var sourceFile = path.join(file.cwd, file.sourceMap.file);
        return path.relative(path.dirname(sourceFile), file.cwd);
      }
    }))
    .pipe(gulp.dest('.'));
});

Thanks for your help, I'll close the issue.

bgever avatar Sep 06 '15 14:09 bgever

Good to hear that!

ivogabe avatar Sep 06 '15 15:09 ivogabe

This seems to work:

.pipe(sourcemaps.write('.', {
           sourceRoot: function(file){ return file.cwd + '/src'; }
      }))

Credit: http://stackoverflow.com/questions/34218374/vscode-breakpoint-not-working-in-typescript-with-sourcemap-generated-by-gulp-sou

srowan avatar Feb 01 '16 01:02 srowan

@srowan example worked for me, @bgever solution isn't working for some reason I can't pinpoint

ashblue avatar May 09 '16 21:05 ashblue

Hi @srowan, thx for the info, apparently You have a mistake (maybe only on windows) - it shouldn't be /src but "\src" because the SourceRoot has a format of "sourceRoot":"d:\Code\TypeScript\FirstCode\src"} so your original code generates: "sourceRoot":"d:\Code\TypeScript\FirstCode/src"} Your solution works perfectly

@bgever - Your code creates relative URLs, but in the end it generates empty string... I am using vscode + gulp tasks on windows so maybe this is the reason... Apparently there are some problems with gulp-sourcemaps and vscode, I think the easiest way now is to generate the absolute paths

Miszkaaaa avatar Jun 05 '16 16:06 Miszkaaaa

I got this solution to work with vscode + gulp-sourcemaps. The gulpfile automatically figures out the relative path for sourceRoot. FYI I'm using inline sourcemaps.

directory structure:

project root
  - src
    - index.ts // source file
  - lib
    - src
      - index.js // transpiled file with inline sourcemap

gulpfile.js:

.pipe(sourcemaps.write({
  sourceRoot: function (file) {
    return path.relative(path.join(file.cwd, file.path), file.base);
}}))

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch",
            "type": "node",
            "request": "launch",
            "program": "${workspaceRoot}/src/index.ts",
            "outDir": "${workspaceRoot}/lib/src",
            "sourceMaps": true,
            "stopOnEntry": false,
            "args": [],
            "cwd": "${workspaceRoot}",
            "runtimeExecutable": null,
            "runtimeArgs": [
                "--nolazy"
            ],
            "env": {
                "REDACTED": "values"
            },
            "externalConsole": false
        }
    ]
}

westy92 avatar Sep 02 '16 01:09 westy92

I finally got it working (in Windows), based on the solution of @westy92, with gulp 3.9.1, gulp-sourcemaps 2.6.1, gulp-typescript 3.2.3.

directory structure:

root
  - src // source files, in multiple directories
    - index.ts
    - routes
      - router.ts
  - dist // compiled output
    - index.js
    - routes
      - router.js

tsconfig.json

{
  "compilerOptions": {
    "alwaysStrict": true,
    "inlineSourceMap": true,
    "strictNullChecks": true,
    "target": "es6",
    "module": "commonjs",
    "moduleResolution": "node",
    "outDir": "dist"
  },
  "include": [
    "src/**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ]
}

gulpfile.js

const gulp = require('gulp');
const ts = require('gulp-typescript');
const path = require('path');
const sourcemaps = require('gulp-sourcemaps');

const tsProject = ts.createProject('tsconfig.json');

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

  return tsResult.js
    .pipe(sourcemaps.write({
      includeContent: false,
      sourceRoot: function (file) {
        // Difference from westy92's solution:
        //   file.path contains the absolute file path (including file name),
        //   so I get the file's directory and calculate the relative path to the base directory.
        return path.relative(path.dirname(file.path), file.base);
      }
    }))
    .pipe(gulp.dest('dist'));
});

launch,json

{
  "version": "0.2.0",
  "configurations": [{
    "type": "node",
    "request": "launch",
    "name": "Launch Program",
    "program": "${workspaceFolder}/src/index.ts",
    "outFiles": [
      "${workspaceFolder}/dist/**/*.js"
    ]
  }]
}

This produces the following sourcemap data inline in the appropriate files:

{
  "version": 3,
  "sources": ["../src/index.ts"],
  "names": [],
  "mappings": "...",
  "file": "index.js",
  "sourceRoot": ""
}

{
  "version": 3,
  "sources": ["../src/routes/router.ts"],
  "names": [],
  "mappings": "...",
  "file": "routes/router.js",
  "sourceRoot": ".."
}

DanielRose avatar Nov 10 '17 09:11 DanielRose