【问题标题】:Gulp browserSync not reloading automaticallyGulp browserSync 不会自动重新加载
【发布时间】:2018-04-29 14:13:37
【问题描述】:

我已经阅读了我能找到的关于这个主题的所有教程、stackoverflow 问题、论坛,但仍然找不到适合我的解决方案。我确定这是我忽略的简单事情,这就是我寻求帮助的原因。这是我的文件夹结构供参考。

  • 分布
    • CSS
    • img
    • js
    • index.html
    • 萨斯
    • img
    • js
    • index.html
  • gulpfile.js
  • package.json

browserSync 将正确初始化并在浏览器中显示页面。但是,当我对 html、css 或 js 文件进行更改时,终端会告诉我它正在重新加载浏览器,但是浏览器永远不会重新加载。如果我手动刷新浏览器,那么更改会正确显示,但 browserSync 应该会自动刷新吧?

我是否需要制作某种返回流或 .pipe(browserSync.stream());在我的复制任务结束时?

var gulp = require("gulp"),
    browserSync = require("browser-sync").create(),
    sass = require("gulp-sass"),
    autoprefixer = require("gulp-autoprefixer"),
    jasmine = require("gulp-jasmine-phantom"),
    concat = require("gulp-concat"),
    uglify = require("gulp-uglify"),
    babel = require("gulp-babel"),
    sourcemaps = require("gulp-sourcemaps"),
    imagemin = require("gulp-imagemin");

gulp.task("default", ["styles", "copy-html", "copy-img", "scripts", "browser-sync", "watch"], function(){
});

gulp.task("watch", ["browser-sync"], function() {

    gulp.watch("src/img/*", ["copy-img", browserSync.reload]);
    gulp.watch("src/index.html", ["copy-html", browserSync.reload]);
    gulp.watch("src/sass/**/*.scss", ["styles", browserSync.reload]);
    gulp.watch("src/js/**/*.js", ["scripts", browserSync.reload]);
});

gulp.task("browser-sync", function(){
    browserSync.init({
        server: {
            baseDir: "dist/"
        }
    });
});

gulp.task("styles", function () {
    return gulp.src("src/sass/**/*.scss")
        .pipe(sass({outputStyle: "compressed"}).on("error", sass.logError))
        .pipe(autoprefixer({
            browsers: ["last 2 versions"],
            cascade: false
        }))
        .pipe(gulp.dest("dist/css"));
});

gulp.task("copy-html", function() {
    return gulp.src("src/index.html")
        .pipe(gulp.dest("./dist"));

});

gulp.task("scripts", function() {
    return gulp.src("src/js/**/*.js")
        .pipe(babel())
        .pipe(sourcemaps.init())
        .pipe(concat("all.js"))
        .pipe(uglify())
        .pipe(sourcemaps.write())
        .pipe(gulp.dest("dist/js"));
});

gulp.task("copy-img", function() {
    return gulp.src("src/img/*")
        .pipe(imagemin())
        .pipe(gulp.dest("dist/img"));
});

gulp.task("tests", function() {
    gulp.src("tests/spec/extraSpec.js")
        .pipe(jasmine({
            integration: true,
            vendor: "src/js/**/*.js"
        }));
});

我还想知道 package.json 文件中的依赖项是否正确。

{
    "title": "Matthew J. Whitney - Portfolio Site",
    "name": "portfolio-site",
    "description": "A smooth scrolling navigation built with Bootstrap",
    "keywords": [
        "css",
        "sass",
        "html",
        "responsive",
        "javascript"
    ],
    "homepage": "https://www.matthewjwhitney.com",
    "author": "Matthew J. Whitney",
    "repository": {
        "type": "git",
        "url": "https://github.com/matthewjwhitney/portfolio-site.git"
    },
    "dependencies": {
        "ajv": "^6.4.0",
        "babel-core": "^6.26.3",
        "bootstrap": "^4.0.0",
        "gulp-clean-css": "^3.9.3",
        "gulp-cssnano": "^2.1.3",
        "gulp-htmlclean": "^2.7.22",
        "gulp-inject": "^4.3.2",
        "gulp-rename": "^1.2.2",
        "gulp-webserver": "^0.9.1",
        "jquery": "3.3.1",
        "jquery.easing": "^1.4.1",
        "phantomjs": "^2.1.7",
        "popper": "^1.0.1",
        "popper.js": "^1.14.3"
    },
    "devDependencies": {
        "browser-sync": "^2.23.6",
        "del": "^3.0.0",
        "eslint": "^4.19.1",
        "eslint-plugin-react": "^7.7.0",
        "gulp": "^3.9.1",
        "gulp-autoprefixer": "^5.0.0",
        "gulp-babel": "^7.0.1",
        "gulp-cache": "^1.0.2",
        "gulp-concat": "^2.6.1",
        "gulp-eslint": "^4.0.2",
        "gulp-imagemin": "^4.1.0",
        "gulp-jasmine-phantom": "^3.0.0",
        "gulp-sass": "^4.0.1",
        "gulp-sourcemaps": "^2.6.4",
        "gulp-uglify": "^3.0.0",
        "gulp-useref": "^3.1.5",
        "run-sequence": "^2.2.1"
    }
}

这里是a link to my GitHub repo,如果有帮助的话。这真的让我发疯,所以我希望有人可以并且会提供帮助。谢谢! :)

【问题讨论】:

  • 你在多个浏览器中试过了吗?
  • @TheodorB,是的,我尝试过使用 chrome canary、chrome 和 safari,结果相同。谢谢。
  • 尝试从开发工具中检查网络选项卡,是否有任何错误?
  • @TheodorB,没有错误。

标签: javascript gulp


【解决方案1】:

我下载了你的 repo 来检查它,你的 Gulp 安装一切正常。

您遇到的问题是您的 index.html 文件格式错误。在文件的最开始,在文件声明之前有一个单词“portfolio”。

我在其他一些答案中读到 BrowserSync 需要 HTML 中的 body 标记来注入一些启用同步的代码,我猜格式错误的 HTML 可能会破坏该功能。

谢谢,

【讨论】:

  • 呃,非常感谢。我正在使用一个 gulp-inject 节点,我认为出于某种原因自动添加了该节点。我没有想到要删除它。我知道这是我忽略的小东西!谢谢你,谢谢你,谢谢你。这让我发疯了,因为我知道我的 gulpfile.js 是正确的!
  • 我遇到了重载问题。当我插入身体标签时,问题立即解决了。谢谢!
  • 哇!这解决了我的一个重新加载问题。由于我遇到了与上述相同的问题,我不明白为什么 BrowserSync 在上述情况下不会抛出错误?
猜你喜欢
  • 1970-01-01
  • 2018-11-30
  • 1970-01-01
  • 2019-10-05
  • 2017-08-01
  • 2015-05-05
  • 1970-01-01
  • 2014-07-28
  • 2016-08-26
相关资源
最近更新 更多