gulp-load-plugins没有加载插件

时间:2015-03-26 21:06:47

标签: javascript gulp gulp-load-plugins

gulp-load-plugins未加载任何插件。任何人都可以建议为什么会这样吗?

Node: v0.12.0
NPM: v2.7.3

我的package.json

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

我的gulpfile.js

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

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

gulp.task('default');

3 个答案:

答案 0 :(得分:10)

安装其他gulp插件。

<强> TL;博士

如果那是您的完整package.json,看起来您没有安装其他gulp插件。

让我们说以下是您的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"
  }
}

$ npm install一切,然后......

<强> 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');

答案 1 :(得分:7)

尝试将延迟加载设置为false。

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

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

gulp.task('default');

正如其他人提到的,安装一些插件。

答案 2 :(得分:4)

让我告诉你我拥有什么以及如何做,也许这会有所帮助。

我的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"
  }
}

我如何运行gulp-load-plugins

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

这是一个插件的例子:

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

正如你所看到的,我所有的管道都被称为.pipe($。plugin()),意思是$代表gulp-。如果你有一个名为gulp-name-secondname的插件,你可以这样称它:.pipe($。nameSecondname())。

顶部是我需要gulp-load-plugins我将camelize设置为true。延迟加载仅加载您使用的插件而不是所有插件。

小心使用gulp-load-plugins,因为它会减慢你的任务,例如我运行gulp-webserver,当我使用gulp-load-plugins时,任务在200ms后完成,而如果我正常使用它则完成20ms。因此,不要使用所有内容,与它一起玩,看看你在每项任务中失去了多少性能并确定优先级。