38

Getting to grips with Gulp and have a question.

So I have a gulp CSS task like the below which works just fine:

var sassDir = 'app/scss';
var targetCssDir = 'public/assets';

gulp.task('css', function(){
    return gulp.src(sassDir + '/main.scss')
        .pipe(sass({ style: 'compressed' }).on('error', gutil.log))
        .pipe(autoprefix('last 10 version'))
        .pipe(gulp.dest(targetCssDir));;
});

But is there a way to add my vendor files like Bootstrap so it will be included for minification and concatenation.

Hope you can advise.!

Lee
  • 20,034
  • 23
  • 75
  • 102

4 Answers4

82

gulp-sass will meet your request. Pls let me show you how to compile Sass files and minify (or compress) compiled css files:

  • install gulp-sass from here
  • in you project's gulpfile.js, add following code:

Note: outputStyle in gulp-sass has four options: nested, expanded, compact, compressed

It really works, I have used it in my project.

var gulp = require('gulp');
var sass = require('gulp-sass');

//sass
gulp.task('sass', function () {
    gulp.src(['yourCSSFolder/*.scss', 'yourCSSFolder/**/*.scss'])
        .pipe(sass({outputStyle: 'compressed'}))
        .pipe(gulp.dest('yourCSSFolder/'));
});

// Default task
gulp.task('default', function () {
    gulp.start('sass');
});

For reminder the readme

starball
  • 20,030
  • 7
  • 43
  • 238
aisin
  • 1,107
  • 7
  • 11
23

I can think of two solutions. The best option, I feel, is to use @import statements within your Sass file to include the vendor files. Use relative paths to where they live, and you can then include them in the order you want. Apparently this doesn't work using SASS unless you are using SASS imports.


Alternatively, you can use event-stream and gulp-concat to concatenate streams of files. In this case, you should not use gulp-sass to compress the files, rather, use something like gulp-csso to handle the compression.

var es = require('event-stream'),
    concat = require('gulp-concat');

gulp.task('css', function(){
    var vendorFiles = gulp.src('/glob/for/vendor/files');
    var appFiles = gulp.src(sassDir + '/main.scss')
        .pipe(sass({ style: 'compressed' }).on('error', gutil.log));

    return es.concat(vendorFiles, appFiles)
        .pipe(concat('output-file-name.css'))
        .pipe(autoprefix('last 10 version'))
        .pipe(gulp.dest(targetCssDir));
});

Again, you should use the first method if you can, but es.concat is useful for other scenarios.

OverZealous
  • 39,252
  • 15
  • 98
  • 100
  • I find it unlikely that the vendor files the OP wants to include for minification purposes are Sass files, otherwise they probably would have included it as you suggest. Sass cannot import CSS files in the same way that it does with Sass files (see: http://stackoverflow.com/questions/16947337/can-i-import-an-externally-hosted-file-with-sass). – cimmanon Feb 27 '14 at 16:08
  • Thanks OverZealous. Using @import just echo's out import in the css file so thats no good. the only thing I don't like having to src of files is the order in which they load. Of course I could prefix them ie 01_bootstrap.css. – Lee Feb 27 '14 at 17:47
  • That's too bad, Less had an alternative term (`include`) which can include any file. The second solution could still help. You can specify [multiple `src` files using an array](https://github.com/gulpjs/gulp/blob/master/docs/API.md#globs) to control the order. – OverZealous Feb 27 '14 at 18:19
  • I cant get this to work OverZealous. It just creates separate files in the destination directory. the concat is not doing anything:-( – Lee Feb 27 '14 at 20:41
  • I'm an idiot, and forgot to include `gulp-concat`, I've updated the solution. I apologize! – OverZealous Feb 27 '14 at 21:05
  • @cimmanon, @OverZealous - since CSS is valid SCSS, another quick and dirty solution to OP is to rename the CSS --> SCSS, and use `@import` like you originally suggested. – jlee Aug 08 '14 at 07:45
  • 2
    Gulp always place appFiles code before vendorFiles code in the target output-file (maybe because their stream runs faster, in my case?). How to solve that? I'm using `mainBowerFiles()` with `gulpFilter` as the vendorFiles source. – zok Sep 10 '14 at 14:52
2

i found this recently gulp-cssjoin this allows you to replace imports with inline css

the scss

@import '../../bower_components/angular/angular-csp.css';

the gulp task

var gulp = require('gulp'),
    gutil = require('gulp-util'),
    sass = require('gulp-ruby-sass'),
    cssjoin = require('gulp-cssjoin'),
    csscomb = require('gulp-csscomb');

gulp.task('sass',['images'], function() {
  return gulp.src('src/sass/*.{sass,scss}')
    .pipe(sass({
      compass: true,
      bundleExec: true,
      sourcemap: true,
      sourcemapPath: '../src/sass'
    }))
    .on('error',gutil.log.bind(gutil, 'Sass Error'))
    .pipe(cssjoin({
      paths: ['./src/sass']
    }))
    .pipe(csscomb())
    .pipe(gulp.dest('build'));
});

the important part is the passing in of the paths

.pipe(cssjoin({
  paths: ['./src/sass']
}))
1

You can convert scss files to min.css with just one simple task

const { series, src, dest } = require("gulp");
const sass = require("gulp-sass")(require("sass"));
const cleanCss = require("gulp-clean-css");
var rename = require("gulp-rename");

function minCss() {
  return src("scss/*.scss")
    .pipe(sass())
    .pipe(
      rename(function (file) {
        file.basename = file.basename + ".min";
      })
    )
    .pipe(cleanCss())
    .pipe(dest("css"));
}

exports.watch = series(minCss);
Vu Anh
  • 21
  • 5