gulp-load-plugins not loading plugins

2019-02-18 06:25发布

问题:

gulp-load-plugins is not loading any plugins. Can anyone suggest why this might be?

Node: v0.12.0
NPM: v2.7.3

My package.json:

{
  "name": "foo",
  "version": "0.0.1",
  "dependencies": {},
  "devDependencies": {
    "gulp": "^3.8.11",
    "gulp-load-plugins": "^0.9.0"
  }
}

My gulpfile.js:

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

console.log(JSON.stringify(plugins)); // {}

gulp.task('default');

回答1:

Install other gulp plugins.

tl;dr

If that is your complete package.json, looks like you have no other gulp plugins installed.

Lets say the following is your package.json:

package.json

{
  "name": "foo",
  "version": "0.0.1",
  "dependencies": {},
  "devDependencies": {
    "gulp": "^3.8.11",
    "gulp-load-plugins": "^0.9.0",
    "gulp-rename": "^1.2.0",
    "gulp-concat": "^2.5.2"
  }
}

You $ npm install everything, then...

gulpfile.js

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

// `plugins.rename` should exist
// `plugins.concat` should exist

console.log(JSON.stringify(plugins));

gulp.task('default');


回答2:

Try setting lazy loading to false.

var gulp = require('gulp');
var plugins= require('gulp-load-plugins')({lazy:false});

console.log(JSON.stringify(plugins));

gulp.task('default');

And as others mentioned, install some plugins.



回答3:

Let me show you what i have and how i do it , maybe that will help.

My package.json :

{
  "dependencies": {
    "gulp": "*",
    "gulp-autoprefixer": "*",
    "gulp-html-validator": "0.0.5",
    "gulp-image-optimization": "^0.1.3",
    "gulp-plumber": "*",
    "gulp-rev-collector": "^0.1.4",
    "gulp-rev-manifest-replace": "0.0.5",
    "gulp-ruby-sass": "*",
    "gulp-sass": "*",
    "gulp-scss-lint": "^0.1.10",
    "gulp-sourcemaps": "*",
    "imagemin-optipng": "^4.2.0",
    "imagemin-pngquant": "^4.0.0",
    "vinyl-paths": "^1.0.0"
  },
  "devDependencies": {
    "del": "^1.1.1",
    "gulp-cached": "^1.0.4",
    "gulp-concat": "^2.5.2",
    "gulp-cssmin": "^0.1.6",
    "gulp-filesize": "0.0.6",
    "gulp-gzip": "^1.0.0",
    "gulp-htmlhint": "0.0.9",
    "gulp-htmlmin": "^1.1.1",
    "gulp-if": "^1.2.5",
    "gulp-imagemin": "^2.2.1",
    "gulp-load-plugins": "^0.8.0",
    "gulp-rename": "^1.2.0",
    "gulp-rev": "^3.0.1",
    "gulp-uglify": "^1.1.0",
    "gulp-useref": "^1.1.1",
    "gulp-webserver": "^0.9.0",
    "run-sequence": "^1.0.2"
  }
}

How i run gulp-load-plugins :

 'use strict';
    var gulp = require('gulp'),
          $ = require('gulp-load-plugins')({
            pattern: ['gulp-*', 'gulp.*'],
            replaceString: /\bgulp[\-.]/,
            lazy: true,
            camelize: true
          }),

And this is an example of a plugin:

 // html optimization 
      gulp.task('htmloptimize', function () {
          return gulp.src(dev.html) 
            .pipe($.htmlmin({
              collapseWhitespace: true
            }))
            .pipe(gulp.dest(dist.dist))
      });

As you can see all my pipes are called .pipe($.plugin()) meaning $ stands for gulp- . If you have a plugin named gulp-name-secondname you call it like this: .pipe($.nameSecondname()) .

Top were i require gulp-load-plugins i have camelize set to true . Lazy loading loads only the plugins you use not all of them .

Careful with gulp-load-plugins because it slows your tasks , for example i run gulp-webserver , when i use it with gulp-load-plugins the task finishes after 200ms versus 20ms if i use it normally. So don't use with everything, play with it see how much performance you lose on each task and prioritize.