Gulp脚本抛出错误:抛出新错误。

时间:2018-09-07 18:31:17

标签: gulp

使用Gulp的断言错误:

$ gulp scripts
  assert.js:42
   throw new errors.AssertionError({
   ^
AssertionError [ERR_ASSERTION]: Task function must be specified
    at Gulp.set [as _setTask] (/Users/ThedWord/Documents/my-project/node_modules/undertaker/lib/set-task.js:10:3)
    at Gulp.task (/Users/ThedWord/Documents/my-proje
ct/node_modules/undertaker/lib/task.js:13:8)
    at Object.<anonymous> (/Users/ThedWord/Documents/my-project/gulpfile.js:10:6)
    at Module._compile (module.js:660:30)
    at Object.Module._extensions..js (module.js:671:    
    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)
/*eslint-env node */
const gulp = require('gulp');
const sass = require('gulp-sass');
const autoprefixer = require('gulp-autoprefixer');
const browserSync = require('browser-sync').create();
const eslint = require('gulp-eslint');
const jasmine = require('gulp-jasmine-phantom');
const concat = require('gulp-concat');

gulp.task('default', ['copy-html', 'copy-images', 'styles', 'lint'], function() {
    gulp.watch('sass/**/*.scss', ['styles']);
    gulp.watch('js/**/*.js', ['lint']);
  gulp.watch('/index.html', ['copy-html']);
  gulp.watch('./build/index.html').on('change', browserSync.reload);
});

  browserSync.init({
        server: './dist'
    });


 gulp.task('scripts', function(){
    gulp.src('js/**/*.js')
      .pipe(concat('all.js'))
      .pipe(gulp.dest('dist/js'));
 });

 gulp.task('scripts-dist', function(){
   gulp.src('js/**/*.js')
      .pipe(concat('all.js'))
      .pipe(gulp.dest('dist/js'));
 });


gulp.task('copy-html', function() {
  gulp.src('./index.html')
      .pipe(gulp.dest('./dist'));
});

gulp.task('copy-images', function(){
  gulp.src('img/*')
      .pipe(gulp.dest('dist/img'));
});

gulp.task('styles', function() {
    gulp.src('sass/**/*.scss')
        .pipe(sass().on('error', sass.logError))
        .pipe(autoprefixer({
            browsers: ['last 2 versions']
        }))
        .pipe(gulp.dest('dist/css'))
        .pipe(browserSync.stream());


 });

    gulp.task('lint', function () {
        return gulp.src(['js/**/*.js'])
            // eslint() attaches the lint output to the eslint property
            // of the file object so it can be used by other modules.
            .pipe(eslint())
            // eslint.format() outputs the lint results to the console.
            // Alternatively use eslint.formatEach() (see Docs).
            .pipe(eslint.format())
            // To have the process exit with an error code (1) on
            // lint error, return the stream and pipe to failOnError last.
            .pipe(eslint.failOnError());
    });

    gulp.task('dist', [
        'copy-html',
        'copy-images',
        'styles',
        'lint',
        'scripts-dist'
    ]);

    gulp.task('tests', function () {
        gulp.src('tests/spec/extraSpec.js')
            .pipe(jasmine({
                integration: true,
                vendor: 'js/**/*.js'
            }));
    });

1 个答案:

答案 0 :(得分:2)

您遇到的问题是您正在使用gulp v4.0语法运行gulp 3AssertionError: Task function must be specified错误来自gulp.task已更改且3个参数签名已被删除的事实。简而言之,您需要更改以下任务以删除[]参数:

gulp.task('default', ['copy-html', 'copy-images', 'styles', 'lint'], function() {
    gulp.watch('sass/**/*.scss', ['styles']);
    gulp.watch('js/**/*.js', ['lint']);
  gulp.watch('/index.html', ['copy-html']);
  gulp.watch('./build/index.html').on('change', browserSync.reload);
});

This article将帮助您迁移到gulp 4语法。

如果您想简单地解决问题,而不必更改gulpfile.js,请运行以下命令将gulp的版本从版本4降级到{{1} }:

3.9.1

假设一切顺利,您应该不再看到Assertion错误。