0

我正在为 WordPress 开发自己的主题,我开始了解 Glup 以及它使我的工作流程变得多么容易,我在下面的代码中面临的问题是我能够看到我对主页(html或 php),但我对 css 文件或 java-script 文件所做的任何更改都不会受到影响,我仍然必须手动刷新页面:

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(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'));
});

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()
}))
4

1 回答 1

1

试试这个:

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))

     // added below
    .pipe(browserSync.stream());
});

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


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

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

      // change to gulp.series below 
  // gulp.watch(settings.themeLocation + 'css/**/*.css', gulp.series('waitForStyles'));

   // changed to 'styles' below        
 gulp.watch(settings.themeLocation + 'css/**/*.css', gulp.series('styles'));

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

  cb();
});

我已经看到 gulp4 只有一个任务 ala 有问题gulp.parallel('oneTaskHere'),所以尝试在你的监视语句中交换parallelwith上面的代码。series

我进行了一些编辑以简化代码 - 试一试。不需要'waitForStyles',只需将browserSync.stream()管道移动到styles任务结束即可。

或者不要移动 browserSync.stream 管道,只需执行以下操作:

gulp.watch(settings.themeLocation + 'css/**/*.css', gulp.series('styles', browserSync.reload));

'styles'但我自己在任务版本结束时似乎对 browserSync 管道有更好的运气。

因为您使用的是webpack插件,所以我假设脚本任务的处理方式必须与样式任务不同。您可以尝试:

gulp.watch([settings.themeLocation + 'js/modules/*.js', settings.themeLocation + 'js/scripts.js'], gulp.series('waitForScripts', browserSync.reload));

然后不需要'waitForScripts'任务。

于 2019-03-14T03:16:16.663 回答