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.

combining sourcemaps over two gulp tasks yields hard to read maps

See original GitHub issue

gulp-sourcemaps version: 1.6.0 & 2.1.1

I have the following Gulp tasks:

gulp.task('compile-client-scripts', () => {
    return gulp.src(["!src/client/scripts/client.js", "src/client/scripts/**/*.js"])
        .pipe(sourcemaps.init())
        .pipe(babel({moduleIds: true}))
        .pipe(sourcemaps.write("."))
        .pipe(print())
        .pipe(gulp.dest("src/client/lib"));
});

gulp.task('concat-compiled-client-scripts', ['compile-client-scripts'], () => {
    return gulp.src("src/client/lib/**/*.js")
        .pipe(sourcemaps.init({loadMaps: true}))
        .pipe(concat("client.js"))
        .pipe(sourcemaps.write("."))
        .pipe(print())
        .pipe(gulp.dest("src/client/scripts"));
});

The final sourcemap map to the right places in code, but it generates a hard-to-read structure in the browser’s (both FF and Chrome) devtools.

image

Do I have to specify another option to fix this or is what I want just not possible?

Edit: What I want to achieve as result:

The following Gulp task (Basically both above combined) generates the result below:

gulp.task('debug-client-scripts', () => {
    return gulp.src(["!src/client/scripts/client.js", "src/client/scripts/**/*.js"])
        .pipe(sourcemaps.init())
        .pipe(babel({moduleIds: true}))
        .pipe(concat("client.js"))
        .pipe(sourcemaps.write("."))
        .pipe(print())
        .pipe(gulp.dest("src/client/scripts"));
});

image

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:13

github_iconTop GitHub Comments

2reactions
nmccreadycommented, Dec 1, 2016

@plamens @Nimelrian I think this has to deal with more of how your using gulp. gulp-sourcemaps and gulp are doing exact what you are telling them to do in you example.

The main issue is that when you run the task compile-source-scripts it keeps the folder integrity in the destination. Then you run gulp again on the source destination thus doubling the folder hierarchy. The simple solution is to flatten the files in the compile-source-scripts task.

example.tar

Code:

var gulp = require('gulp');
var flatten = require('gulp-flatten');
var typescript = require('gulp-typescript');
var concat = require('gulp-concat');
var sourcemaps = require('gulp-sourcemaps');
var destFileOptions = {mode: "755"};

function getTsCompilerOptions() {
   return {
      allowJs: true,
      noEmitOnError: true,
      target: "ES5"
   };
}

gulp.task('compileTypescript', function() {
   return gulp.src('views/**/*.ts')
         .pipe(sourcemaps.init())
         .pipe(typescript(getTsCompilerOptions()))
         .pipe(sourcemaps.write('.'))
         .pipe(flatten())// <------------ KEY to your problem
         .pipe(gulp.dest('transpiled-typescript', destFileOptions));
});

gulp.task('concatenateJavascript', ['compileTypescript'], function() {
   return gulp.src('transpiled-typescript/**/*.js')
         .pipe(sourcemaps.init({loadMaps: true}))
         .pipe(concat('index.js'))
         .pipe(sourcemaps.write('.'))
         .pipe(gulp.dest('dest', destFileOptions));
});

gulp.task('default', ['concatenateJavascript']);

2reactions
plamenscommented, Oct 20, 2016

I also observed that with gulp-sourcemaps 2.1.1. The problem is that paths in the properties of the generated sourceMap are relative to the base of the project, just like gulp-sourcemaps docs say.

For example: if the file ./subdir/main.ts is transpiled, the output source map will have the following property: “sources”:[“subdir/main.ts”]

When the next build task inits a sourcemap with loadMaps: true and loads this file, it will think that the sources property is relative to the sourceMap, so it will just append it to the path to the sourceMap. This happens at https://github.com/floridoo/gulp-sourcemaps/blob/master/src/init.js#L86 Thus, a part of the path will be duplicated and the sourceMap from the output of the second build task will have: “sources”:[“subdir/subdir/main.ts”]

Here is an example project with 2 simple build tasks, one transpiles a typescript file and the other does a gulp-concat with just this file: example-project.zip

Read more comments on GitHub >

github_iconTop Results From Across the Web

Gulp + source maps (multiple output files) - Stack Overflow
This task will take multiple files, do stuff to them, and output them along with source maps. It will include the source code...
Read more >
CoffeeScript
-m, --map, Generate source maps alongside the compiled JavaScript files. ... There are many great task runners for setting up JavaScript build chains, ......
Read more >
A complete toolchain for AngularJs - Gulp, Browserify, Sass
In this blog post we will propose a set of tools that a new Angular project will likely need, and present a simple...
Read more >
gulp-jumpscript - npm Package Health Analysis | Snyk
Learn more about gulp-jumpscript: package health score, popularity, ... Instead, create multiple projects or use a single task to compile your sources.
Read more >
Source Maps - SurviveJS
Source map types supported by webpack can be split into two categories: ... Thanks to their speed, inline source maps are ideal for...
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