2

hi I'm having trouble setting up gulp it seems to have all changed since I last used it I'm getting errors and can't figure out why. I'll post some pics along with my code. the first problem is that uglify doesn't complete and the second problem is that gulp default won't run the command prompt should explain my problems better than I can if you have any further questions please ask and be civil.

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




sass.compiler = require('node-sass');

gulp.task('sass', function () {
 return gulp.src('./Edit/sass/*.scss')
   .pipe(sass().on('error', sass.logError))
   .pipe(gulp.dest('./Edit/css'));
});


gulp.task('css', function () {
  gulp.src('./Edit/css/*.css')
    .pipe(uglifycss({
      "maxLineLen": 80,
      "uglyComments": true
    }))
    .pipe(gulp.dest('./upload/css'));
});



gulp.task('run',['sass','css']);

gulp.task('watch', function(){
    gulp.watch('./Edit/sass/*.scss',['sass']);
    gulp.watch('./Edit/css/*.css',['css']);
});

gulp.task('default',['watch', 'run']);

here is my output

uglify errors

default errors

1 Answer 1

3

So you've got two kinds of errors going on:

1. Task function must be specified

The way gulp runs dependent tasks has changed in v4.0.0. Instead of specifying those tasks in an array, like this:

gulp.task('run',['sass','css']);

They've introduced the gulp.series and gulp.parallel functions. A task function, and not an array, because Task function must be specified. In your case, that gives:

gulp.task('run', gulp.series('sass','css'));

2. Did you forget to signal async completion

This one you could have found, given that this question has been asked many times now. You need to add a return statement to your css task for gulp to know when it's completed and can thus move on. Your task becomes:

gulp.task('css', function () {
  return gulp.src('./Edit/css/*.css')
    .pipe(uglifycss({
      "maxLineLen": 80,
      "uglyComments": true
    }))
    .pipe(gulp.dest('./upload/css'));
});

Result:

Putting it all together, you get this gulpfile:

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

sass.compiler = require('node-sass');

gulp.task('sass', function () {
 return gulp.src('./Edit/sass/*.scss')
   .pipe(sass().on('error', sass.logError))
   .pipe(gulp.dest('./Edit/css'));
});


gulp.task('css', function () {
  return gulp.src('./Edit/css/*.css')
    .pipe(uglifycss({
      "maxLineLen": 80,
      "uglyComments": true
    }))
    .pipe(gulp.dest('./upload/css'));
});



gulp.task('run', gulp.series('sass','css'));

gulp.task('watch', function(){
    gulp.watch('./Edit/sass/*.scss',gulp.series('sass'));
    gulp.watch('./Edit/css/*.css', gulp.series('css'));
});

gulp.task('default', gulp.series('watch', 'run'));

Note that you can combine your sass and css task if you'd like:

gulp.task('styles', function(){
  return gulp.src('./Edit/sass/*.scss')
   .pipe(sass().on('error', sass.logError))
   .pipe(uglifycss({
    "maxLineLen": 80,
    "uglyComments": true
  }))
  .pipe(gulp.dest('./upload/css'));
});
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the help I did eventually find the solution after some searching, I don't know why gulps site doesn't make any mention of the new syntax as it is there to speed up the process you'd think they would at least mention it. I don't have time at the moment to test your solution but I'm sure it works just fine. The fix I found said you could no longer use gulp.task instead you hade to create a named function and then turn that function in to a task separately. Hopefully your code works as it is much more elegant than creating named functions and them making them tasks. Thanks again.
Just quickly tested your amendments works perfectly appreciate the help. Quick question you mentioned combining the sass and css tasks appart from saving on code would there be ny other benefits as If I understand the new code you gave me the series command runs them both together and since the run tak is how I'm running them there should be no difference?
realise now I mixed up series and parallel plus I wouldn't want to run them in parallel as they depend on each other completing in order.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.