簡體   English   中英

為什么gulp瀏覽器同步在加載時顯示空白頁?

[英]Why is gulp browser-sync displaying a blank page on load?

我試圖使用瀏覽器同步來建立一個gulp項目,並且一切正常,除了運行默認任務時,它最初在瀏覽器中顯示空白頁面。 如果我在瀏覽器中單擊“刷新”或對css / js / html文件進行了任何編輯,則瀏覽器將重新加載並正確顯示所有內容。

誰能告訴我我想念的東西嗎?

var gulp = require('gulp');
var sass = require('gulp-sass');
var browserSync = require('browser-sync').create();
var useref = require('gulp-useref');
var uglify = require('gulp-uglify');
var gulpIf = require('gulp-if');
var cssnano = require('gulp-cssnano');
var del = require('del');
var runSequence = require('run-sequence');
var fileinclude = require('gulp-file-include');
var notify = require('gulp-notify');
var concat = require('gulp-concat');
var rename = require('gulp-rename');

gulp.task('sass', function() {
  return gulp.src('app/scss/**/*.scss') // Gets all files ending with .scss in app/scss
    .pipe(concat('styles.scss'))
    .pipe(sass())
    .pipe(cssnano())
    .pipe(rename('styles.min.css'))
    .pipe(gulp.dest('dist/css'))
    .pipe(browserSync.reload({
      stream: true
    }))
    .pipe( notify({ message: "sass tasks have been completed!"}) );
});

// Concatenate & Minify JS
gulp.task('scripts', function() {
    return gulp.src('app/js/**/*.js')
        .pipe(concat('js/main.js'))
        .pipe(gulp.dest('dist'))
        .pipe(rename('main.min.js'))
        .pipe(uglify())
        .pipe(gulp.dest('dist/js'))
        .pipe(browserSync.reload({
          stream: true
        }))
        .pipe( notify({ message: "scripts tasks have been completed!"}) );
});

gulp.task('browserSync', function() {
  browserSync.init({
    server: {
      baseDir: 'dist'
    },
  })
});

gulp.task('fileinclude', function() {
  gulp.src(['app/**/*.html'])
  .pipe(fileinclude())
    .pipe(useref())
    .pipe(gulp.dest('dist'))
    .pipe(browserSync.reload({
      stream: true
    }))
    .pipe( notify({ message: "fileInclude tasks have been completed!"}) );
});

gulp.task('watch', ['browserSync', 'sass', 'scripts', 'fileinclude'], function (){
  gulp.watch('app/scss/**/*.scss', ['sass']);
  gulp.watch('app/**/*.html', ['fileinclude']);
  gulp.watch('app/js/**/*.js', ['scripts']);
});

gulp.task('useref', function(){
  return gulp.src('app/*.html')
    .pipe(useref())
    .pipe(gulp.dest('dist'))
    .pipe( notify({ message: "useref tasks have been completed!"}) );
});

gulp.task('clean:dist', function() {
  return del.sync('dist/**/*');
})


gulp.task('default', function (callback) {
  runSequence(['clean:dist', 'sass', 'scripts', 'fileinclude', 'useref', 'browserSync', 'watch'],
    callback
  )
})

gulp.task('build', function (callback) {
  runSequence(
    'clean:dist',
    'sass',
    'scripts',
    'fileinclude',
    ['useref'],
    callback
  )
})

我可以解決這個問題。 只需編輯您的主機文件(/ etc / hosts)並添加新的域引用即可。 121.0.0.1 myvirualdomain.localhost,其中myvirtualdomain.localhost是您嘗試將瀏覽器同步代碼指向的代理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM