将gulp-file分成几个文件

时间:2017-12-27 17:15:26

标签: javascript node.js gulp

我想将一个大的gulp文件分成几个文件 其中一些文件应该包含gulp-tasks,其他文件 - 原始函数,第三文件 - 两者都有 我该怎么办?

我尝试使用一些文件创建目录,其中一些类似:

gulpfile.js
gulp/
├── css_tasks.js
└── js_tasks.js

然后在require-dir中使用gulpfile.js

require('require-dir')('./gulp');

效果很好,但此方法允许使用所需目录中的仅任务

但这还不够。
另外,我想使用其他文件中的原始函数,有些类似:

gulpfile.js
gulp/
├── common_methods.js
├── css_tasks.js
└── js_tasks.js

以这种方式使用它:

/* css_tasks.js: */

gulp.task('some_css_task', function() {
   var foo = someCommonMethod();
   /* task stuff */
})

/* js_tasks.js: */

gulp.task('some_js_task', function() {
   var boo = anotherCommonMethod();
   /* task stuff */
})

那么,我该怎么做呢?

1 个答案:

答案 0 :(得分:0)

根据require-dir模块,它根据您的目录结构返回一个对象。

{
  a: require('./a'),
  b: require('./b')
}

所以我建议您为js& css“gulp-task-files”。所以结构可以是下一个:

gulp/
├── common_methods.js
├── css/index.js
└── js/index.js

然后在你的gulpfile中你应该要求css& js文件夹分开如:

const js = require('require-dir')('./gulp/js');
const css = require('require-dir')('./gulp/css');

它允许您拥有common_methods.js文件,这些文件可以在这些(js& css)任务文件夹和&之间共享。不会通过common_methods.js

导出require-dir

<强> 更新

// common_methods.js
function method1(){}
function method2(){}

module.exports = {
  foo: method1,
  baz: method2
}

// js/index.js
const foo = require('../common_methods').foo;

// css/index.js
const baz = require('../common_methods').baz;

希望它有意义。

相关问题