简体   繁体   English

带有gulp-load-plugins的Gulp入门套件

[英]Gulp starter kit with gulp-load-plugins

I have a gulp starter kit for my project, however, I want to use gulp-load-plugins to for devDependencies of package.json file. 我的项目有一个gulp入门工具包 ,但是,我想使用gulp-load-plugins来打包package.json文件的devDependencies。 My file structure is 我的文件结构是

ProjectName
  Gulp
   -Tasks
     -broswerify.js
     -browserSync.js
     -jade.js
     -lint.js
  Gulpfile.js
  config.json
  package.json

Gulpfile.js Gulpfile.js

var requireDir = require('require-dir');
var dir        = requireDir('./gulp/tasks', {recurse: true});

jade.js (Which is working as expected using gulp-load-plugins) jade.js (使用gulp -load-plugins可以按预期工作)

var gulp    = require('gulp');
var config  = require('../../config.json');
var plugins = require('gulp-load-plugins')();

gulp.task('jade', function(){
    return gulp.src(config.jade.src)
    .pipe(plugins.jade())
    .pipe(gulp.dest(config.jade.build))
});

browsersync.js (which is not working using gulp-load-plugins) browsersync.js (使用gulp-load-plugins无法正常工作)

var gulp    = require('gulp');
var config  = require('../../config.json').browsersync;
var plugins = require('browsersync'); // works
//var plugins = require('gulp-load-plugins')(); // it doesn't works.

gulp.task('browsersync', function () {
   plugins.browserSync.init(config); // (browsersync required)
  //plugins.browserSync.init(config) it doesn't work. (gulp-load-plugins required)
});

I would like to know that if there is a better way to do that? 我想知道是否有更好的方法?

如果每个插件都有单独的文件,为什么不使用gulp-load-plugins?

This is how i load gulp-load-plugins : 这就是我加载gulp-load-plugins的方式:

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

Here is an example of a revision plugin: 这是一个修订插件的示例:

 // revision styles 
  gulp.task('rev-styles', function () {
    return gulp.src(dev.css)
      .pipe($.rev())
      .pipe($.cssmin())
      .pipe(gulp.dest(dist.css))
      .pipe($.filesize())
      .pipe($.rev.manifest({merge: true}))
      .pipe(gulp.dest('./'))
      //rev replace
      .on('end', function() {
      return gulp.src(['./rev-manifest.json', 'dist/*.html'])
        .pipe($.revCollector({
          replaceReved: true,
          dirReplacements: {
            'css': 'css'
          }
        }))
      .pipe(gulp.dest(dist.dist)) 
    });
  });

As you can see all my pipes are called .pipe($.pluginName()) meaning $ stands for gulp- . 如您所见,我所有的管道都称为.pipe($.pluginName())这意味着$代表gulp-。 If you have a plugin named gulp-name-secondname you call it like this: .pipe($.nameSecondname()) . 如果您有一个名为gulp-name-secondname的插件,则可以这样命名: .pipe($.nameSecondname())

Top were i require gulp-load-plugins i have camelize set to true . 我需要将gulpize设置为true的gulp-load-plugins设置为页首。 Lazy loading loads only the plugins you use not all of them . 延迟加载仅加载您使用的全部插件。

As a side note i strongly recommend not separating plugins in diffrent files but you can modulate them, meaning separating important tasks in separate files like compilation file , optimization file , build file, etc . 作为一个侧面说明,我强烈建议不要在不同的文件中分离插件,但是可以对其进行调制,这意味着将重要的任务分离在单独的文件中,例如编译文件,优化文件,构建文件等。

This might help you understand gulp file separation better http://macr.ae/article/splitting-gulpfile-multiple-files.html 这可能有助于您更好地了解gulp文件分离http://macr.ae/article/splitting-gulpfile-multiple-files.html

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. 小心gulp-load-plugins,因为它会减慢您的任务速度,例如,我运行gulp-webserver,当我将其与gulp-load-plugins一起使用时,任务会在200毫秒后完成,而我通常使用20毫秒即可完成。 So don't use with everything, play with it see how much performance you lose on each task and prioritize. 因此,不要将所有内容都使用起来,而是玩弄它,看看您在每个任务上损失多少性能并确定优先级。

I have used gulp-load-plugins but found that it mainly adds complexity and obscures my code. 我使用了gulp-load-plugins但发现它主要增加了复杂性并使我的代码模糊不清。 At also makes it harder to understand for people less familiar with Gulp. 对于不那么了解Gulp的人来说,at也使理解变得更加困难。 It looks cleaner and easier to understand to have all modules explicitly declared at the top. 在顶部显式声明所有模块看起来更简洁易懂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM