Buy

Check out the source style.scss file. We're giving an element a fancy cursive font on lines 4 and 5. And that's what makes the Dinosaurs text look so awesome.

But if we inspect that element, it says the font is coming from styles.css line 2. Dang it! The browser is looking at the final, processed file. And that means that debugging CSS is going to be an absolute nightmare.

What I really want is the debugger to be smart enough to tell me that this font is coming from our styles.scss file at line 4. Such mysterious magic goodness exists, and it's called a sourcemap.

Using gulp-sourcemaps

Like with everything, this works via a plugin. Head back to the plugins page and search for gulp-sourcemaps. There's the one I want!

Step 1 is always the same - install with npm. So:

npm install gulp-sourcemaps --save-dev

Awesome! Next, copy the require statement and put that on top:

12 lines gulpfile.js
var gulp = require('gulp');
var sass = require('gulp-sass');
var sourcemaps = require('gulp-sourcemaps');
... lines 4 - 12

This plugin is great. First, activate it before piping through any filters that may change which line some code lives on. So, before the sass() line, use pipe() with sourcemaps.init() inside. Then after all those filters are done, pipe it again through sourcemaps.write('.'):

12 lines gulpfile.js
... lines 1 - 4
gulp.task('default', function() {
gulp.src('app/Resources/assets/sass/**/*.scss')
.pipe(sourcemaps.init())
.pipe(sass())
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('web/css'));
});

Ok, let's try it! At the terminal, run gulp... and hope for the best!

gulp

Cool - no errors. And now, the generated styles.css has a neighboring file: styles.css.map! That's what the . did - it told Gulp to put the map file right in the same directory as styles.css, and the browser knows to look there.

Time to refresh the page again. Inspect the element again. Now it says the font comes from styles.scss on line 4. This is a huge deal guys. We can do whatever weird processing we want and not worry about killing our debugging.

Sourcemaps Support

Of course, this all works because gulp-sourcemaps and gulp-sass work together like super friends. If you look at the sourcemaps docs, they have a link on their wiki to a list of all super-friend gulp plugins that play nice with it. We'll use a couple of these later.

Leave a comment!

  • 2015-11-30 weaverryan

    I've got this on my list to look into - definitely not just your issue :)

  • 2015-11-27 edincc

    Ok cool.

  • 2015-11-27 edincc

    Ryan,

    For me it's the other way around: everything works as excepted with *.css files but not with scss files. I suspect it works with less too, but I havent fully tested this. I say this because including bootstrap.css with scss files does show source bootstrap less files.

    For example, when I comment .pipe(plugins.sass()) from addStyle function and load css files sourcemaps work as expected.
    Strangely, as soon as I use scss files (with .pipe(plugins.sass()) and write style it switches to stdin. To clearify:

    In style.scss:
    .some-dino-class {}
    .some-other-thingie {}

    works.

    main.css.map gives: {"version":3,"sources":["bootstrap.css","font-awesome.css","layout.scss","style.scss"]....

    When I do (with or without variables):

    .some-dino-class {
    font-size: bigger;
    }
    .some-other-thingie { color: red }

    It breaks.
    main.css.map gives: {"version":3,"sources":["bootstrap.css","font-awesome.css","../../../stdin","../../../stdin"]....

    At first I thought it has something to do with my project (maybe I overlooked something), but I downloaded the course source files with same results.

  • 2015-11-27 weaverryan

    I opened an issue to track this problem: https://github.com/knpuniversi...

  • 2015-11-27 weaverryan

    Hey!

    I don't remember seeing this problem originally, but I *can* see it now: the sourcemaps incorrectly thinks that the source of *some* selectors os stdin. For me, it looks like it works correctly for any source files that are .scss: the problem is only for source files that are true css files (e.g. bootstrap.css). Do you see the same thing? What I mean is, I can "Inspect Element" on a style that comes from an .scss file and it shows me the proper source. If I do the same for a style that comes originally from a CSS file, that's when I see the stdin.

    Thanks!

  • 2015-11-27 edincc

    Whatever I do I can't make sourcemaps work as in the video.
    Sourcemaps always gives me 'stdin' as style file, so when I inspect an element I see stdin and not source scss file.