gulp-watch 只运行一次

Cli*_*ner 5 wordpress node.js ubuntu-14.04 gulp-watch npm-scripts

Gulpwatch在第一次更改时只运行一次。我正在 ubuntu 14 上的 apache2 上运行 Wordpress,并且正在使用教程。它说明了如何使用 gulp 来观察变化。第一次更改可以正常工作 - 刷新浏览器http://localhost:3000。我对设置 gulp watch 非常陌生,所以我不知道为什么它只运行一次。gulpfile.js 如下。如果有人需要其他任何澄清,请回来。谢谢

我已经完成了将其关闭并再次打开的编码版本。我已经在 stackoverflow 和其他地方查看了针对此类问题的各种响应。

var gulp = require('gulp'),
   settings = require('./settings'),
   webpack = require('webpack'),
   browserSync = require('browser-sync').create(),
   postcss = require('gulp-postcss'),
   rgba = require('postcss-hexrgba'),
   autoprefixer = require('autoprefixer'),
   cssvars = require('postcss-simple-vars'),
   nested = require('postcss-nested'),
   cssImport = require('postcss-import'),
   mixins = require('postcss-mixins'),
   colorFunctions = require('postcss-color-function');

gulp.task('styles', function() {
  return gulp.src(settings.themeLocation + 'css/style.css')
    .pipe(postcss([cssImport, mixins, cssvars, nested, rgba, colorFunctions, autoprefixer]))
    .on('error', (error) => console.log(error.toString()))
    .pipe(gulp.dest(settings.themeLocation));
});

gulp.task('scripts', function(callback) {
  webpack(require('./webpack.config.js'), function(err, stats) {
    if (err) {
      console.log(err.toString());
    }

    console.log(stats.toString());
    callback();
  });
});

gulp.task('watch', function() {
 browserSync.init({
    notify: false,
    proxy: settings.urlToPreview,
    ghostMode: false
  });

  gulp.watch('./**/*.php', function() {
    browserSync.reload();
  });

  gulp.watch(settings.themeLocation + 'css/**/*.css', gulp.parallel('waitForStyles'));

  gulp.watch([settings.themeLocation + 'js/modules/*.js', settings.themeLocation + 'js/scripts.js'], 
       gulp.parallel('waitForScripts'));
});

gulp.task('waitForStyles', gulp.series('styles', function() {
  return gulp.src(settings.themeLocation + 'style.css')
    .pipe(browserSync.stream());
}))

gulp.task('waitForScripts', gulp.series('scripts', function(cb) {
  browserSync.reload();
  cb()
}))
Run Code Online (Sandbox Code Playgroud)

我希望每次更改代码时都会更新浏览器视图和功能。这只会在用 ' gulp watch'开始 gulp 后发生一次。

Mar*_*ark 13

只运行一次的 gulp 任务通常表示 gulp 无法确定它已经完成第一次,因此 gulp 不会再次运行它。

更改为此代码:

// notice the "done" added below
gulp.task('watch', function(done) {
 browserSync.init({
    notify: false,
    proxy: settings.urlToPreview,
    ghostMode: false
  });

  //  I also added a callback function below, just within this one gulp.watch
  gulp.watch('./**/*.php', function(done) {
    browserSync.reload();
    done();
  });

  gulp.watch(settings.themeLocation + 'css/**/*.css', gulp.parallel('waitForStyles'));
  gulp.watch([settings.themeLocation + 'js/modules/*.js', settings.themeLocation + 'js/scripts.js'], 
             gulp.parallel('waitForScripts'));
  done();
});
Run Code Online (Sandbox Code Playgroud)

我还看到有人建议使用这种形式的 watch 语句:

gulp.watch('src/pages/**/*.html').on('change',gulp.series(pages, browser.reload));
Run Code Online (Sandbox Code Playgroud)

而不是常用的:

gulp.watch('src/pages/**/*.html', gulp.series(pages, browserSync.reload));
Run Code Online (Sandbox Code Playgroud)

因此,如果回调解决方案不起作用,请尝试使用建议的watch.