简体   繁体   English

页面没有使用Gulp 4和Browsersync重新加载/自动注入

[英]Page not reloading/autoinjecting with Gulp 4 and Browsersync

Attempting to create boilerplate for new PWA creation using Gulp 4 with Browsersync to automate tasks, auto-reload, and auto-inject changes. 尝试使用Gulp 4和Browsersync创建用于创建新PWA的样板,以自动执行任务,自动重新加载和自动注入更改。 Dist folder files are being updated when changes are made. 正在进行更改时正在更新Dist文件夹文件。 Reload only works with HTML alone. 重新加载仅适用于HTML。 CSS doesn't auto-inject and HTML doesn't reload after changing CSS and HTML. CSS不会自动注入,并且在更改CSS和HTML后不会重新加载HTML。 https://gist.github.com/flaura42/a22823d97baf889160b2b18ca85dbfb4 https://gist.github.com/flaura42/a22823d97baf889160b2b18ca85dbfb4

I have read a bunch of other SO questions and attempted different ways of doing this, but nothing works so far. 我已经阅读了其他一些SO问题并尝试了不同的方法,但迄今为止没有任何工作。 Gist has commented out code to show some of the methods I've tried. Gist已经注释掉代码以显示我尝试过的一些方法。 I uninstalled Gulp before installing Gulp 4. 我在安装Gulp 4之前卸载了Gulp。

import babel from 'gulp-babel';
import browserSync from 'browser-sync';
import del from 'del';
const server = browserSync.create();

// Process and minify css, copy to dist folder
export function styles() {
  return gulp.src(paths.styles.src)
    .pipe(gulp.dest(paths.styles.dest))
    .pipe(server.reload({stream: true}));
}

// Copy html to dist folder
export function html() {
  return gulp.src(paths.html.src)
    .pipe(gulp.dest(paths.html.dest));
}

// Watch files for changes and reload server
export function watch() {
  gulp.watch(paths.styles.src, styles);
  gulp.watch(paths.html.src, html).on('change', server.reload);
}

// Start serving src folder
export function serve(done) {
  server.init({
    server: {
      baseDir: './src'
    }
  });
  done();
}

// Build dist folder, start server, and watch files
const build = gulp.series(clean, gulp.parallel(styles, html), serve, watch);

// Make build be default task
export default build;

I expected the page to reload/auto-inject when changes were made. 我希望在进行更改时重新加载/自动注入页面。 Terminal output indicates this is happening, but no changes are displayed: 终端输出表明发生了这种情况,但未显示任何更改:

[13:35:57] Starting 'styles'...
[Browsersync] 1 file changed (app.css)
[13:35:57] Finished 'styles' after 8.05 ms
[13:36:03] Starting 'html'...
[13:36:03] Finished 'html' after 6.04 ms
[Browsersync] Reloading Browsers...

Your server.baseDir should be your dist directory: 您的server.baseDir应该是您的dist目录:

export function serve(done) {
    server.init({
        server: {
            baseDir: './dist'
        }
    });
    done();
}

I created a simplified version in this gist . 我在这个要点中创建了一个简化版本。

I have resolved the issue and updated the gist. 我已经解决了这个问题并更新了要点。 There was an issue with my watch function and it seems necessary to have reloading done in a separate function. 我的监视功能存在问题,似乎有必要在单独的功能中完成重新加载。

function reload(done) {
  server.reload();
  done();
}

// Watch files for changes and reload server
export function watch() {
  gulp.watch(paths.styles.src, gulp.series(styles, reload));
  gulp.watch(paths.html.src, gulp.series(html, reload));
}

Gist: https://gist.github.com/flaura42/a22823d97baf889160b2b18ca85dbfb4 要点: https//gist.github.com/flaura42/a22823d97baf889160b2b18ca85dbfb4

Figured out issue after taking another look at this recipe: https://github.com/gulpjs/gulp/blob/master/docs/recipes/minimal-browsersync-setup-with-gulp4.md 再看看这个食谱后想出问题: https//github.com/gulpjs/gulp/blob/master/docs/recipes/minimal-browsersync-setup-with-gulp4.md

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

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