javascriptgulpgulp-load-plugins

gulp task for(... in ...) create correctly?


What am I doing wrong?

gulpfile.js

'use strict';

// Modules & Plugins
var gulp = require('gulp');
const image = require('gulp-image');
var $ = require('gulp-load-plugins')();

let directories = [
  '2006/Art1',
  '2006/Art2',
  '2010/Art1'
];

function gulpTask(directoryName) {
  gulp.task('blogmotionAllMedia', function () {
    return gulp
      .src('src/content/' + directoryName + '/media/*.{jpg,jpeg,png,gif}')
      .pipe($.responsive({
        '*.jpg': [
          { width: 400, rename: { suffix: '-4' } },
          { rename: { suffix: '-6' } }
        ],
      }))
      .pipe(image({
        jpegRecompress: true
      }))
      .pipe(gulp.dest('dist/content/' + directoryName + '/media'))
  });
}

var directory;

for (directory in directories) {
  gulpTask(directory);
}

run task npx gulp gulpTask

unexpected

I get the following error message:

Task never defined: gulpTask

I am still attached to my past question with the answer (idea) from Miles


Solution

  • First, the gulp task should be registered with the name you want to invoke it gulp.task('blogmotionAllMedia', ... should be gulp.task('gulpTask', .... Furthermore I think you should iterate over the directories inside of the task.

    Additionally I think you need to merge the resulting streams using merge-stream by const merge = require('merge-stream') and return merge(directories.map(singleDirectoryGulpTask));

    Result should look like this: (Disclaimer: haven't done gulp stuff for years. Still hope it helps)

    'use strict';
    
    // Modules & Plugins
    var gulp = require('gulp');
    const image = require('gulp-image');
    var $ = require('gulp-load-plugins')();
    const merge = require('merge-stream')
    
    let directories = [
        '2006/Art1',
        '2006/Art2',
        '2010/Art1'
    ];
    
    gulp.task('gulpTask', function () {
        function singleDirectoryGulpTask(directoryName) {
            return gulp
                .src('src/content/' + directoryName + '/media/*.{jpg,jpeg,png,gif}')
                .pipe($.responsive({
                    '*.jpg': [
                        { width: 400, rename: { suffix: '-4' } },
                        { rename: { suffix: '-6' } }
                    ],
                }))
                .pipe(image({
                    jpegRecompress: true
                }))
                .pipe(gulp.dest('dist/content/' + directoryName + '/media'));
        }
    
        return merge(directories.map(singleDirectoryGulpTask));
    });