简体   繁体   English

使用Gulp和browserSync时重新加载顺序错误

[英]Wrong reload order when using Gulp and browserSync

So I'm trying to compile pug (jade), javascript and sass files with Gulp V4, and using browserSync to reload the browser when any of these files change. 因此,我尝试使用Gulp V4编译pug(jade),javascript和sass文件,并在这些文件中的任何一个更改时使用browserSync重新加载浏览器。 There are a lot of guides out there, but no matter which way I write the code, it just doesn't seem to get the task order right. 那里有很多指南,但是无论我以哪种方式编写代码,似乎都无法正确执行任务顺序。 Im using gulp.series to set the task order, but browserSync doesn't want to play ball. 我使用gulp.series设置任务顺序,但是browserSync不想玩。

Here's a print of my Gulp file: 这是我的Gulp文件的打印件:

var gulp = require('gulp'),
... etc.

var paths = {
    sass: ['src/scss/**/*.scss'],
    js:   ['src/js/**/*.js'],
    pug:  ['src/**/*.pug']
};

// Shared Tasks:
//*------------------------------------*/

gulp.task('clean', function(done){
    del(['dist/assets/**/*.css', 'dist/assets/**/*.map', 'dist/assets/**/*.js']);
    done();
});

// App Specific Tasks:
//*------------------------------------*/

// Sass
gulp.task('build-sass', function(){
    gulp.src(paths.sass)
        return sass(paths.sass, {
            style: 'expanded'
        })
        .on('error', sass.logError)
        .pipe(prefix('last 2 version', '> 1%', 'ie 8', 'ie 9'))
        .pipe(sourcemaps.write())
        .pipe(gulp.dest('dist/assets/css'))
        .pipe(cleanCSS({ advanced: false, keepSpecialComments: 0 }))
        .pipe(rename({suffix: '.min'}))
        .pipe(gulp.dest('dist/assets/css'));
});

