問題描述
也許我的方法有問題,但我有以下情況:
Perhaps it's something wrong with my approach but I have a following situation:
- 我有一個包含 gulpfile 的
component-a
.它的一項任務(例如構建)構建組件并在 dist 文件夾中創建一個組合的 js 文件 - 我有一個包含 gulpfile 的
component-b
.它的一項任務(例如構建)構建組件并在 dist 文件夾中創建一個組合的 js 文件 - 我有一個使用這兩個組件的項目.這個項目也有一個 gulpfile,我想在其中編寫一個任務:
- 從/components/component-a/gulpfile.js 執行構建任務
- 從/components/component-b/gulpfile.js 執行構建任務
- 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 執行構建任務.是否有可能或者我應該以其他方式處理這種情況?
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<從不同的目錄運行 Gulpfile 非常簡單/code> 模塊.
嘗試根據您的需要調整以下內容:
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
的維護者列入黑名單 因為...
gulp-chug
Although using gulp-chug
is one way to go about this, it has been blacklisted by gulp
's maintainers for being...
執行,太復雜,只是將 gulp 用作 globber"
"execing, too complex and is just using gulp as a globber"
官方黑名單聲明...
沒有理由存在,使用 require-all 模塊或節點的 require"
"no reason for this to exist, use the require-all module or node's require"
這篇關于從一個 gulpfile.js 從另一個 gulpfile.js 運行 gulp 任務的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!