Gulp-file-include 不适用于 gulp.watch



我想使用gulp-file-include来组装索引.html来自不同的HTML文件。dist 文件夹包含所有生产文件。问题是gulp-watch无法识别 HTML 文件是否已更改,因此在我运行 watch 时不会更新 dist 文件夹中的 HTML 文件。

如何自定义 gulp 以便gulp-watch也实时检测这些变化?

-- dist
---- css
---- js
---- vendor
---- index.html
-- res
---- js
---- scss
-- src
---- footer.html
---- nav.html
gulpfile.js
index.html

索引.html

@@include('./src/nav.html')
@@include('./src/footer.html')

古尔普文件.js

"use strict";
// Load plugins
const autoprefixer = require("gulp-autoprefixer");
const browsersync = require("browser-sync").create();
const cleanCSS = require("gulp-clean-css");
const del = require("del");
const gulp = require("gulp");
const header = require("gulp-header");
const merge = require("merge-stream");
const plumber = require("gulp-plumber");
const rename = require("gulp-rename");
const sass = require("gulp-sass");
const uglify = require("gulp-uglify");
const fileinclude = require('gulp-file-include');
// Load package.json for banner
const pkg = require('./package.json');
// BrowserSync
function browserSync(done) {
browsersync.init({
server: {
baseDir: "./dist/"
},
port: 3000
});
done();
}
// BrowserSync reload
function browserSyncReload(done) {
browsersync.reload();
done();
}
// Clean vendor
function clean() {
return del(["./dist/vendor/"]);
}
// Bring third party dependencies from node_modules into vendor directory
function modules() {
// Bootstrap
var bootstrap = gulp.src('./node_modules/bootstrap/dist/**/*')
.pipe(gulp.dest('./dist/vendor/bootstrap'));
// Font Awesome CSS
var fontAwesomeCSS = gulp.src('./node_modules/@fortawesome/fontawesome-free/css/**/*')
.pipe(gulp.dest('./dist/vendor/fontawesome-free/css'));
// Font Awesome Webfonts
var fontAwesomeWebfonts = gulp.src('./node_modules/@fortawesome/fontawesome-free/webfonts/**/*')
.pipe(gulp.dest('./dist/vendor/fontawesome-free/webfonts'));
// jQuery Easing
var jqueryEasing = gulp.src('./node_modules/jquery.easing/*.js')
.pipe(gulp.dest('./dist/vendor/jquery-easing'));
// jQuery
var jquery = gulp.src([
'./node_modules/jquery/dist/*',
'!./node_modules/jquery/dist/core.js'
])
.pipe(gulp.dest('./dist/vendor/jquery'));
return merge(bootstrap, fontAwesomeCSS, fontAwesomeWebfonts, jquery, jqueryEasing);
}
// CSS task
function css() {
return gulp
.src("./res/scss/**/*.scss")
.pipe(plumber())
.pipe(sass({
outputStyle: "expanded",
includePaths: "./node_modules",
}))
.on("error", sass.logError)
.pipe(autoprefixer({
cascade: false
}))
.pipe(header(banner, {
pkg: pkg
}))
.pipe(gulp.dest("./dist/css"))
.pipe(rename({
suffix: ".min"
}))
.pipe(cleanCSS())
.pipe(gulp.dest("./dist/css"))
.pipe(browsersync.stream());
}
// JS task
function js() {
return gulp
.src([
'./res/js/*.js',
'!./res/js/*.min.js',
'!./res/js/contact_me.js',
'!./res/js/jqBootstrapValidation.js'
])
.pipe(uglify())
.pipe(header(banner, {
pkg: pkg
}))
.pipe(rename({
suffix: '.min'
}))
.pipe(gulp.dest('./dist/js'))
.pipe(browsersync.stream());
}
function html() {
return gulp
.src(['index.html'])
.pipe(fileinclude({
prefix: '@@',
basepath: '@file'
}))
.pipe(gulp.dest('./dist/'))
.pipe(browsersync.stream());
}
// Watch files
function watchFiles() {
gulp.watch("./res/scss/**/*", css);
gulp.watch(["./res/js/**/*", "!./dist/js/**/*.min.js"], js);
gulp.watch("./**/*.html", browserSyncReload);
}
// Define complex tasks
const vendor = gulp.series(clean, modules);
const build = gulp.series(vendor, gulp.parallel(css, js, html));
const watch = gulp.series(build, gulp.parallel(watchFiles, browserSync));
// Export tasks
exports.css = css;
exports.js = js;
exports.html = html;
exports.clean = clean;
exports.vendor = vendor;
exports.build = build;
exports.watch = watch;
exports.default = build;

浏览器同步是惊人的,但似乎总是很难让它按照你想要的方式运行!我最终创建了一个小测试程序来调试您的 gulpfile.js

下面是代码的经过测试的工作版本,注释如下:

