gulp.watch() 没有运行后续任务 [英] gulp.watch() not running subsequent task

查看:49
本文介绍了gulp.watch() 没有运行后续任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试将模块化 gulp 任务拆分为单独的文件时遇到了一个奇怪的错误.以下应该执行任务 css,但不执行:

Running into a bizarre bug when trying to make modular gulp tasks by splitting them into separate files. The following should execute the task css, but does not:

var gulp = require('gulp');
var plugins = require('gulp-load-plugins')();

gulp.task('watch', function () {
  plugins.watch('assets/styl/**/*.styl', ['css']); // PROBLEM
});

plugins.watch() 中声明 ['css'] 在技术上应该接下来运行以下任务:

Declaring ['css'] in plugins.watch() should technically run the following task next:

var gulp = require('gulp');
var plugins = require('gulp-load-plugins')();

gulp.task('css', function () {
  return gulp.src('assets/styl/*.styl')
    .pipe(plugins.stylus())
    .pipe(gulp.dest('/assets/css'));
});

文件:gulpfile.js

var gulp = require('gulp');
var requireDir = require('require-dir');
requireDir('./gulp/tasks', { recurse: true });

gulp.task('develop', ['css', 'watch']);

文件夹结构

<代码>- 吞咽/- 任务/-css.js- watch.js-gulpfile.js

gulp develop 应该运行任务 csswatch(确实如此).在文件更改时,watch 应该检测到它们(它会)然后运行 ​​css 任务(它不会).

gulp develop should run tasks css and watch (it does). On file changes, watch should detect them (it does) and then run the css task (it's does not).

不太喜欢这个解决方案,因为 gulp.start() 在下一个版本中被弃用,但这确实解决了它:

Not terribly fond of this solution as gulp.start() is being deprecated in the next release, but this does fix it:

plugins.watch('assets/styl/**/*.styl', function() {
  gulp.start('css');
});

推荐答案

要么使用gulp的内置手表,语法如下:

Either use gulp's builtin watch with this syntax:

gulp.task('watch', function () {
  gulp.watch('assets/styl/**/*.styl', ['css']);
});

gulp-watch插件 使用这种语法:

gulp.task('watch', function () {
  plugins.watch('assets/styl/**/*.styl', function (files, cb) {
    gulp.start('css', cb);
  });
});

您的 gulp.dest 路径中也可能有错字.将其更改为相对:

There's also probably a typo in your gulp.dest path. Change it to relative:

.pipe(gulp.dest('assets/css'));

这篇关于gulp.watch() 没有运行后续任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