Gulps gulp.watch not triggered for new or deleted files?

前端 未结 7 1024

The following Gulpjs task works fine when editing files in the glob match:

// watch task.
gulp.task(\'watch\', [\'build\'], function () {
    gulp.watch(src          


        
7条回答
  •  忘掉有多难
    2020-11-27 11:35

    I know this is an older question, but I thought I'd throw the solution I came up with. None of the gulp plugins I found would notify me of new or renamed files. So I ended up wrapping monocle in a convenience function.

    Here's an example of how that function is used:

    watch({
        root: config.src.root,
        match: [{
          when: 'js/**',
          then: gulpStart('js')
        }, {
          when: '+(scss|css)/**',
          then: gulpStart('css')
        }, {
          when: '+(fonts|img)/**',
          then: gulpStart('assets')
        }, {
          when: '*.+(html|ejs)',
          then: gulpStart('html')
        }]
      });
    

    I should note that gulpStart is also a convenience function I made.

    And here is the actual watch module.

    module.exports = function (options) {
      var path = require('path'),
          monocle = require('monocle'),
          minimatch = require('minimatch');
    
      var fullRoot = path.resolve(options.root);
    
      function onFileChange (e) {
        var relativePath = path.relative(fullRoot, e.fullPath);
    
        options.match.some(function (match) {
          var isMatch = minimatch(relativePath, match.when);
          isMatch && match.then();
          return isMatch;
        });
      }
    
      monocle().watchDirectory({
        root: options.root,
        listener: onFileChange
      });
    };
    

    Pretty simple, eh? The whole thing can be found over at my gulp starter kit: https://github.com/chrisdavies/gulp_starter_kit

提交回复
热议问题