Browser Sync + Gulp + Jade,为什么要分离 Jade watch 任务?
Browser Sync + Gulp + Jade, Why separate the Jade watch task?
我在看这个 browser sync recipe 这是一个与 jade,sass 和浏览器同步一起工作的 gulpfile 配置,我不关心 sass 所以为了简化我修改了代码一点:
var gulp = require('gulp');
var browserSync = require('browser-sync');
var jade = require('gulp-jade');
var reload = browserSync.reload;
/**
* Compile jade files into HTML
*/
gulp.task('templates', function() {
return gulp.src('./app/*.jade')
.pipe(jade())
.pipe(gulp.dest('./dist/'));
});
/**
* Important!!
* Separate task for the reaction to `.jade` files
*/
gulp.task('jade-watch', ['templates'], reload);
/**
* Serve and watch the jade files for changes
*/
gulp.task('default', ['templates'], function () {
browserSync({server: './dist'});
gulp.watch('./app/*.jade', ['jade-watch']);
});
我不明白的是这条评论:
/**
* Important!!
* Separate task for the reaction to `.jade` files
*/
为什么这很重要?为什么不这样做呢?
/**
* Compile jade files into HTML
*/
gulp.task('templates', function() {
return gulp.src('./app/*.jade')
.pipe(jade())
.pipe(gulp.dest('./dist/'))
.pipe(reload({stream: true}));
});
/**
* Serve and watch the jade files for changes
*/
gulp.task('default', ['templates'], function () {
browserSync({server: './dist'});
gulp.watch('./app/*.jade', ['templates']);
});
你现在可能已经明白了;但以防万一其他人想知道同样的事情(就像我一样):通过将 'templates' 任务设置为 'jade-watch' 的依赖项,您可以确保它在触发重新加载之前已完成。
我在看这个 browser sync recipe 这是一个与 jade,sass 和浏览器同步一起工作的 gulpfile 配置,我不关心 sass 所以为了简化我修改了代码一点:
var gulp = require('gulp');
var browserSync = require('browser-sync');
var jade = require('gulp-jade');
var reload = browserSync.reload;
/**
* Compile jade files into HTML
*/
gulp.task('templates', function() {
return gulp.src('./app/*.jade')
.pipe(jade())
.pipe(gulp.dest('./dist/'));
});
/**
* Important!!
* Separate task for the reaction to `.jade` files
*/
gulp.task('jade-watch', ['templates'], reload);
/**
* Serve and watch the jade files for changes
*/
gulp.task('default', ['templates'], function () {
browserSync({server: './dist'});
gulp.watch('./app/*.jade', ['jade-watch']);
});
我不明白的是这条评论:
/**
* Important!!
* Separate task for the reaction to `.jade` files
*/
为什么这很重要?为什么不这样做呢?
/**
* Compile jade files into HTML
*/
gulp.task('templates', function() {
return gulp.src('./app/*.jade')
.pipe(jade())
.pipe(gulp.dest('./dist/'))
.pipe(reload({stream: true}));
});
/**
* Serve and watch the jade files for changes
*/
gulp.task('default', ['templates'], function () {
browserSync({server: './dist'});
gulp.watch('./app/*.jade', ['templates']);
});
你现在可能已经明白了;但以防万一其他人想知道同样的事情(就像我一样):通过将 'templates' 任务设置为 'jade-watch' 的依赖项,您可以确保它在触发重新加载之前已完成。