Clean-Css task gives error in gulpfile : cannot read property 'on'

761 views Asked by At

I am totally new to gulp and I wanted to add a task to minify and eventually clean any duplicates or unused css. My gulpfile is below, and for the moment I'm still in the process of learning.

I know I will be using post-css and the modules that can go with it, later on. Right now I get an error: "Cannot read property 'on' of undefined at DestroyableTransform.Readable.pipe". It comes from the cleancss task, when I take it out, there's no errors. Any help and suggestions would be appreciated.

//JS
var gulp = require('gulp');
var sass = require('gulp-sass');
var browserSync = require('browser-sync');
var cleancss = require('clean-css');
var autoPrefixer = require('gulp-autoprefixer');

gulp.task('autoPrefixer', function() {
    return gulp.src('../css/*.css')
        .pipe(autoPrefixer ({
            browsers: ['last 2 versions'],
            }))
            .pipe(gulp.dest('../css'))
    });

gulp.task('cleancss', function() {
  return gulp.src('../css/*.css')
    .pipe(cleancss({compatibility: 'ie8'}))
    .pipe(gulp.dest('../css/min'));
});


gulp.task('sass', function(){
  return gulp.src('../scss/*.scss')
    .pipe(sass().on('error', function(err) {
      console.error('\x07'); // so it doesn't just fail (literally) silently!
      sass.logError.bind(this)(err);
    })) // Converts Sass to CSS with gulp-sass
    .pipe(gulp.dest('../css'))
    .pipe(autoPrefixer())
    .pipe(browserSync.reload({
        stream: true
    }))
});


gulp.task('watch', ['browserSync','sass','autoPrefixer','cleancss'], function() {
    gulp.watch('../scss/*.scss', ['sass']);
    gulp.watch('../*.php', browserSync.reload);
    gulp.watch('../js/*.js', browserSync.reload);
});

gulp.task('browserSync', function() {
    browserSync.init ({
        open: 'external',
        host: 'testsite.local',
        proxy: 'testsite.local',
        port: 3000
    })
        browserSync ({
        server: {
            baseDir: 'app'
        },
    })

});
2

There are 2 answers

1
Mika Sundland On

You should use the gulp plugin for cleancss, rather than cleancss directly.

var cleancss = require('gulp-clean-css');

instead of

var cleancss = require('clean-css');

Remember to install it if you haven't done that already:

npm install gulp-clean-css --save-dev
0
mlclm On

I actually managed to make it work by using "vynil-map" as discussed on here : https://github.com/jakubpawlowicz/clean-css/issues/342. Hope this link could help anybody else having the same problem.