question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to generate sourcemap that contains relative 'sources' and 'files', as tsc does?

See original GitHub issue

I am building a typescript nodejs project using VSCode.

The gulp code is:

var tsProject = ts.createProject(tsConfigFile);
  return tsProject.src()
    .pipe(sourcemaps.init())
    .pipe(ts(tsProject))
    .js
    .pipe(sourcemaps.write('.', {includeContent: false, sourceRoot: ''})) 
    .pipe(gulp.dest('.'));

For files in subdirectories, such as routes/users.ts, the content of the generated users.js.map is

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

The breakpoint is not working in VSCode, so I tried to use tsc to generate the sourcemap. The sourcemap generated by tsc is

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

and it works fine in VSCode.

So how to config gulp-sourcemaps to generate the sourcemap as the tsc does?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:2
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
Nepoxxcommented, Jan 11, 2017

This is what I have and it works with VSCode although the output is not exactly like TSC’s output:

return gulp.src(['typings/index.d.ts', 'src/**/*.ts'])
    .pipe(sourcemaps.init())
    .pipe(tsProject(ts.reporter.longReporter()))
    .js
    .pipe(sourcemaps.write({
      mapSources: (path) => path, // This affects the "sources" attribute even if it is a no-op. I don't know why.
      sourceRoot: (file) => {
        return path.relative(file.relative, path.join(file.cwd, 'src'));
      }
    }))
    .pipe(gulp.dest('dist'));
1reaction
gravatoncommented, Jul 10, 2016

I have a similar issue, although the differences are a little more troubling - if I write out sourcemaps with the following invocation:

.pipe(sourcemaps.write('.')

The sourcemaps have the following at the end:

"sourceRoot":"/source"

How this gets there is baffling to me as no such directory is used anywhere in my compilation steps.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Emitting TypeScript Source Maps | Building SPAs - Carl Rippon
Generating source map files ​​ We can generate a basic source map using the sourceMap compilation option in tsconfig. json : { "compilerOptions": ......
Read more >
Emitting source maps - Learn TypeScript
Generating source map files · Open the index.ts file in the src folder. · Go to the run view in Visual Studio Code...
Read more >
How to set up source maps to help debug production JavaScript
These five tips help developers get source maps working in JavaScript so they can spend less time debugging and more time writing software....
Read more >
Is there source map support for typescript in node / nodemon?
run npm install source-map-support --save at the root of your node project and add import 'source-map-support/register' to your main.ts or index.ts file.
Read more >
TSConfig Reference - Docs on every TSConfig option
"tsc.ts". ] } This is useful when you only have a small number of files and ... Generates a source map for .d.ts...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found