简体   繁体   中英

BrowserSync does not automatically refresh the page with gulp-jade

I recently created a workspace with gulp. Everything was working normally (auto-reload, sass compilation, minification, and more) You can see the code on github

Today I wanted to add jade to the project. The compilation works well but browserSync does not refresh the page automatically

Here is my code

'use strict'

var gulp = require('gulp')
var jade = require('gulp-jade')
var sass = require('gulp-sass')
var cssmin = require('gulp-cssmin')
var rename = require('gulp-rename')
var prefix = require('gulp-autoprefixer')
var uglify = require('gulp-uglify')
var concat = require('gulp-concat')
var imagemin = require('gulp-imagemin')
var browserSync = require('browser-sync').create()

// Compile Jade
gulp.task('jade', function () {
  gulp.src('src/*.jade')
    .pipe(jade({
      pretty: true
    }))
    .pipe(gulp.dest('./dist'))
})

// Configure CSS tasks.
gulp.task('sass', function () {
  gulp.src('src/scss/**/*.scss')
    .pipe(sass.sync().on('error', sass.logError))
    .pipe(prefix('last 2 versions'))
    .pipe(cssmin())
    .pipe(concat('styles.css'))
    .pipe(rename({suffix: '.min'}))
    .pipe(gulp.dest('dist/css'))
    .pipe(browserSync.stream())
})

// Configure JS.
gulp.task('js', function () {
  gulp.src('src/js/**/*.js')
    .pipe(uglify())
    .pipe(concat('app.js'))
    .pipe(rename({suffix: '.min'}))
    .pipe(gulp.dest('dist/js'))
    .pipe(browserSync.stream())
})

// Configure image stuff.
gulp.task('images', function () {
  gulp.src('src/img/**/*.+(png|jpg|gif|svg)')
    .pipe(imagemin())
    .pipe(gulp.dest('dist/img'))
    .pipe(browserSync.stream())
})

gulp.task('default', ['jade', 'sass', 'js', 'images'], function () {
  browserSync.init({
    server: './dist'
  })

  gulp.watch('src/*.jade', ['jade'])
  gulp.watch('src/scss/**/*.scss', ['sass'])
  gulp.watch('src/js/**/*.js', ['js'])
  gulp.watch('./dist/*.html').on('change', browserSync.reload)
})

I tested the code and found the error. Browsersync gets injected into <head> element, therefore you need to have that element on your page (still its good to have html/head/body). Implementing my suggestion from comment (about missing browserSync.stream()) and fixing your jade file to:

html
  head
  body
    h1 hello Jade

    ul
      li comment ça va
      li et toi ?

    .box salut comment tu vas ?

Solves the problem. BrowserSync now correctly gets inserted into the page :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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