Morvael
Morvael

Reputation: 3567

Node.js Gulp no outputfile created

I have a Gulp script to concatenate, and minimize javascript. It seems to be working but doesn't output the combined file.

The script is (complete - including extra debug bits):

// include plug-ins
var fs = require('fs');
var gulp = require('gulp');
var count = require('gulp-count');
var debug = require('gulp-debug');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var del = require('del');


var config = {
    src: 'dist/libraries/',
    dest: 'dist/js/',
    outputfile: 'libraries.min.js'
}

gulp.task('read', (done) =>  {
    fs.readdir(config.src, (err, items) => {
        console.log(items);
    });
    done();
});

//delete the output file(s)
gulp.task('clean', gulp.series('read'), (done) => {
    //del is an async function and not a gulp plugin (just standard nodejs)
    //It returns a promise, so make sure you return that from this task function
    //  so gulp knows when the delete is complete
    return del([config.dest + config.outputfile]);
});

// Combine and minify all files from the app folder
// This tasks depends on the clean task which means gulp will ensure that the 
// Clean task is completed before running the scripts task.
gulp.task('scripts', gulp.series('clean'), (done) => {

    //Include all js files but exclude any min.js files
    var files = [config.src + '*.js', '!' + config.src + '*.min.js'];

    return gulp.src(files)
       .pipe(debug())
       .pipe(count('## files selected'))
       .pipe(uglify())
       .pipe(concat(config.outputfile))
       .pipe(gulp.dest(config.dest));
});

//Set a default tasks
gulp.task('default', gulp.series('scripts'), (done) => { 

});

Which produces the output - including file list for verification there are src files:

[07:46:25] Using gulpfile <path>\gulpfile.js
[07:46:25] Starting 'default'...
[07:46:25] Starting 'scripts'...
[07:46:25] Starting 'clean'...
[07:46:25] Starting 'read'...
[07:46:25] Finished 'read' after 996 μs
[07:46:25] Finished 'clean' after 2.73 ms
[07:46:25] Finished 'scripts' after 4.26 ms
[07:46:25] Finished 'default' after 6.9 ms
[ 'bootstrap-datetimepicker.js',
  'bootstrap.min.js',
  'chart.min.js',
  'cycle.js',
  'farbtastic.js',
  'jquery-3.2.1.min.js',
  'jquery-sortable-min.js',
  'moment.min.js',
  'ol.min.js',
  'pablo.min.js',
  'popper.min.js',
  'proj4.js',
  'promisedIndexDB.js',
  'qunit-2.6.1.js',
  'toastr.js' ]

If I create an empty file, at dist/js/libraries.min.js it isn't deleted as part of the gulp tasks, however if i move the call to del() outside the gulp tasks it is deleted, so that leads me to assume that its not as simple as a permissions issue, or path issues.

Any idea what I've done wrong?

PS: its on a windows box, running in an admin cmd window.

Upvotes: 0

Views: 127

Answers (2)

Mark
Mark

Reputation: 180755

You were using the wrong signature for the task. The correct one is :

task([taskName], taskFunction)

see task signature

But your tasks look like this:

gulp.task('scripts', gulp.series('clean'), (done) => {   // 3 parameters

Merely changing that to:

gulp.task('scripts', gulp.series('clean', (done) => {
  ...
}));

makes it work - I tested it. So now that task has only two parameters: a task name and a function. Yours had a task name plus two functions.

You would also need to change your default and clean tasks to this proper signature. Also you should call done() at the end of the task as you did with your cb().

Your new code uses task functions, which are better than named tasks for a number of reasons - but now you know what was wrong with your original code. The main body of your scripts task was never being run.

Upvotes: 1

Morvael
Morvael

Reputation: 3567

I never worked out what was wrong, but went direct to the doc's and started again (previous version was from a example)..

Works with the below (much simpler) script.

// // include plug-ins
var gulp = require('gulp');
var count = require('gulp-count');
var debug = require('gulp-debug');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var del = require('del');

var config = {
    src: 'jspa-scada/dist/libraries/',
    dest: 'jspa-scada/dist/js/',
    outputfile: 'libraries.min.js'
}

function defaultTask(cb) {

    del([config.dest + config.outputfile]);


    // Include all js files but exclude any min.js files
    var globs = [
        config.src + '*.js',
        '!' + config.src + '*.min.js'
    ];

    return gulp.src(globs)
       .pipe(debug())
       .pipe(count('## files selected'))
       .pipe(uglify())
       .pipe(concat(config.outputfile))
       .pipe(gulp.dest(config.dest));

    cb();
}

exports.default = defaultTask

Upvotes: 0

Related Questions