0

我究竟做错了什么?

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

我仍然对迈尔斯回答(想法)依附于我过去的问题

4

2 回答 2

1

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

此外,我认为您需要使用merge-stream byconst merge = require('merge-stream')return merge(directories.map(singleDirectoryGulpTask));

结果应该是这样的:(免责声明:多年来没有做过 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));
});

于 2020-06-05T09:22:14.990 回答
0

改变:

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

至:

for (directory of directories) {
  gulpTask(directory);
}
于 2020-06-05T09:35:19.740 回答