// JS
gulp.task('build-js', function(done){
    gulp.src(paths.js)
        .pipe(concat('all.js'))
        .pipe(gulp.dest('dist/assets/js'))
        .pipe(rename({suffix: '.min'}))
        .pipe(uglify())
        .pipe(gulp.dest('dist/assets/js'))
        done();

// Pug
gulp.task('build-pug', function buildHTML(done){
    gulp.src(['src/**/*.pug'], {
        base: 'src'
    })
    .pipe(pug())
    .pipe(gulp.dest('./dist/'))
    done();
});


// Service Tasks:
//*------------------------------------*/

// Delete the compiled views dir.
gulp.task('remove', function(done){
    del(['dist/views/**/']);
    done();
});

// Serve the site locally and watch for file changes
gulp.task('serve', function(done){
    browserSync.init({
        server: {
            baseDir: './dist/',
            notify: false,
            open: false
        }
    });

   // Watch & Reload Pug Files
   gulp.watch([paths.pug], gulp.series('build-pug')).on('change', browserSync.reload);
   // Watch & Reload Sass Files
   gulp.watch([paths.sass], gulp.series('build-sass')).on('change', browserSync.reload);
});


gulp.task('default', gulp.series('clean', 'build-sass', 'build-js', 'build-pug', 'remove', 'serve', function(done){
    done();
}));

And the terminal console log outputs this when starting gulp: 启动gulp时,终端控制台日志将输出以下内容:

$ gulp
[13:09:33] Using gulpfile ~/Dropbox Aaron/Dropbox (Personal)/01_Me/01_Dev/01_My Work/*Template/gulpfile.js
[13:09:33] Starting 'default'...
[13:09:33] Starting 'clean'...
[13:09:33] Finished 'clean' after 2.64 ms
[13:09:33] Starting 'build-sass'...
[13:09:33] Finished 'build-sass' after 443 ms
[13:09:33] Starting 'build-js'...
[13:09:33] Finished 'build-js' after 4.42 ms
[13:09:33] Starting 'build-pug'...
[13:09:33] Finished 'build-pug' after 3.15 ms
[13:09:33] Starting 'remove'...
[13:09:33] Finished 'remove' after 656 μs
[13:09:33] Starting 'serve'...
[BS] Access URLs:
-------------------------------------
      Local: http://localhost:3000
   External: http://10.130.91.51:3000
-------------------------------------
         UI: http://localhost:3001
UI External: http://10.130.91.51:3001
-------------------------------------
[BS] Serving files from: ./dist/

And this when I change a file within the watched files: 当我在监视文件中更改文件时,这是这样的:

[BS] File changed: src/scss/4_elements/_elements.page.scss
[13:12:33] Starting 'build-sass'...
[13:12:34] write ./style.css
[13:12:34] Finished 'build-sass' after 366 ms

So what seems to me to be happening in this instance, the file is changing, browserSync reloads the page, and then 'build-sass' starts. 因此,在我看来,这种情况正在发生,文件正在更改,browserSync重新加载页面,然后启动“ build-sass”。 Which is the wrong way round. 这是错误的方法。 I obviously want to build the sass, then reload the page. 我显然想构建sass,然后重新加载页面。 I have to save 2 times for the browser to refresh the code I want. 我必须保存2次以供浏览器刷新所需的代码。

Some of the code I've used is directly from the browserSync documentation: 我使用的一些代码直接来自browserSync文档:

https://www.browsersync.io/docs/gulp#gulp-reload https://www.browsersync.io/docs/gulp#gulp-reload

This doesn't seem to work. 这似乎不起作用。

Your log output looks right. 您的日志输出看起来正确。 First it runs 'default' and then your series of tasks. 首先它运行“默认”,然后运行您的一系列任务。 Later when you modify a sass file, it starts 'build-sass'. 稍后,当您修改sass文件时,它将启动“ build-sass”。 The only problem is it doesn't reload via browserSync.reload(). 唯一的问题是它不会通过browserSync.reload()重新加载。 So I believe the problem may be here: 所以我相信问题可能在这里:

gulp.watch([paths.sass], gulp.series('build-sass')).on('change', browserSync.reload);

I highly recommend 我强烈推荐

https://github.com/gulpjs/gulp/blob/4.0/docs/recipes/minimal-browsersync-setup-with-gulp4.md https://github.com/gulpjs/gulp/blob/4.0/docs/recipes/minimal-browsersync-setup-with-gulp4.md

that is the gulp4.0 recipe to do exactly what you are trying to do. 这就是gulp4.0配方,可以完全按照您的意图进行。 I am using pretty much what they have there, so if you have problems getting their code to work I can help. 我几乎在使用他们那里拥有的东西,因此,如果您在使他们的代码正常工作方面遇到问题,我可以提供帮助。 Here is my working code: 这是我的工作代码:

function serve (done) {
  browserSync.init({
  server: {
    baseDir: "./",
    index: "home.html"
  },
  ghostMode: false
 });
 done();
}

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

var paths = {
  styles: {
    src: "./scss/*.scss",
    dest: "./css"
  },
  scripts: {
    src: "./js/*.js",
    dest: "./js"
  }
};

function watch() {
  gulp.watch(paths.scripts.src, gulp.series(processJS, reload));
  gulp.watch(paths.styles.src,  gulp.series(sass2css,  reload));
  gulp.watch("./*.html").on("change", browserSync.reload);
}

var build = gulp.series(serve, watch);

gulp.task("sync", build);

function sass2css() {
  return gulp.src("./scss/*.scss")
    .pipe(cached("removing scss cached"))
    // .pipe(sourcemaps.init())
    .pipe(sass().on("error", sass.logError))
    // .pipe(sourcemaps.write("./css/sourceMaps"))
    .pipe(gulp.dest("./css"));
}

function processJS() {
  return gulp.src("./js/*.js")
    .pipe(sourcemaps.init())
    // .pipe(concat("concat.js"))
    // .pipe(gulp.dest("./concats/"))
    // .pipe(rename({ suffix: ".min" }))
    // .pipe(uglify())
    .pipe(sourcemaps.write("./js/sourceMaps/"))
    .pipe(gulp.dest("./js/maps/"));
}

Note the general use of functions instead of 'tasks' and particularly the reload(done) function. 请注意,通常使用函数代替“任务”,尤其是reload(done)函数。 You might try simply adding that to your watch statements like 您可以尝试将其简单地添加到watch语句中,例如

gulp.watch([paths.sass], gulp.series('build-sass', reload);

after you have added the reload function, that might be enough to fix your problem without making all the other changes as in my code. 添加重新加载功能后,可能足以解决您的问题,而无需进行代码中的所有其他更改。 Or you might simply have to add a return statement to your 'build-sass' task so gulp knows that task has completed. 或者,您可能只需要在“ build-sass”任务中添加return语句,以便gulp知道任务已完成。 Good luck. 祝好运。

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

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