Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Francis N
Francis N
10,376 Points

How to use gulp-sourcemaps with gulp-useref & gulp-if?

Hello,

I am quite new to gulp but I am getting good practice in after watching the gulp basics and 'user-ref' courses. I also found https://css-tricks.com/gulp-for-beginners/ to be very helpful. But I am currently running into an issue where I can't get my minified css and js to include a source map. I looked into userref and it looks like there may be a solution using transform stream (link: https://www.npmjs.com/package/gulp-useref#transform-streams) but I do not know how to implement or know if this work.

Here is my code. Any advice or help would be appreciated. If more code is needed, I can paste the whole gulpfile.js

gulp.task('useref', function() {
      return gulp.src('app/*.html')
        .pipe(useref())
        .pipe(gulpIf('*.js', uglify()))
        .pipe(gulpIf('*.css', cssnano()))
        .pipe(gulp.dest('dist'))
    });

here is what the example of transform-streams look like

var gulp = require('gulp'),
    sourcemaps = require('gulp-sourcemaps'),
    useref = require('gulp-useref'),
    lazypipe = require('lazypipe');

gulp.task('default', function () {
    return gulp.src('index.html')
        .pipe(useref({}, lazypipe().pipe(sourcemaps.init, { loadMaps: true })))
        .pipe(sourcemaps.write('maps'))
        .pipe(gulp.dest('dist'));
});