从 Gulp 3 升级到 Gulp 4

Upgrading to Gulp 4 from Gulp 3

我已尝试将下面的 gulpfile 从 v3 更新为 Gulp v4,但仍然出现错误:AssertionError [ERR_ASSERTION]: Task never defined: client

不确定我遗漏了什么,但意识到函数可能没有正确编写。我认为系列任务是正确的:例如gulp.task('build', gulp.series('client', 'sass'));

// gulpfile.js

var gulp = require('gulp'),
    concat = require('gulp-concat'),
    browserify = require('gulp-browserify'),
    rename = require('gulp-rename'),
    nodemon = require('gulp-nodemon'),
    sass = require('gulp-sass');


gulp.task('build', gulp.series('client', 'sass'));

gulp.task('watch', gulp.series('client-watch', 'sass-watch'));

gulp.task('server', function () {
  nodemon({
    script: 'server/index',
    ext: 'js json'
  });
});

gulp.task('client', function () {
  gulp.src('client/js/main.js')
      .pipe(browserify({
        transform: ['hbsfy'],
        extensions: ['.hbs']
      }))
      .pipe(rename('hearthclone.js'))
      .pipe(gulp.dest('./client/build'));
});

gulp.task('client-watch', function () {
  gulp.watch('client/js/**/*.js', ['client']);
});

gulp.task('sass', function () {
  gulp.src('client/**/*.scss')
    .pipe(sass())
    .pipe(concat('style.css'))
    .pipe(gulp.dest('client/assets/css'));
});

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


gulp.task('default', gulp.series('build', 'server', 'watch'));

当您使用 gulp.task 形式定义任务(而不是作为函数)时,您不能在声明这些任务名称之前引用它们。那将是 forward reference (见下文)。所以只要把你的

gulp.task('build', gulp.series('client', 'sass'));

gulp.task('watch', gulp.series('client-watch', 'sass-watch'));

在定义完所有这些任务之后。我建议在 default 任务行之前。

https://gulpjs.com/docs/en/api/series#forward-references

Forward references

A forward reference is when you compose tasks, using string references, that haven't been registered yet. This was a common practice in older versions, but this feature was removed to achieve faster task runtime and promote the use of named functions.

In newer versions, you'll get an error, with the message "Task never defined", if you try to use forward references. You may experience this when trying to use exports for your task registration and composing tasks by string. In this situation, use named functions instead of string references.