简体   繁体   English

如何从另一个 gulp file.js 导入所有任务

[英]How to import all tasks from another gulp file.js

Is it possible to have one main gulpfile.js from which to call tasks from other gulp files.js?是否有可能有一个主 gulpfile.js 从中调用来自其他 gulp files.js 的任务? Simple "require" of child gulpfile.js into main one doesn't work.将子 gulpfile.js 简单地“要求”到主文件中是行不通的。 I have a platform project which includes several sub projects with separate gulpfiles, so I need a solution to manage all child gulpfiles from within main one我有一个平台项目,其中包括几个带有单独 gulpfiles 的子项目,所以我需要一个解决方案来管理主项目中的所有子 gulpfiles

It is possible to have one main gulpfile.js from which to call tasks from other gulp files.js using the require-dir module.可以有一个主 gulpfile.js 从中使用require-dir模块从其他 gulp files.js 调用任务。 From the projects README use it like this:从项目 README 中使用它是这样的:


Split tasks across multiple files跨多个文件拆分任务

If your gulpfile.js is starting to grow too large, you can split the tasks into separate files by using the require-dir module.如果您的gulpfile.js开始变得太大,您可以使用require-dir模块将任务拆分为单独的文件。

Imagine the following file structure:想象一下以下文件结构:

gulpfile.js
tasks/
├── dev.js
├── release.js
└── test.js

Install the require-dir module:安装require-dir模块:

npm install --save-dev require-dir

Add the following lines to your gulpfile.js file. gulpfile.js添加到您的gulpfile.js文件中。

var requireDir = require('require-dir');
var dir = requireDir('./tasks');

I've create a special gulp-require-tasks module that will help you to split your gulpfile.js into separate smaller task files.我创建了一个特殊的gulp-require-tasks模块,它将帮助您将gulpfile.js拆分为单独的较小任务文件。 Please see the README for example and documentation.请参阅README示例和文档。

Please consider using it and let me know if it works for you!请考虑使用它,让我知道它是否适合您! If you have any suggestions or ideas for improvement, I will gladly accept them.如果您有任何改进建议或想法,我将很乐意接受。

And what if I want to make it future-proof and don't want to install another package for it.如果我想让它面向未来并且不想为它安装另一个包怎么办。

The following works for me with gulp 4, without any extra plugins.以下适用于我的 gulp 4,没有任何额外的插件。

In taskfile.js :taskfile.js

const { src, dest } = require('gulp');

const mytask = function () {
  return src('assets/**/*')
    .pipe(dosomething())
    .pipe(dest('dest');
}

module.exports = {
  mytask
}

In gulpfile.js :gulpfile.js

const { mytask } = require('taskfile.js');

// use in other tasks
gulp.task('manythings', gulp.series(..., mytask, ...));

// or use directly as 'gulp mytask'
module.exports = {
  mytask
}

I would recommend this answer for anyone who doesn't want to include a separate module or refactoring:我会向不想包含单独模块或重构的任何人推荐这个答案:

https://stackoverflow.com/a/5809968/40769 https://stackoverflow.com/a/5809968/40769

var fs = require('fs');

// file is included here:
eval(fs.readFileSync('tools.js')+'');

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

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