Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a refresh in browser with gulp

I have an app is in iis, it is an app made in angularjs and webapi C # 2.0, I would like to create a task that updates the browser as soon as I save any js file.

Version of gulp: 3.9.1

gulp.task('livereload', function () {
    gulp.watch(config.files.js);
});
like image 913
Max Ferreira Avatar asked Apr 14 '17 16:04

Max Ferreira


People also ask

Which plugin will be used for live reload in browser in gulp?

BrowserSync is used to watch all HTML and CSS files in the CSS directory and perform live reload to the page in all browsers, whenever files are changed.

What is gulp Livereload?

A lightweight gulp plugin for livereload to be used with the livereload chrome extension or a livereload middleware.

How do I run a gulp file in Visual Studio code?

Run a Gulp Task in Visual Studio CodeType "Run Task" and select it, which will bring up a list of tasks configured in Gulp. Choose the Gulp Task you want to run! Most of your Gulp Tasks will probably be automated using gulp watch, but manual Gulp Tasks can easily be run within Visual Studio Code.

What is Vscode gulp?

Allows you to visualize and execute gulp tasks found in your workspaces. NOTE: If you are using a multi-root workspace and have a custom filters array in your settings, you may need to add a glob prefix (**) to node_modules and bower_components to avoid errors.


1 Answers

gulp-livereload

A lightweight gulp plugin for livereload to be used with the livereload chrome extension or a livereload middleware.

Simple to setup:

var gulp = require('gulp'),
    less = require('gulp-less'),
    livereload = require('gulp-livereload');

gulp.task('less', function() {
  gulp.src('less/*.less')
    .pipe(less())
    .pipe(gulp.dest('dist'))
    .pipe(livereload());
});

gulp.task('watch', function() {
  livereload.listen();
  gulp.watch('less/*.less', ['less']);
});

Browsersync

There's no official Browsersync plugin for Gulp, because it's not needed! You simply require the module, utilise the API and configure it with options.

The new cool kid, most have already moved to it.

Streams are supported in Browsersync, so you can call reload at specific points during your tasks and all browsers will be informed of the changes. Because Browsersync only cares about your CSS when it's finished compiling - make sure you call .stream() after gulp.dest.

var gulp = require('gulp'),
    browserSync = require('browser-sync').create(),
    sass = require('gulp-sass');

// Static Server + watching scss/html files
gulp.task('serve', ['sass'], function() {

    browserSync.init({
        server: "./app"
        // or
        // proxy: 'yourserver.dev'
    });

    gulp.watch("app/scss/*.scss", ['sass']);
    gulp.watch("app/*.html").on('change', browserSync.reload);
});

// Compile sass into CSS & auto-inject into browsers
gulp.task('sass', function() {
    return gulp.src("app/scss/*.scss")
        .pipe(sass())
        .pipe(gulp.dest("app/css"))
        .pipe(browserSync.stream());
});

gulp.task('default', ['serve']);

For a manual reload:

// ...
var reload = browserSync.reload;

// Save a reference to the `reload` method

// Watch scss AND html files, doing different things with each.
gulp.task('serve', function () {

    // Serve files from the root of this project
    browserSync.init({/* ... */});

    gulp.watch("*.html").on("change", reload);
});

Why Browsersync is better?

It is not constrained to a single device, it works across desktop and mobile devices at the same time. It will update code changes, synchronize scroll positions and form inputs automatically across all browsers and devices.

like image 191
Emile Bergeron Avatar answered Oct 05 '22 03:10

Emile Bergeron