問題描述
也許我的方法有問題,但我有以下情況:
Perhaps it's something wrong with my approach but I have a following situation:
- 我有一個(gè)包含 gulpfile 的
component-a
.它的一項(xiàng)任務(wù)(例如構(gòu)建)構(gòu)建組件并在 dist 文件夾中創(chuàng)建一個(gè)組合的 js 文件 - 我有一個(gè)包含 gulpfile 的
component-b
.它的一項(xiàng)任務(wù)(例如構(gòu)建)構(gòu)建組件并在 dist 文件夾中創(chuàng)建一個(gè)組合的 js 文件 - 我有一個(gè)使用這兩個(gè)組件的項(xiàng)目.這個(gè)項(xiàng)目也有一個(gè) gulpfile,我想在其中編寫一個(gè)任務(wù):
- 從/components/component-a/gulpfile.js 執(zhí)行構(gòu)建任務(wù)
- 從/components/component-b/gulpfile.js 執(zhí)行構(gòu)建任務(wù)
- concats/components/component-a/dist/build.js 和/components/component-b/dist/build.js(我知道怎么做)
- I have a
component-a
that has a gulpfile. One of its tasks (eg. build) builds the component and creates a combined js file in dist folder - I have a
component-b
that has a gulpfile. One of its tasks (eg. build) builds the component and creates a combined js file in dist folder - 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:
- executes build task from /components/component-a/gulpfile.js
- executes build task from /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)
我不知道如何從/components/component-?/gulpfile.js 執(zhí)行構(gòu)建任務(wù).是否有可能或者我應(yīng)該以其他方式處理這種情況?
What I don't know is how to execute the build task from /components/component-?/gulpfile.js. Is it even possible or I should deal with this situation otherwise?
推薦答案
require('child_process').spawn;
使用 Node 的 child_process#spawn<從不同的目錄運(yùn)行 Gulpfile 非常簡(jiǎn)單/code> 模塊.
嘗試根據(jù)您的需要調(diào)整以下內(nèi)容:
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
是解決此問題的一種方法,但 它已被 gulp
的維護(hù)者列入黑名單 因?yàn)?..
gulp-chug
Although using gulp-chug
is one way to go about this, it has been blacklisted by gulp
's maintainers for being...
執(zhí)行,太復(fù)雜,只是將 gulp 用作 globber"
"execing, too complex and is just using gulp as a globber"
官方黑名單聲明...
沒有理由存在,使用 require-all 模塊或節(jié)點(diǎn)的 require"
"no reason for this to exist, use the require-all module or node's require"
這篇關(guān)于從一個(gè) gulpfile.js 從另一個(gè) gulpfile.js 運(yùn)行 gulp 任務(wù)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!