简体   繁体   English

从另一个gulpfile.js中的一个gulpfile.js运行gulp任务

[英]Running gulp task from one gulpfile.js from another gulpfile.js

Perhaps it's something wrong with my approach but I have a following situation: 也许这与我的方法有关,但我有以下情况:

  1. I have a component-a that has a gulpfile. 我有一个component-a -a有一个gulpfile。 One of its tasks (eg. build) builds the component and creates a combined js file in dist folder 其任务之一(例如构建)构建组件并在dist文件夹中创建组合的js文件
  2. I have a component-b that has a gulpfile. 我有一个component-b有一个gulpfile。 One of its tasks (eg. build) builds the component and creates a combined js file in dist folder 其任务之一(例如构建)构建组件并在dist文件夹中创建组合的js文件
  3. I have a project that uses both components. 我有一个使用这两个组件的项目。 This project has a gulpfile as well and in it I would like to write a task that: 这个项目也有一个gulp文件,我想写一个任务:
    • executes build task from /components/component-a/gulpfile.js 从/components/component-a/gulpfile.js执行构建任务
    • executes build task from /components/component-b/gulpfile.js 从/components/component-b/gulpfile.js执行构建任务
    • concats /components/component-a/dist/build.js and /components/component-b/dist/build.js (I know how to do this) concats /components/component-a/dist/build.js和/components/component-b/dist/build.js(我知道怎么做)

What I don't know is how to execute the build task from /components/component-?/gulpfile.js. 我不知道的是如何从/components/component-?/gulpfile.js执行构建任务。 Is it even possible or I should deal with this situation otherwise? 是否有可能或者我应该处理这种情况呢?

require('child_process').spawn; 要求( 'child_process')产卵。;

Running a Gulpfile from a different directory is quite simple with Node's child_process#spawn module . 使用Node的child_process#spawn模块,从不同的目录运行Gulpfile非常简单。

Try adapting the following to your needs: 尝试根据您的需求调整以下内容:

// Use `spawn` to execute shell commands with Node
const { spawn } = require('child_process')
const { join } = require('path')

/*
  Set the working directory of your current process as
  the directory where the target Gulpfile exists.
*/
process.chdir(join('tasks', 'foo'))

// Gulp tasks that will be run.
const tasks = ['js:uglify', 'js:lint']

// Run the `gulp` executable
const child = spawn('gulp', tasks)

// Print output from Gulpfile
child.stdout.on('data', function(data) {
    if (data) console.log(data.toString())
})

gulp-chug 一饮而尽,突突

Although using gulp-chug is one way to go about this, it has been blacklisted by gulp 's maintainers for being... 虽然使用gulp-chug是解决这个问题的一种方法, 但是已经被gulp的维护人员列入黑名单 ,因为......

"execing, too complex and is just using gulp as a globber" “执行,太复杂,只是使用gulp作为一个球体”

The official blacklist states... 官方黑名单声明......

"no reason for this to exist, use the require-all module or node's require" “没有理由存在,请使用require-all模块或节点的要求”

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

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