我想使用gulp-file-include
来组装来自不同HTML文件的index.html。 dist文件夹包含所有生产文件。问题是gulp-watch
无法识别HTML文件是否已更改,因此在我运行watch时dist文件夹中的HTML文件不会被更新。
如何自定义口杯,以便gulp-watch
还能实时检测到这些变化?
-- dist
---- css
---- js
---- vendor
---- index.html
-- res
---- js
---- scss
-- src
---- footer.html
---- nav.html
gulpfile.js
index.html
index.html
@@include('./src/nav.html')
@@include('./src/footer.html')
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 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;
答案 0 :(得分:2)
BrowserSync很棒,但始终很难以您想要的方式运行它!我最终创建了一个小测试项目来调试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;
只有几位需要更改才能运行:
gulp.watch('./**/*.html', browserSyncReload);
更新为:gulp.watch(['./src/**/*.html', 'index.html'], gulp.series(html, browserSyncReload));
stream
用于将代码注入到页面中,因此对于html
和js
而言,它并不能正常工作,因此我将其从任务中删除了并在监视任务中替换为reload
。browserSyncReload
和server
的watchFiles
和browserSync
并行运行时出现比赛问题,因此它们现在串联运行。即:首先构建服务器,然后监视文件。不是必需的,而是在更改过程中发生的更改:
browserSync
的函数和一个名为browsersync
的const令我感到困惑,因此该函数已重命名为server
,而const现在为browserSync
。stream
设置为仅match: '**/*.css'
答案 1 :(得分:0)
尝试更改:
add_action( 'wpcf7_mail_sent', 'show_pdf_button',11 );
function show_pdf_button() {
?>
<script type="text/javascript">
document.addEventListener( 'wpcf7mailsent', function( event ) {
if ( '18' == event.detail.contactFormId ) { // Change 18 to the ID of the form
jQuery('#yourdivid').show(); //Replace yourdivid with your div id
}
}, false );
</script>
<?php } ?>
到
gulp.watch("./**/*.html", browserSyncReload);