6

I'm having trouble with my gulpfile and can't figure out what's going wrong.

Issues: autoprefixer not working as intended, and it isn't always properly outputting a .css and .min.css file as intended.

var gulp = require('gulp');
var sass = require('gulp-sass');
var sourcemaps = require('gulp-sourcemaps');
var prefix = require('gulp-autoprefixer');
var minify = require('gulp-minify-css');
var rename = require('gulp-rename');

var config = {
    sassMain: 'style/Main.scss',
    sassFiles: 'style/**/*.scss',
    cssfiles: ['style/Main.css'],
    dest: 'style',
    autoprefixerOptions: {
    browsers: ['last 2 versions', '> 5%']
    }
};

gulp.task('compile-sass', function () {
    return gulp.src(config.sassMain)
        .pipe(sourcemaps.init())
        .pipe(sass().on('error', sass.logError))
        .pipe(prefix(config.autoprefixerOptions.browsers))
        .pipe(sourcemaps.write())
        .pipe(gulp.dest(config.dest))
        .pipe(rename({ extname: '.min.css' }))
        .pipe(minify())
        .pipe(sourcemaps.write())
        .pipe(gulp.dest(config.dest))
});

gulp.task('default', function () {
    gulp.watch(config.sassFiles, ['compile-sass']);
});

Any ideas?

anthumchris
  • 8,245
  • 2
  • 28
  • 53
hi.im.kvothe
  • 63
  • 1
  • 10

1 Answers1

17

First, I recommend putting all source files into folder src and built files into build. This works below, and you can incrementally add new pipes to see what breaks it

gulpfile.js

var gulp = require('gulp')
var sass = require('gulp-sass')
var autoprefixer = require('gulp-autoprefixer')
var minCss = require('gulp-minify-css')
var rename = require('gulp-rename')

var config = {
   srcCss : 'src/scss/**/*.scss',
   buildCss: 'build/css'
}

gulp.task('build-css', function(cb) {
   gulp.src(config.srcCss)

      // output non-minified CSS file
      .pipe(sass({
         outputStyle : 'expanded'
      }).on('error', sass.logError))
      .pipe(autoprefixer())
      .pipe(gulp.dest(config.buildCss))

      // output the minified version
      .pipe(minCss())
      .pipe(rename({ extname: '.min.css' }))
      .pipe(gulp.dest(config.buildCss))

   cb()
})

gulp.task('watch', function(cb) {
   gulp.watch(config.srcCss, ['build-css']) 
})

gulp.task('default', ['build-css', 'watch'])

main.scss

$degrees: '50%';

.rotated {
    transform: $degrees;
}

package.json

{
  "name": "",
  "version": "1.0.0",
  "private": true,
  "devDependencies": {
    "gulp": "^3.9.0",
    "gulp-autoprefixer": "^3.1.0",
    "gulp-minify-css": "^1.2.2",
    "gulp-rename": "^1.2.2",
    "gulp-sass": "^2.1.0"
  }
}

Resulting File Structure

Here's what you'll have after you run $ gulp (excludes node_modules folder)

$ tree -I node_modules
.
├── build
│   └── css
│       ├── main.css
│       └── main.min.css
├── gulpfile.js
├── package.json
└── src
    └── scss
        └── main.scss

4 directories, 5 files

My Environment

$ node -v
v5.1.0

$ npm -v
3.3.12

$ gulp -v
[13:39:54] CLI version 1.0.0
[13:39:54] Local version 3.9.0
anthumchris
  • 8,245
  • 2
  • 28
  • 53
  • I tried stripping it down to this: `.pipe(sass())` `.pipe(gulp.dest(config.destDir))` But it's still not working. Could there be something wrong with my npm/node packages? My original Gulpfile.js was working last week. node `5.1.0` and npm `3.3.12` – hi.im.kvothe Dec 01 '15 at 20:16
  • Try using my code exactly as I wrote and tested it. Then add more code on incrementally if needed. – anthumchris Dec 01 '15 at 20:38
  • I tried that. Getting the same thing. Definitely looks like something screwy happened with my node and npm packages – hi.im.kvothe Dec 01 '15 at 21:00
  • `rm -rf node_modules && npm install` – anthumchris Dec 01 '15 at 21:04
  • This is going to sound really stupid but I'm boggled so I'm just going to put myself out there and ask this: I'm getting this error when I try to remove node_modules: `Remove-Item : A parameter cannot be found that matches parameter name 'rf'.` Thanks for all the help by the way. – hi.im.kvothe Dec 01 '15 at 21:43
  • `$ ...` typically indicates a linux command. I'll assume you're on Windows: `c:\...\project> rmdir node_modules`. In otherwords, delete `node_modules` and run `npm install` again. Then run `gulp`. – anthumchris Dec 01 '15 at 21:49
  • Thanks for all the help. Was eventually able to resolve the issue by reinstalling Node, NPM, node-sass, and gulp, then rebuilding the whole thing. – hi.im.kvothe Dec 03 '15 at 22:04
  • 2
    This was a nice find on Google. One comment - `gulp-minify-css` is deprecated and has introduces at least one security vulnerability (https://npmjs.com/advisories/785) at the time of this comment. Use of `gulp-clean-css` is now recommended. – Luke A. Leber Feb 09 '20 at 01:29
  • This is awesome and I like how it is one task to generate the expanded and compressed styles, but can you also generate sourcemaps for the minified version of the stylesheet? @AnthumChris – Robbiegod May 27 '20 at 20:28