node.js - gulp-watch 只运行一次

标签 node.js wordpress ubuntu-14.04 gulp-watch npm-scripts

Gulp watch 仅在第一次更改时运行一次。我正在 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()
}))

我希望浏览器 View 和功能会随着代码的每次更改而更新。这只会在使用“gulp watch”启动 gulp 后发生一次。

最佳答案

只运行一次的 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();
});

我也看到过建议使用这种形式的监视语句:

gulp.watch('src/pages/**/*.html').on('change',gulp.series(pages, browser.reload));

代替常用的:

gulp.watch('src/pages/**/*.html', gulp.series(pages, browserSync.reload));

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

关于node.js - gulp-watch 只运行一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54064808/

相关文章:

linux - 删除了根目录的读取和执行权限

node.js - 可扩展的 Node 快速路线

html - 如何修复 wordpress 中的粘性页脚

android - 使用死键时无法在 Android Studio 中键入单引号/双引号

php - WordPress SSL 通过 Cloudflare 重定向

javascript - 脚本不会根据当前 URL 更改 "href"和 "div"内容

virtualbox - Ubuntu 14.04 LTS VMware和Virtual Box ova/ovf镜像

javascript - 在 Node js中提交表单后无法加载CSS文件

python - 运行 "altinstall"ed python2.4 用于 nodejs 安装

javascript - Jest Node.js 无法使用单独的文件正确调用 beforeAll 和 afterAll