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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25518870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 04:37:59  来源:igfitidea点击:

How to import all tasks from another gulp file.js

javascriptnode.jsgulp

提问by netanalyzer

Is it possible to have one main gulpfile.js from which to call tasks from other gulp files.js? Simple "require" of child gulpfile.js into main one doesn't work. 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

是否有可能有一个主 gulpfile.js 从中调用其他 gulp files.js 的任务?将子 gulpfile.js 简单地“要求”到主文件中是行不通的。我有一个平台项目,其中包括几个带有单独 gulpfiles 的子项目,所以我需要一个解决方案来管理主 gulpfiles 中的所有子 gulpfiles

回答by Brian Leathem

It is possible to have one main gulpfile.js from which to call tasks from other gulp files.js using the require-dirmodule. From the projects README use it like this:

可以有一个主 gulpfile.js,从中可以使用require-dir模块从其他 gulp files.js 调用任务。从项目 README 中,像这样使用它:



Split tasks across multiple files

跨多个文件拆分任务

If your gulpfile.jsis starting to grow too large, you can split the tasks into separate files by using the require-dirmodule.

如果您gulpfile.js开始变得太大,您可以使用require-dir模块将任务拆分为单独的文件。

Imagine the following file structure:

想象一下以下文件结构:

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

Install the require-dirmodule:

安装require-dir模块:

npm install --save-dev require-dir

Add the following lines to your gulpfile.jsfile.

将以下行添加到您的gulpfile.js文件中。

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

回答by Slava Fomin II

I've create a special gulp-require-tasksmodule that will help you to split your gulpfile.jsinto separate smaller task files. Please see the READMEfor example and documentation.

我创建了一个特殊的gulp-require-tasks模块,它将帮助您将您的gulpfile.js任务文件拆分为单独的较小的任务文件。请参阅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.

请考虑使用它,让我知道它是否适合您!如果您有任何改进建议或想法,我将很乐意接受。