gulp 任务 for(... in ...) 是否正确创建?

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

我做错了什么?

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);
}

运行任务 npx gulp gulpTask

意外

我收到以下错误消息:

Task never defined: gulpTask

我仍然依恋我的past question with the answer (idea) from Miles

首先,gulp 任务应该使用您想要调用它的名称注册 gulp.task('blogmotionAllMedia', ... 应该是 gulp.task('gulpTask', ...。 此外,我认为您应该遍历任务内的目录。

此外,我认为您需要通过 const merge = require('merge-stream')return merge(directories.map(singleDirectoryGulpTask));

使用 merge-stream 合并结果流

结果应如下所示: (免责声明:gulp 多年没有做过这些事情了。仍然希望它有所帮助)

'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));
});

变化:

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

至:

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