Running gulp task from one gulpfile.js from another gulpfile.js

前端 未结 2 1105
耶瑟儿~
耶瑟儿~ 2020-12-16 11:08

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. b
相关标签:
2条回答
  • 2020-12-16 11:22

    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"

    0 讨论(0)
  • 2020-12-16 11:34

    Maybe this would help someone too, so I'll give this a shot. I don't know specifically what the OP intends to do but the question is similar to my problem which my solution is to access the other gulp file in another way like using a project's pre-build event, for example:

    cd $(SolutionDir)\ProjectThatHasTargetGulpFile
    gulp theTaskNeedToRun
    

    Considering that Gulp and NodeJs is properly installed in the machine (can run in cmd).

    0 讨论(0)
提交回复
热议问题