5

在遵循记录的示例后,我无法让gulp-watchgulp-watch-less触发。我最初遇到的问题是lazypipe(此处未显示),但在我看来,我在使用插件的方式上做错了。这是我的愚蠢代码,但仍然无法正常工作。

请注意,我使用普通的 gulp-watch 进行了尝试,它表现出完全相同的问题:它不会触发后续管道的更改。如果出现问题,我将在此处包含相关信息。

这是我的 gulpfile。

var debug = require ( 'gulp-debug' );
var gulp = require ( 'gulp' );
var less = require ( 'gulp-less' );
var watchLess = require ( 'gulp-watch-less' ); 

gulp.task ( 'dev-watch', function () {
  // main.less just imports child less files
  gulp.src ( './app/styles/less/main.less' )
    .pipe ( watchLess ( './app/styles/less/main.less' ) )
    .pipe ( debug () );
    .pipe ( less () )
    .pipe ( gulp.dest ( './app/styles' ) )
  ;
});

当我启动任务时,它会完美地执行并生成预期的文件。我看到调试输出流信息也很好。

当我更改文件时,我看到 watchLess 正在接受更改:

 [10:49:54] LESS saw child.less was changed
 [10:49:54] LESS saw child.less was changed
 [10:49:54] LESS saw main.less was changed:by:import
 [10:49:54] LESS saw main.less was changed:by:import

但是,较少的任务不会执行。它似乎没有发出任何东西,因为调试没有触发。

这是相关的 package.json 信息:

"devDependencies": {
  "gulp": "^3.8.7",
  "gulp-less": "^1.3.6",
  "gulp-watch": "^1.2.0",
  "gulp-watch-less": "^0.2.1"
}
4

2 回答 2

1

您的代码仅在管道中运行观察程序,但不告诉该做什么。

工作示例应如下所示:

var
  gulp = require('gulp'),
  debug = require ('gulp-debug'),
  less = require ( 'gulp-less'),
  watchLess = require('gulp-watch-less');

gulp.task('dev-watch', function () {
  watchLess('./app/styles/less/main.less')
    .pipe (debug ())
    .pipe(less())
    .pipe(gulp.dest('./app/styles'))
});

然而,你也可以只使用 gulp-watch 或 gulp (gulp.watch) 来做同样的事情。

于 2015-07-22T09:39:54.907 回答
0

这一定是最好的解决方案,我在 gulp-less github 中进入自述文件; https://github.com/plus3network/gulp-less https://github.com/gulpjs/gulp/blob/master/docs/recipes/combining-streams-to-handle-errors.md npm i stream-combiner2 --save-dev

var combiner = require('stream-combiner2');

var combined = combiner.obj([
    gulp.src(srcs),
    less(),
    autoprefixer({
        browsers: ['last 6 versions'],
        cascade: false
    }),
    isDev ? null : cleanCss(),
    gulp.dest(targetDir + 'css/multi/'),
].filter(v => v));

// any errors in the above streams will get caught
// by this listener, instead of being thrown:
combined.on('error', console.error.bind(console));
combined.on('end', () => {}); //done have been call when return combined;
return combined;
于 2017-02-06T12:05:51.850 回答