如何扳手插件对于gulp有用

dip*_*dip 4 gulp

我正在学习吞咽.我是通过以下代码来的.

wrench.readdirSyncRecursive('./gulp').filter(function(file) {
  return (/\.(js|coffee)$/i).test(file);
}).map(function(file) {
  require('./gulp/' + file);
});
Run Code Online (Sandbox Code Playgroud)

有人可以帮助我理解上面代码对什么有用吗?

Slo*_*pez 5

这将加载gulp目录中的所有js或coffee文件以加载所有gulp任务,因此你不必手动导入新的gulp任务,只需在'/ gulp'目录中创建whatevergulptask.js,你就可以从命令中使用它线.

这样做的另一个好处是,你没有一个包含数百万个任务和代码行的庞大的gulpfile.js,因为你的每个TASK都有一个whatevergulptask.js,这只是一个很好的练习,因为gulpfile增长得非常快

示例gulpfile.js

 /**
 *  Welcome to your gulpfile!
 *  The gulp tasks are splitted in several files in the gulp directory
 *  because putting all here was really too long
 */

'use strict';

var gulp = require('gulp');
var wrench = require('wrench');

/**
 *  This will load all js or coffee files in the gulp directory
 *  in order to load all gulp tasks
 */

wrench.readdirSyncRecursive('./gulp').filter(function (file) {
  return (/\.(js|coffee)$/i).test(file);
}).map(function (file) {
  require('./gulp/' + file);
});


/**
 *  Default task clean temporaries directories and launch the
 *  main optimization build task
 */

gulp.task('default', ['clean'], function () {
  gulp.start('build');
});
Run Code Online (Sandbox Code Playgroud)

你的文件结构

gulp/

    build.js
    whatevergulptask.js
    ...
Run Code Online (Sandbox Code Playgroud)