繁体   English   中英

Gulp 4 - 注意不要看变化

[英]Gulp 4 - watch not watching changes

今天我迁移到了Gulp 4 ,但是我无法让我的手表功能正常工作。

// Watch
gulp.task('watch', gulp.series('typescript', 'sass', 'browserSync', function(){
  gulp.watch('./app/styles/**/*.scss', gulp.series('sass'));
  gulp.watch('app/scripts/**/*.ts', gulp.series('typescript'));
  gulp.watch('./app/*.html', browserSync.reload);
}));

typescriptsassbrowserSync将运行,但手表不应对文件更改。

我刚遇到同样的问题。 实际上,您没有在watch browserSync任务中引用初始化的browserSync 而不是在单独的browserSync任务中使用browserSync.init函数,将其移动到watch任务内部,它应该可以工作。 希望这可以帮助!

例:

gulp.task('watch', gulp.series(function (){
  browserSync.init({
    proxy:"http://localhost:8888",
    injectChanges: true,
    plugins: ["bs-html-injector?files[]=*.html"]
  });
  var html = gulp.watch('development/index.html');
  html.on('change', function(path, stats) {
    console.log('you changed the html');
    browserSync.notify("Compiling, please wait!");
    browserSync.reload("index.html");
  })
  var js = gulp.watch('development/**/*.js');
  js.on('change', function(path, stats) {
    console.log('you changed the js');
    browserSync.notify("Compiling, please wait!");
    browserSync.reload();
  })
  var css = gulp.watch('development/**/*.css');
  css.on('change', function(path, stats) {
    console.log('you changed the css');
    browserSync.notify("Injecting CSS!");
    browserSync.reload("*.css");
  })
}));

改变gulp.watch('app/scripts/**/*.ts', gulp.series('typescript')); 绝对gulp.watch('./app/scripts/**/*.ts', gulp.series('typescript'));

此外,我通常坚持每个任务的语法。

var watcher = gulp.watch('js/**/*.js', gulp.parallel('concat', 'uglify'));
watcher.on('change', function(path, stats) {
  console.log('File ' + path + ' was changed');
});

我也有一些问题,我发现这个gulp 4的教程 ,这是我的gulpfile来编译scss并观察Scss文件,concat并使用autoprefix编译到main.min.css。

var gulp = require('gulp'),
    concat  = require('gulp-concat'),
    autoprefixer    = require('gulp-autoprefixer'),
    sass = require('gulp-sass');

//task para o sass

var paths = {
  styles: {
    src: 'scss/**/*.scss',
    dest: 'css'
  }
};

function styles() {
  return gulp
    .src(paths.styles.src, {
      sourcemaps: true
    })
  .pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError))
  .pipe(concat('main.min.css'))
  .pipe(autoprefixer({
    browser: ['last 2 version', '> 5%'],
    cascade: false
  }))
  .pipe(gulp.dest(paths.styles.dest));
}

function watch() {
  gulp.watch(paths.styles.src, styles);
}

var build = gulp.parallel(styles, watch);
gulp.task(build);
gulp.task('default', build);

我认为在gulp v4上你需要使用gulp.parallel 我正在挖掘以了解有关新版本的更多信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM