简体   繁体   English

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

[英]Gulp-file-include doesn't work with gulp.watch

I want to use gulp-file-include to assemble the index.html from different HTML files.我想使用 gulp gulp-file-include从不同的 HTML 文件中组装 index.html 。 The dist folder contains all production files. dist 文件夹包含所有生产文件。 The problem is that gulp-watch does not recognize if an HTML file has changed, so the HTML file in the dist folder will not be updated while I run watch.问题是 gulp gulp-watch无法识别 HTML 文件是否已更改,因此当我运行 watch 时, dist 文件夹中的 HTML 文件不会更新。

How can I customize gulp so that gulp-watch also detects these changes in real-time?如何自定义 gulp 以便 gulp gulp-watch也实时检测到这些变化?

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

index.html索引.html

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

gulpfile.js 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;

BrowserSync is amazing but always seems tough to get it running the way you want. BrowserSync 很棒,但似乎总是很难让它以你想要的方式运行。 I endedup creating a little test projct to debug your gulpfile.js我最终创建了一个小测试项目来调试你的 gulpfile.js

Here is a tested, working version of your code, notes below:这是您的代码的经过测试的工作版本,注释如下:

'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;

Only a couple of bits had to change to get this working:只需更改几位即可使其正常工作:

  1. I think the main issue you were seeing was you weren't recompiling the html on change, it was just reloading the browser.我认为您看到的主要问题是您没有在更改时重新编译 html,它只是重新加载浏览器。 I've updated the watch html from: gulp.watch('./**/*.html', browserSyncReload);我已经更新了手表 html 来自: gulp.watch('./**/*.html', browserSyncReload); to: gulp.watch(['./src/**/*.html', 'index.html'], gulp.series(html, browserSyncReload)); to: gulp.watch(['./src/**/*.html', 'index.html'], gulp.series(html, browserSyncReload));
  2. The stream is meant for injecting in code onto the page, so won't work as you want for the html and js , so I've removed it from the tasks and replaced with a reload in the watch task. stream用于将代码注入页面,因此对于htmljs将无法正常工作,因此我已将其从任务中删除并替换为监视任务中的reload
  3. Exports for browserSyncReload and server were needed需要导出browserSyncReloadserver
  4. It seemed there was a race issue with watchFiles and browserSync when running in parallel, so they now run in series. watchFilesbrowserSync在并行运行时似乎存在竞争问题,因此它们现在串联运行。 ie: The server builds first, then the files are watched.即:服务器先构建,然后文件被监视。

Changes that weren't required but happened along the way:不需要但在此过程中发生的更改:

  1. Having a function named browserSync and const named browsersync was confusing me, so the function has been renamed to server and the const is now browserSync .有一个名为 browserSync 的browserSync和名为browsersync的 const 让我感到困惑,因此 function 已重命名为server ,而 const 现在是browserSync
  2. My editor config set all your quotes to single, rather than a mix.我的编辑器配置将所有引号设置为单个,而不是混合。
  3. I have set the stream in the css task to only match: '**/*.css'我已将stream 8CBA22E28EB17B5F5C6AE2A266AZ 任务中的 stream 设置为仅match: '**/*.css'

Try changing:尝试改变:

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

to

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

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

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