我在gulp-assemble
使用gulp-watch
时遇到问题。我希望gulp
观看整个汇编源目录(data
,includes
,layouts
和pages
),并在文件发生变化时重新编译网站。< / p>
我能够使pages
正常运行,但gulp
在对data
,includes
或{{进行了更改时未重新编译网站1}}文件。
我已将layouts
任务添加到watch
存储库中的example gulpfile.js:
gulp-assemble
如果我运行var gulp = require('gulp');
var htmlmin = require('gulp-htmlmin');
var extname = require('gulp-extname');
var assemble = require('assemble');
var middleware = require('./examples/middleware');
var gulpAssemble = require('./');
// setup items on the assemble object
assemble.data({site: {title: 'Blog'}});
assemble.data(['test/fixtures/data/*.{json,yml}']);
assemble.layouts(['test/fixtures/layouts/*.hbs']);
assemble.partials(['test/fixtures/includes/*.hbs']);
// arbitrary middleware that runs when files loaded
assemble.onLoad(/index\.hbs/, middleware(assemble));
// render templates in `test/fixtures`
gulp.task('default', function () {
gulp.src('test/fixtures/pages/*.hbs')
.pipe(gulpAssemble(assemble, { layout: 'default' }))
.pipe(htmlmin({collapseWhitespace: true}))
.pipe(extname())
.pipe(gulp.dest('_gh_pages/'));
});
// ============================================================
// my watch task
// ============================================================
gulp.task('watch', ['default'], function() {
gulp.watch('test/fixtures/**/*.{hbs,yml,json}', ['default']);
});
并将更改保存到gulp watch
目录中的任何.hbs
文件,我看到pages
会触发我的gulp
终端输出,我看到default
中的.html
文件更新了。
但是,如果我将更改保存到_gh_pages
,.hbs
或{{1}中的任何.json
,.yml
或data
个文件}}目录,我看到includes
在我的终端输出中触发layouts
,但我看不到gulp
default
文件的更改。我必须手动运行_gh_pages
任务才能将更改应用于.html
文件。
为了获得理想的行为,我需要更改什么?
答案 0 :(得分:1)
gulp-watch只会在默认任务的函数内执行代码,所以为了重新加载数据和布局,你需要将这些代码移到函数内部(就在gulp.src之前)
var gulp = require('gulp');
var htmlmin = require('gulp-htmlmin');
var extname = require('gulp-extname');
var assemble = require('assemble');
var middleware = require('./examples/middleware');
var gulpAssemble = require('./');
// arbitrary middleware that runs when files loaded
assemble.onLoad(/index\.hbs/, middleware(assemble));
// render templates in `test/fixtures`
gulp.task('default', function () {
// setup items on the assemble object
assemble.data({site: {title: 'Blog'}});
assemble.data(['test/fixtures/data/*.{json,yml}']);
assemble.layouts(['test/fixtures/layouts/*.hbs']);
assemble.partials(['test/fixtures/includes/*.hbs']);
gulp.src('test/fixtures/pages/*.hbs')
.pipe(gulpAssemble(assemble, { layout: 'default' }))
.pipe(htmlmin({collapseWhitespace: true}))
.pipe(extname())
.pipe(gulp.dest('_gh_pages/'));
});
// ============================================================
// my watch task
// ============================================================
gulp.task('watch', ['default'], function() {
gulp.watch('test/fixtures/**/*.{hbs,yml,json}', ['default']);
});