简体   繁体   English

从 Gulp 3 升级到 Gulp 4

[英]Upgrading to Gulp 4 from Gulp 3

I've attempted to update the gulpfile below to Gulp v4 from v3 but am still getting an error: AssertionError [ERR_ASSERTION]: Task never defined: client我试图将下面的 gulpfile 从 v3 更新到 Gulp v4,但仍然收到错误: AssertionError [ERR_ASSERTION]: Task never defined: client

Not sure what I am missing but realise that the functions may not be written correctly.不确定我缺少什么,但意识到函数可能没有正确编写。 The tasks in series are correct I believe: gulp.task('build', gulp.series('client', 'sass'));我相信系列任务是正确的: gulp.task('build', gulp.series('client', 'sass')); for example.例如。

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

When you use the gulp.task form of defining tasks (rather that as functions) then you cannot refer to those tasknames until after they have been declared.当您使用gulp.task形式定义任务(而不是作为函数)时,您不能在声明这些任务名称之前引用它们。 That would be a forward reference (see below).这将是一个forward reference (见下文)。 So just put your所以只要把你的

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

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

after all those tasks have been defined.在定义了所有这些任务之后。 I recommend right before the default task line.我建议在default任务行之前。

See https://gulpjs.com/docs/en/api/series#forward-references请参阅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.在这种情况下,请使用命名函数而不是字符串引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM