Gulps gulp.watch は新規ファイルまたは削除されたファイルに対してトリガーされませんか? 質問する

Gulps gulp.watch は新規ファイルまたは削除されたファイルに対してトリガーされませんか? 質問する

次の Gulpjs タスクは、glob マッチでファイルを編集するときに正常に機能します。

// watch task.
gulp.task('watch', ['build'], function () {
    gulp.watch(src + '/js/**/*.js', ['scripts']);
    gulp.watch(src + '/img//**/*.{jpg,jpeg,png,gif}', ['copy:images']);
    gulp.watch(src + '/less/*.less', ['styles']);
    gulp.watch(src + '/templates/**/*.{swig,json}', ['html']);
});

// build task.
gulp.task('build', ['clean'], function() {
    return gulp.start('copy', 'scripts', 'less', 'htmlmin');
});

ただし、新規ファイルまたは削除されたファイルでは機能しません (トリガーされません)。何か見落としている点があるのでしょうか?

編集: grunt-watch プラグインを使用しても動作しないようです:

gulp.task('scripts', function() {
    return streamqueue(
        { objectMode: true },
        gulp.src([
            vendor + '/jquery/dist/jquery.min.js',
            vendor + '/bootstrap/dist/js/bootstrap.min.js'
        ]),
        gulp.src([
            src + '/js/**/*.js'
        ]).pipe(plugins.uglify())
    )
    .pipe(plugins.concat(pkg.name + '.min.js'))
    .pipe(gulp.dest(dest + '/js/'));
});

gulp.task('watch', ['build'], function () {
    plugins.watch({glob: src + '/js/**/*.js'}, function () {
        gulp.start('scripts');
    });
});

編集: 解決しましたこの問題./。 ( の値)で始まるグロブは、src現時点では機能していないようです。

ベストアンサー1

編集:どうやら、gulp.watch現在は新規ファイルや削除されたファイルでも動作するようです。質問があったときは動作しませんでした。

私の回答の残りの部分は変わりません。gulp-watchは通常、より優れたソリューションです。 では、変更されたファイルに対してのみ特定のアクションを実行できますが、 ではgulp.watch完全なタスクしか実行できないためです。 適度なサイズのプロジェクトの場合、これはすぐに遅すぎて役に立たなくなります。


何も見逃していません。gulp.watch新規ファイルや削除されたファイルでは機能しません。これは、単純なプロジェクト向けに設計されたシンプルなソリューションです。

新しいファイルを探すファイル監視機能を利用するには、プラグgulp-watchイン、これははるかに強力です。使用方法は次のとおりです。

var watch = require('gulp-watch');

// in a task
watch({glob: <<glob or array of globs>> })
        .pipe( << add per-file tasks here>> );

// if you'd rather rerun the whole task, you can do this:
watch({glob: <<glob or array of globs>>}, function() {
    gulp.start( <<task name>> );
});

個人的には、最初のオプションをお勧めします。これにより、ファイルごとのプロセスがはるかに高速になります。ファイルを連結しない限り、livereload を使用した開発中はうまく機能します。

ストリームを終了するには、私のlazypipe図書館、または単に関数を使用してstream-combinerこのような:

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

function scriptsPipeline() {
    return combine(coffeeescript(), uglify(), gulp.dest('/path/to/dest'));
}

watch({glob: 'src/scripts/**/*.js' })
        .pipe(scriptsPipeline());

アップデート2014年10月15日

下記の @pkyeck が指摘しているように、 の 1.0 リリースではgulp-watch形式が若干変更されたため、上記の例は次のようになります。

var watch = require('gulp-watch');

// in a task
watch(<<glob or array of globs>>)
        .pipe( << add per-file tasks here>> );

// if you'd rather rerun the whole task, you can do this:
watch(<<glob or array of globs>>, function() {
    gulp.start( <<task name>> );
});

そして

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

function scriptsPipeline() {
    return combine(coffeeescript(), uglify(), gulp.dest('/path/to/dest'));
}

watch('src/scripts/**/*.js')
        .pipe(scriptsPipeline());

おすすめ記事