How to use gulp -load-plugins with browser-sync?

I am in my use of Gulp to start sharing tasks with individual files.

I was hoping to use for this gulp-load-plugins, but although my task is running, Browser-Sync does not seem to work / does nothing.

Here is my disabled setting. Not sure where I'm wrong!

gulpfile.js

var gulp = require('gulp'),
    plugins = require('gulp-load-plugins')();

function getTask(task) {
    return require('./gulp/tasks/' + task)(gulp, plugins);
}

gulp.task('browser-sync', getTask('browser-sync'));
gulp.task('bs-reload', getTask('bs-reload'));
gulp.task('scripts', getTask('scripts'));

gulp.task('default', ['browser-sync'], function(){
    gulp.watch('src/js/**/*.js', ['scripts']);
});

scripts.js (Gulp Task)

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

module.exports = function(gulp, plugins) {

    return function() {

        return gulp.src([
            'src/js/plugins/**', 
            'src/js/app.js', 
            '!src/js/{libraries,libraries/**}'
        ])
        .pipe(plugins.plumber({
            errorHandler: function(error) {
            console.log(error.message);
            this.emit('end');
        }}))
        .pipe(plugins.concat('app.js'))
        .pipe(gulp.dest('dist/js/'))
        .pipe(plugins.rename({
            suffix: '.min'
        }))
        .pipe(plugins.uglify({
            preserveComments: 'none'
            //preserveComments: 'some'
        }))
        .pipe(gulp.dest('dist/js/')) // Seems okay up until here...
        .pipe(browserSync.reload({   // ...but this never seems to fire!
            stream: true
        }));

    };

};

You will notice that I need Browser-Sync in my file scripts.js, but this is because plugins.Browser-Sync execution does not work. I read somewhere that this is because Browser-Sync is not really a Gulp plugin.

So, until I get any errors, and Browser-Sync starts ... from now on, my script task works, but it does not start part of BrowserSync.

Any help is much appreciated!

PS, , package.json ( , gulpfile.js).

package.json

{
  "name": "MichaelPumo",
  "version": "1.0.0",
  "description": "A frontend Gulp project for WordPress by Michael Pumo.",
  "main": "gulpfile.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "MichaelPumo",
  "license": "MIT",
  "devDependencies": {
    "browser-sync": "2.6.5",
    "gulp-postcss": "~6.0.0",
    "autoprefixer-core": "~5.2.1",
    "gulp-sourcemaps": "~1.5.2",
    "gulp-concat": "2.5.2",
    "gulp-plumber": "1.0.0",
    "gulp-rename": "1.2.2",
    "gulp-sass": "2.0.4",
    "gulp-uglify": "1.2.0",
    "gulp": "~3.9.0",
    "node-neat": "~1.7.2",
    "gulp-svgmin": "~1.2.0",
    "gulp-image-optimization": "~0.1.3",
    "gulp-load-plugins": "~1.0.0"
  }
}
+4
1

, , , gulp-load-plugins NPM, "gulp-".

, :

gulpfile.js

var gulp = require('gulp'),
    plugins = require('gulp-load-plugins')({
        pattern: '*'
    });

Browser-Sync plugins.browserSync :

browser-sync.js(Gulp )

'use strict';

module.exports = function(gulp, plugins) {

    return function() {

        plugins.browserSync.init({
            port: 8080,
            proxy: {
                target: 'http://localhost:8888/my-website-path/'
            }
        });

    };

};

, - !

+19

Source: https://habr.com/ru/post/1613464/


All Articles