如何在浏览器同步中使用gulp-load-plugins?

Mic*_*umo 4 npm gulp browser-sync gulp-load-plugins

我正处于我的Gulp使用的一个阶段,它开始有意义地将任务分解为单独的文件.

为此,我希望使用gulp-load-plugins,但是虽然我的任务运行,但浏览器同步似乎不会触发/执行任何操作.

这是我的剥离设置.不知道我哪里错了!

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']);
});
Run Code Online (Sandbox Code Playgroud)

scripts.js(Gulp任务)

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
        }));

    };

};
Run Code Online (Sandbox Code Playgroud)

您会注意到我在我的scripts.js文件中需要浏览器同步,但这是因为执行plugins.Browser-Sync无法正常工作.我在某处读到这是因为Browser-Sync实际上并不是Gulp插件.

因此,虽然我没有得到任何错误,浏览器同步似乎启动...从那时起,我的脚本任务工作,但它不会触发BrowserSync部分.

任何帮助非常感谢!

PS,Incase它有帮助,这是我的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"
  }
}
Run Code Online (Sandbox Code Playgroud)

Mic*_*umo 20

对于任何想知道或暗淡的人,请注意默认情况下gulp-load-plugins只能使用带有' gulp-' 前缀的NPM包.

幸运的是,您可以更改此行为并将搜索作为选项传递,如下所示:

gulpfile.js

var gulp = require('gulp'),
    plugins = require('gulp-load-plugins')({
        pattern: '*'
    });
Run Code Online (Sandbox Code Playgroud)

在您的任务中,您现在可以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/'
            }
        });

    };

};
Run Code Online (Sandbox Code Playgroud)

希望这有助于其他人!