【问题标题】:Browsersync reloads the page before CSS injectionBrowsersync 在 CSS 注入之前重新加载页面
【发布时间】:2017-08-29 11:18:52
【问题描述】:

我正在使用GulpBrowsersyncSass 用于 CSS 和 Pug 用于 HTML 实现构建。在样式和标记编译完成后,我还将样式表 <link>s 注入 HTML。

初始构建工作正常,但我在使用 Gulp 实现监视 CSS 和 HTML 文件的更改时遇到了问题。现在发生的事情是在更改任何源文件(标记或样式表)时,当 Browsersync 重新加载浏览器时,我留下了一个未设置样式的页面,其中没有注入样式表 <head>。似乎 Browsersync 重新加载在 pug(Pug 到 HTML 编译)完成之后触发,但在 sass(Sass 到 CSS 编译)完成之前,所以没有什么可以注入。

主要问题:请告诉我如何在 Gulp 中强制执行正确的顺序。

额外问题:如何在标记更改时重新加载整页,但在样式表更改时不重新加载整页,而仅将 CSS 注入 <head> 为在“SASS + CSS 注入”下的this articleGulp docs 中进行了描述。

这是我的代码:

gulpfile.babel.js

import gulp from "gulp";

// Gulpfile is split across multiple files located in .gulp/.
// Importing them executes them and registers appropriate tasks.
import "./gulp/browser-sync.js";
import "./gulp/inject.js";
import "./gulp/markup.js";
import "./gulp/styles.js";
import "./gulp/watch.js";


// Build all the source files.
gulp.task("build", ["markup", "styles"]);

// Default task ran when `gulp` executed with no arguments.
gulp.task("default", ["build", "browser-sync"]);

gulp/browser-sync.js

import gulp from "gulp";
import browserSync from "browser-sync";

import paths from "./paths.js";


// Running a local server, which posts live updates to connected browsers when
// any of the source files change.
gulp.task("browser-sync", ["build", "watch"], () => {
  browserSync.init({
    server: {
      baseDir: paths.global.tmp
    }
  });
});

gulp/styles.js

import gulp from "gulp";
import sass from "gulp-sass";
import sassModuleImporter from "sass-module-importer";
import sourcemaps from "gulp-sourcemaps";
import browserSync from "browser-sync";
import del from "del";

import paths from "./paths.js";


gulp.task("styles", ["clean:styles", "sass", "inject"]);


// Compiles Sass stylesheets to CSS.
gulp.task("sass", ["clean:styles"], () => {
  const SASS_OPTIONS = {
    includePaths: paths.styles.src,
    importer: sassModuleImporter()
  };

  return gulp.src(paths.styles.srcMainGlob, { base: paths.styles.src })
             .pipe(sourcemaps.init())
             .pipe(sass(SASS_OPTIONS))
             .pipe(sourcemaps.write(paths.styles.maps))
             .pipe(gulp.dest(paths.styles.tmp))
             .pipe(browserSync.stream({ match: "**/*.css" }));
});


gulp.task("clean:styles", () => {
  let pathsToDel = [paths.styles.tmp, paths.styles.dist];
  return del(pathsToDel);
});


gulp.task("watch:styles", ["styles"], () => {
  gulp.watch(paths.styles.srcAllGlob, ["styles"]);
});

gulp/markup.js

import gulp from "gulp";
import pug from "gulp-pug";
import browserSync from "browser-sync";
import del from "del";

import paths from "./paths.js";


gulp.task("markup", ["clean:markup", "pug", "inject"]);


// Compile Pug templates to HTML
gulp.task("pug", ["clean:markup"], () => {
  return gulp.src(paths.markup.srcMainGlob)
             .pipe(pug())
             .pipe(gulp.dest(paths.markup.tmp))
             .pipe(browserSync.stream());
});


gulp.task("clean:markup", () => {
  let pathsToDel = [paths.markup.tmp, paths.markup.dist];
  return del(pathsToDel);
});


gulp.task("watch:markup", ["markup"], () => {
  gulp.watch(paths.markup.srcAllGlob, ["markup"]);
});

gulp/inject.js

import gulp from "gulp";
import inject from "gulp-inject";

import paths from "./paths.js";


gulp.task("inject", ["inject:styles"]);


// Inject generated stylesheets into HTML files.
gulp.task("inject:styles", ["pug", "sass"], () => {
  let sources = gulp.src(paths.styles.tmpGlob, { read: false });

  return gulp.src(paths.markup.tmpGlob)
             .pipe(inject(sources, { relative: true }))
             .pipe(gulp.dest(paths.markup.tmp));
});

gulp/watch.js

import gulp from "gulp";


// Watching for all the changes in source files.
gulp.task("watch", ["watch:markup", "watch:styles"]);

这是运行gulp 并对其中一个源 Sass 文件进行更改后的控制台输出:

[BS] Access URLs:
 --------------------------------------
       Local: http://localhost:3000
    External: http://192.168.88.32:3000
 --------------------------------------
          UI: http://localhost:3001
 UI External: http://192.168.88.32:3001
 --------------------------------------
[BS] Serving files from: .tmp/
[12:00:30] Starting 'clean:styles'...
[12:00:30] Starting 'clean:markup'...
[12:00:30] Finished 'clean:markup' after 3.12 ms
[12:00:30] Starting 'pug'...
[12:00:30] Finished 'clean:styles' after 8.61 ms
[12:00:30] Starting 'sass'...
[BS] Reloading Browsers...
[BS] 1 file changed (index.html)
[12:00:30] Finished 'pug' after 72 ms
[BS] 1 file changed (main.css)
[12:00:30] Finished 'sass' after 144 ms
[12:00:30] Starting 'inject:styles'...
[12:00:30] gulp-inject 1 files into index.html.
[12:00:30] Finished 'inject:styles' after 9.67 ms
[12:00:30] Starting 'inject'...
[12:00:30] Finished 'inject' after 2.99 μs
[12:00:30] Starting 'styles'...
[12:00:30] Finished 'styles' after 2.47 μs

【问题讨论】:

    标签: gulp browser-sync gulp-inject gulp-browser-sync


    【解决方案1】:

    首先要做的是将browserSync添加到你的inject:styles任务中,否则样式链接注入后它不会重新加载

    // Inject generated stylesheets into HTML files.
    gulp.task("inject:styles", ["pug", "sass"], () => {
      let sources = gulp.src(paths.styles.tmpGlob, { read: false });
    
      return gulp.src(paths.markup.tmpGlob)
             .pipe(inject(sources, { relative: true }))
             .pipe(gulp.dest(paths.markup.tmp))
             .pipe(browserSync.stream());
    });
    

    【讨论】:

    • 是的。昨天我什至自己添加了这个。我认为这回答了我的主要问题。
    • 关于您的额外问题,我认为 browserSync 应该在您通过管道传输到 browserSync.stream() 时自动处理注入。它不应该重新加载整个页面吗?
    • 根据我的经验,browserSync.stream() 只有在 sass 任务中通过管道传输 Sass 文件时才能正常注入。所以我最终在sassinject:styles任务中运行browserSync.stream()watch:styles仅在添加或删除某些文件时运行iinject:styles,否则sass运行(当文件更改时)。跨度>
    猜你喜欢
    • 1970-01-01
    • 2018-05-24
    • 1970-01-01
    • 1970-01-01
    • 2019-10-05
    • 2018-11-30
    • 2015-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多