Gulp + Sass + Del - 随机行为

Gulp + Sass + Del - Random behaviour

我不明白为什么这个简单的脚本会因 del 插件设置而表现不同。

当我启动 gulp sass 时,我只想清理 public/css 目录和 "compile" 来自 sass 的 css。到目前为止一切顺利:

gulp.task('clean-css', del.bind(null,['./public/css']));

gulp.task('sass', ['clean-css'], function () {
  return gulp.src('./resources/sass/**/*.scss')
    .pipe(plugins.sass({outputStyle: 'compressed'})) 
    .pipe(gulp.dest('./public/css'));
});

但是,如果将 clean-css 任务更改为:

gulp.task('clean-css', del(['./public/css']));

那么就每隔一段时间才有效。第一个它清理并生成 css,下一个它删除目录但不生成任何东西。

那么,del(['./public/css'])del.bind(null,['./public/css'])有什么区别呢?为什么它会以这种方式影响脚本?

更新: 它不生成任何东西的时候我看到这个错误:

events.js:141
      throw er; // Unhandled 'error' event
            ^
Error: ENOENT: no such file or directory, open 'C:\Users\XXXX\gulp-project\public\css\style.css'
    at Error (native)

tl;dr

如果不提供回调,

Gulp不知道del什么时候完成。无论是否使用 bind,如果您的 sass 任务在 del 之前 运行 并且要删除的文件实际存在,则 del 每隔一段时间工作一次。


根据gulp documentation you should also provide the callback method to del. This is 任务完成时间:

var gulp = require('gulp');
var del = require('del');

gulp.task('clean:mobile', function (cb) {
  del([
    'dist/report.csv',
    // here we use a globbing pattern to match everything inside the `mobile` folder
    'dist/mobile/**/*',
    // we don't want to clean this file though so we negate the pattern
    '!dist/mobile/deploy.json'
  ], cb);
});

gulp.task('default', ['clean:mobile']);

我想你的任务 运行 的顺序每次都不一样,因为 gulp 不知道 del 何时完成,因为没有提供回调。根据 documentation:

If you want to create a series where tasks run in a particular order, you need to do two things:

  1. give it a hint to tell it when the task is done,
  2. and give it a hint that a task depends on completion of another.