'use strict';
// Load plugins
const autoprefixer = require('gulp-autoprefixer');
const browserSync = require('browser-sync').create();
const cleanCSS = require('gulp-clean-css');
const del = require('del');
const gulp = require('gulp');
const header = require('gulp-header');
const merge = require('merge-stream');
const plumber = require('gulp-plumber');
const rename = require('gulp-rename');
const sass = require('gulp-sass');
const uglify = require('gulp-uglify');
const fileinclude = require('gulp-file-include');
// Load package.json for banner
const pkg = require('./package.json');
// BrowserSync
function server(done) {
browserSync.init({
server: {
baseDir: './dist/'
},
port: 3000
});
done();
}
// server reload
function browserSyncReload(done) {
browserSync.reload();
done();
};
// Clean vendor
function clean() {
return del(['./dist/vendor/']);
}
// Bring third party dependencies from node_modules into vendor directory
function modules() {
// Bootstrap
var bootstrap = gulp.src('./node_modules/bootstrap/dist/**/*')
.pipe(gulp.dest('./dist/vendor/bootstrap'));
// Font Awesome CSS
var fontAwesomeCSS = gulp.src('./node_modules/@fortawesome/fontawesome-free/css/**/*')
.pipe(gulp.dest('./dist/vendor/fontawesome-free/css'));
// Font Awesome Webfonts
var fontAwesomeWebfonts = gulp.src('./node_modules/@fortawesome/fontawesome-free/webfonts/**/*')
.pipe(gulp.dest('./dist/vendor/fontawesome-free/webfonts'));
// jQuery Easing
var jqueryEasing = gulp.src('./node_modules/jquery.easing/*.js')
.pipe(gulp.dest('./dist/vendor/jquery-easing'));
// jQuery
var jquery = gulp.src([
'./node_modules/jquery/dist/*',
'!./node_modules/jquery/dist/core.js'
])
.pipe(gulp.dest('./dist/vendor/jquery'));
return merge(bootstrap, fontAwesomeCSS, fontAwesomeWebfonts, jquery, jqueryEasing);
}
// CSS task
function css() {
return gulp
.src('./res/scss/**/*.scss')
.pipe(plumber())
.pipe(sass({
outputStyle: 'expanded',
includePaths: './node_modules',
}))
.on('error', sass.logError)
.pipe(autoprefixer({
cascade: false
}))
.pipe(header(banner, {
pkg: pkg
}))
.pipe(gulp.dest('./dist/css'))
.pipe(rename({
suffix: '.min'
}))
.pipe(cleanCSS())
.pipe(gulp.dest('./dist/css'))
.pipe(browserSync.stream({match: '**/*.css'}));
}
// JS task
function js() {
return gulp
.src([
'./res/js/*.js',
'!./res/js/*.min.js',
'!./res/js/contact_me.js',
'!./res/js/jqBootstrapValidation.js'
])
.pipe(uglify())
.pipe(header(banner, {
pkg: pkg
}))
.pipe(rename({
suffix: '.min'
}))
.pipe(gulp.dest('./dist/js'));
}
function html() {
return gulp
.src(['index.html'])
.pipe(fileinclude({
prefix: '@@',
basepath: '@file'
}))
.pipe(gulp.dest('./dist/'));
}
// Watch files
function watchFiles() {
gulp.watch('./res/scss/**/*', css);
gulp.watch(['./res/js/**/*', '!./dist/js/**/*.min.js'], gulp.series(js, browserSyncReload));
gulp.watch(['./src/**/*.html', 'index.html'], gulp.series(html, browserSyncReload));
}
// Define complex tasks
const vendor = gulp.series(clean, modules);
const build = gulp.series(vendor, gulp.parallel(css, js, html));
const watch = gulp.series(build, server, watchFiles);
// Export tasks
exports.css = css;
exports.js = js;
exports.html = html;
exports.clean = clean;
exports.vendor = vendor;
exports.build = build;
exports.watch = watch;
exports.browserSyncReload = browserSyncReload;
exports.server = server;
exports.default = build;

只需更改几位即可使其正常工作:

  1. 我认为您看到的主要问题是您没有在更改时重新编译 html,它只是重新加载浏览器。我已经将手表 html 从:gulp.watch('./**/*.html', browserSyncReload);更新为:gulp.watch(['./src/**/*.html', 'index.html'], gulp.series(html, browserSyncReload));
  2. stream用于将代码注入到页面上,因此对于htmljs,我已将其从任务中删除并替换为监视任务中的reload
  3. 需要出口browserSyncReloadserver
  4. 并行
  5. 运行时,watchFilesbrowserSync似乎存在比赛问题,因此它们现在串联运行。 即:首先构建服务器,然后监视文件。

不需要但在此过程中发生的更改:

  1. 有一个名为browserSync和 const 命名为browsersync的函数让我感到困惑,因此该函数已重命名为server,并且现在browserSyncconst 。
  2. 我的编辑器配置将您的所有引号设置为单个引号,而不是混合引号。
  3. 我已将 css 任务中的stream设置为仅match: '**/*.css'

尝试更改:

gulp.watch("./**/*.html", browserSyncReload);

gulp.watch("./src/**/*.html", browserSyncReload);

最新更新