Gulp v4的gulp.series()声明错误

时间:2018-08-14 21:35:39

标签: gulp

我做错了吗,或者这是gulp v4问题?运行“ build-minify”任务时,出现以下错误:

assert.js:42
  throw new errors.AssertionError({
  ^

AssertionError [ERR_ASSERTION]: Task function must be specified
    at Gulp.set [as _setTask] (/mnt/d/BriteTank 1d/node_modules/undertaker/lib/set-task.js:10:3)
    at Gulp.task (/mnt/d/BriteTank 1d/node_modules/undertaker/lib/task.js:13:8)
    at Object.<anonymous> (/mnt/d/BriteTank 1d/gulpfile.js:60:6)
    at Module._compile (module.js:660:30)
    at Object.Module._extensions..js (module.js:671:10)
    at Module.load (module.js:573:32)
    at tryModuleLoad (module.js:513:12)
    at Function.Module._load (module.js:505:3)
    at Module.require (module.js:604:17)
    at require (internal/module.js:11:18)

据我所知,我已经按照gulp v4的期望(Gulp 4 New Task Execution System)编写了所有内容。这段代码可与gulp v3.9.1一起使用,其“ build-minify”任务是这样编写的:

gulp.task("build-minify", ["sass-by-version", "minify-css-by-version"]);

这是新代码,会引发错误:

var gulp = require('gulp');
let cleanCSS = require("gulp-clean-css");
var rename = require("gulp-rename");
var sass = require("gulp-sass");

gulp.task('sass-by-version', function () {
 return gulp.src('./sass/**/*.scss')
   .pipe(sass({outputStyle: 'expanded'}).on('error', sass.logError))
   .pipe(gulp.dest(stylesheetsDest));
});

gulp.task('minify-css-by-version', () => {
    return gulp.src(stylesheetsSrc + ".css")
        .pipe(cleanCSS({ compatibility: 'ie8' }))
        .pipe(rename({
            suffix: '.min'
        }))
        .pipe(gulp.dest(file => file.base));
});

gulp.task("build-minify", gulp.series("sass-by-version", "minify-css-by-version"));

2 个答案:

答案 0 :(得分:0)

我不确定为什么会出现错误。我在您的环境("gulp": "^4.0.0""gulp-clean-css": "^3.10.0")中使用了您的脚本,并且在添加了stylesheetsSrcstylesheetsDest之后,该脚本便开始工作。您的gulpfile.js是否包含这两个变量?

请注意,我将return gulp.src('./sass/**/*.scss')更改为反映我的return gulp.src('./resources/assets/**/*.scss')的路径,但这不会对您的构建产生影响。

请参阅下面的代码,其中包括stylesheetsSrcstylesheetsDest

var gulp = require('gulp');
let cleanCSS = require("gulp-clean-css");
var rename = require("gulp-rename");
var sass = require("gulp-sass");
var stylesheetsDest = './tests/';
var stylesheetsSrc = './public/css/app';

gulp.task('sass-by-version', function () {
 return gulp.src('./resources/assets/**/*.scss')
   .pipe(sass({outputStyle: 'expanded'}).on('error', sass.logError))
   .pipe(gulp.dest(stylesheetsDest));
});

gulp.task('minify-css-by-version', () => {
    return gulp.src(stylesheetsSrc + ".css")
        .pipe(cleanCSS({ compatibility: 'ie8' }))
        .pipe(rename({
            suffix: '.min'
        }))
        .pipe(gulp.dest(file => file.base));
});

gulp.task(
    "build-minify",
    gulp.series("sass-by-version", "minify-css-by-version")
);

这是控制台输出:

$ npm run dev

> @ dev /Volumes/500GB/master
> gulp build-minify

[00:05:12] Using gulpfile /Volumes/500GB/master/gulpfile.js
[00:05:12] Starting 'build-minify'...
[00:05:12] Starting 'sass-by-version'...
[00:05:13] Finished 'sass-by-version' after 677 ms
[00:05:13] Starting 'minify-css-by-version'...
[00:05:13] Finished 'minify-css-by-version' after 879 ms
[00:05:13] Finished 'build-minify' after 1.56 s

答案 1 :(得分:0)

您的代码看起来不错,只需删除node_modules/package-lock.json并执行npm install,然后再次运行构建即可。

如果您想了解任务从Gulp 3到Gulp 4的迁移:

旧代码:

gulp.task("build-minify", ["sass-by-version", "minify-css-by-version"]);

新代码:

  • 如果要一个接一个地运行任务:

    gulp.task("build-minify", gulp.series("sass-by-version", "minify-css-by-version"]));

  • 如果要并行运行任务:

    gulp.task("build-minify", gulp.parallel("sass-by-version", "minify-css-by-version"]));