7

See below. The problem I'm having with my current code is that Sourcemaps aren't getting injected via Browsersync. Am I missing something here or doing it the wrong way?

For reference: https://www.browsersync.io/docs/gulp/#gulp-sass-maps

// requirements
var gulp         = require('gulp');
var sass         = require('gulp-ruby-sass');
var browserSync  = require('browser-sync').create();
var prefix       = require('gulp-autoprefixer');
var rename       = require('gulp-rename');
var sourcemaps   = require('gulp-sourcemaps');
var cssclean     = require('gulp-clean-css');

// create sass tasks
gulp.task('sass', function() {
  return sass('assets/scss/style.scss', {sourcemap: true, style: 'compact'})
    .on('error', function (err) {
        console.error('Error!', err.message);
    })
    .pipe(rename({suffix: '.min'}))
    .pipe(cssclean())
    .pipe(prefix("last 2 versions", "> 1%", "ie 8", "Android 2", "Firefox ESR"))
    .pipe(sourcemaps.write('.'))
    .pipe(gulp.dest('assets/styles'))
    .pipe(browserSync.stream({match: '**/*.css'}))
});

gulp.task('serve', ['sass'], function() {

  browserSync.init({
    proxy: "localhost/portfolio2014",
    open:false
  });

  gulp.watch("assets/scss/**/*.scss", ['sass']);
  gulp.watch(["assets/styles/*.css", "site/**/*", "content/**/*", "assets/javascript/*.js"]).on('change', browserSync.reload);
});

// default task (just run gulp)
gulp.task('default', ['serve'] );
0

1 Answer 1

4
+25

Do you have to use gulp-ruby-sass? The gulpfile.js below is adding sourcemaps with gulp-sass and updating the browser when saving .scss files with sourcemaps.

        var gulp = require('gulp');
        var sass = require('gulp-sass');
        var browserSync = require('browser-sync').create();
        var prefix = require('gulp-autoprefixer');
        var rename = require('gulp-rename');
        var sourcemaps = require('gulp-sourcemaps');
        var cssclean = require('gulp-clean-css');

        // create sass tasks
        gulp.task('sass', function () {
            return gulp.src('assets/scss/test.scss')
                .pipe(sourcemaps.init())
                .pipe(sass())
                .pipe(rename({ suffix: '.min' }))
                .pipe(cssclean())
                .pipe(prefix("last 2 versions", "> 1%", "ie 8", "Android 2", "Firefox ESR"))
                .pipe(sourcemaps.write(''))
                .pipe(gulp.dest('assets/styles'))
                .pipe(browserSync.stream({ match: '**/*.css' }))
                .on('error', function (err) {
                    console.error('Error!', err.message);
                });
        });

        gulp.task('serve', ['sass'], function () {

            browserSync.init({
                proxy: "localhost/portfolio2014",
                open: false
            });

            gulp.watch("assets/scss/**/*.scss", ['sass']);
            gulp.watch(["assets/styles/*.css", "assets/javascript/*.js"]).on('change', browserSync.reload);
        });

        // default task (just run gulp)
        gulp.task('default', ['serve']);
2
  • Thanks for the help. Oddly enough my own answer seems to work now but not this one. I will have to look into it a bit more and see what I can come up with. You definitely helped though, thanks!
    – gburning
    Mar 25, 2016 at 23:38
  • Glad I could help. The code above is kind of a hybrid of yours and what I used to test it - I had to start a server for browserSync and changed some of the files names, which might be why it didn't work right away. Mar 29, 2016 at 16:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.