gulp报错:AssertionError: Task function must be specified

image.png


文章目录



gulpfile.js文件



var gulp = require("gulp");
var concat = require("gulp-concat");
var uglify = require('gulp-uglify');
var cssmin = require('gulp-cssmin');
var wrap = require("gulp-wrap");
var expressService = require('gulp-express-service');

var wwwroot = './static/';
var paths = {
 scripts: ['static/js/*.js'],
 styles: ['static/css/*.css'],
 server: ['./app.js']
};

gulp.task("scripts", function () {
 gulp.src(paths.scripts)
   .pipe(concat('m.js'))
   .pipe(gulp.dest(wwwroot))
   .pipe(uglify())
   .pipe(concat('main-min.js'))
   .pipe(gulp.dest(wwwroot));
});

gulp.task('styles', function () {
 return gulp.src(paths.styles)
   .pipe(concat('m.css'))
   .pipe(gulp.dest(wwwroot))
   .pipe(cssmin())
   .pipe(concat('main.min.css'))
   .pipe(gulp.dest(wwwroot));
});

gulp.task('run_service', function () {
 return gulp.src(paths.server)
   .pipe(expressService({
     file: './app.js'
   }))
   .on('error', function (e) {
     console.log(e)
   });
});


gulp.task("watch", function () {
 gulp.watch(paths.scripts, ["scripts"]);
 gulp.watch(paths.styles, ['styles']);
 gulp.watch(paths.server, ['run_service']);
});

gulp.task("build", ["scripts", "styles"]);
gulp.task("default", ["build", "run_service", "watch"]);

一切运行正常!



升级package


  • gulp 升级到4.0.2
  • 执行gulp build
  • 报错:AssertionError: Task function must be specified


gulp3 VS gulp4 区别



gulp3中的顺序执行


如果有一个任务列表包含 fun1,fun2,fun3,你想在一个序列中运行(确保fun1在fun2之前执行,fun2在fun3之前执行)
代码如下:

gulp.task('fun1', function () {
  // Do something.
});
gulp.task('fun1', ['fun1'], function () {
  // Do some stuff.
});
gulp.task('fun3', ['fun2'], function () {
    // Do some more stuff.
});

而以上代码在 gulp4 环境下会得到上面的错误:AssertionError: Task function must be specified

assert.js:85
  throw new assert.AssertionError({
  ^
AssertionError: Task function must be specified
    at Gulp.set [as _setTask] (/home/hope/web/node_modules/undertaker/lib/set-task.js:10:3)
    at Gulp.task (/home/hope/web/node_modules/undertaker/lib/task.js:13:8)
    at Object.<anonymous> (/home/hope/web/gulpfile.js:31:6)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)


gulp4中的顺序执行


gulp4 中你不能像gulp3 一样指定任务依赖
如果保证一个任务要在另一个任务完成的情况下执行,需要使用gulp.series和gulp.parallel



series语法


任务串行

gulp.task('tasks', gulp.series('fun1', 'fun2', 'fun3', function() {
  // Do something 
}));


parallel语法


任务并行

gulp.task('task2', gulp.parallel('fun1', 'fun2', 'fun3', function () {
  // Build the website.
}));


解决方案


  1. 将gulp4降级到gulp3 (目前稳定版 3.9.1 )
  2. 使用gulp4的新语法 series 和 parallel 来控制任务依赖关系

出自:gulp报错:AssertionError: Task function must be specified

回到顶部