gulp 构建异常 Task function must be specified

这里写自定义目录标题

  • gulp 构建异常 Task function must be specified
    • 异常信息
    • 查看版本:
    • gulpfile.js
    • 解决
    • 参考资料

gulp 构建异常 Task function must be specified

在学习TypeScript的时候出现的异常信息。
具体原因是由于gulp3gulp4 的加载依赖方式改变导致的。

异常信息

> gulp
{ AssertionError [ERR_ASSERTION]: Task function must be specified
    at Gulp.set [as _setTask] (D:\workspace\font\typescript-demo\node_modules\undertaker\lib\set-task.js:10:3)
    at Gulp.task (D:\workspace\font\typescript-demo\node_modules\undertaker\lib\task.js:13:8)
    at Object.<anonymous> (D:\workspace\font\typescript-demo\gulpfile.js:16:6)
    at Module._compile (internal/modules/cjs/loader.js:722:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:733:10)
    at Module.load (internal/modules/cjs/loader.js:620:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
    at Function.Module._load (internal/modules/cjs/loader.js:552:3)
    at Module.require (internal/modules/cjs/loader.js:658:17)
    at require (internal/modules/cjs/helpers.js:22:18)
  generatedMessage: false,
  name: 'AssertionError [ERR_ASSERTION]',
  code: 'ERR_ASSERTION',
  actual: false,
  expected: true,
  operator: '==' }

查看版本:

> gulp -v
CLI version: 2.3.0
Local version: 4.0.2

gulpfile.js

var gulp = require("gulp");
var browerify = require("browserify");
var source = require("vinyl-source-stream");
var tsify = require("tsify");
var ts = require("gulp-typescript");
var tsProject = ts.createProject("tsconfig.json");
var paths = {
    pages: ["src/*.html"]
}

gulp.task("copy-html", () => {
    return gulp.src(paths.pages)
        .pipe(gulp.dest("dist"));
});

gulp.task("default", ["copy-html"], function () {
    return browerify({
            basedir: ".",
            debug: true,
            entries: ['src/main.ts'],
            cache: {},
            packageCache: {}
        })
        .plugin(tsify)
        .bundle()
        .pipe(source('bundle.js'))
        .pipe(gulp.dest("dist"));
});

解决

使用gulp.series 函数指定模块加载的顺序。

gulp.task('default', gulp.series('del', function() { 
    // default task code here
}));

具体修改gulpfiles.js 如下:

var gulp = require("gulp");
var browerify = require("browserify");
var source = require("vinyl-source-stream");
var tsify = require("tsify");
var ts = require("gulp-typescript");
var tsProject = ts.createProject("tsconfig.json");
var paths = {
    pages: ["src/*.html"]
}

gulp.task("copy-html", () => {
    return gulp.src(paths.pages)
        .pipe(gulp.dest("dist"));
});

gulp.task("default", gulp.series("copy-html", function () {
    return browerify({
            basedir: ".",
            debug: true,
            entries: ['src/main.ts'],
            cache: {},
            packageCache: {}
        })
        .plugin(tsify)
        .bundle()
        .pipe(source('bundle.js'))
        .pipe(gulp.dest("dist"));
}));

参考资料

  1. How do I update to Gulp 4?

你可能感兴趣的:(其他)