Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
363 views
in Technique[技术] by (71.8m points)

javascript - 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. One of its tasks (eg. build) builds the component and creates a combined js file in dist folder
  2. 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
  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:
    • 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)

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

require('child_process').spawn;

Running a Gulpfile from a different directory is quite simple with Node's child_process#spawn module.

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...

"execing, too complex and is just using gulp as a globber"

The official blacklist states...

"no reason for this to exist, use the require-all module or node's require"


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...