gulp4小demo

本来想着用gulp搭建一个自动化构建工具,结果一顿报错,后来发现我重新安装的gulp版本是4.0.0,与3版本还是不太一样的,遂记之。

Gulp 3版本Demo:

const gulp = require('gulp');

// 设定一个任务
gulp.task('hello',() => {
    console.log('print hello');
});

// 再设定一个任务
gulp.task('world',() => {
    console.log('print world');
});

// 设置一个默认的任务
gulp.task('default',['hello','world']);

  但用上述语句在Gulp4中会报错:

assert.js:350
    throw err;
    ^

AssertionError [ERR_ASSERTION]: Task function must be specified
    at Gulp.set [as _setTask] (C:\Users\zxq\Desktop\gulpCourse\node_modules\[email protected]@undertaker\lib\set-task.js:10:3)
    at Gulp.task (C:\Users\zxq\Desktop\gulpCourse\node_modules\[email protected]@undertaker\lib\task.js:13:8)
    at Object.<anonymous> (C:\Users\zxq\Desktop\gulpCourse\gulpfile.js:26:6)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)
    at Module.require (internal/modules/cjs/loader.js:636:17)
    at require (internal/modules/cjs/helpers.js:20:18)

  Gulp 4中的正确写法(结合async 和 await):

const gulp = require('gulp');

gulp.task('hello', async() => {
    await console.log('print hello');
});

gulp.task('world', async() => {
    await console.log('print world');
});

gulp.task('default',gulp.series(gulp.parallel('hello','world')));

  

猜你喜欢

转载自www.cnblogs.com/carriezhao/p/10558629.html
今日推荐