【问题标题】:Running gulp task from one gulpfile.js from another gulpfile.js从一个 gulpfile.js 从另一个 gulpfile.js 运行 gulp 任务
【发布时间】:2014-07-03 02:28:25
【问题描述】:

也许我的方法有问题,但我有以下情况:

  1. 我有一个component-a,它有一个 gulpfile。它的一项任务(例如构建)构建组件并在 dist 文件夹中创建一个组合的 js 文件
  2. 我有一个component-b,它有一个 gulpfile。它的一项任务(例如构建)构建组件并在 dist 文件夹中创建一个组合的 js 文件
  3. 我有一个使用这两个组件的项目。这个项目也有一个 gulpfile,我想在其中编写一个任务:
    • 从 /components/component-a/gulpfile.js 执行构建任务
    • 从 /components/component-b/gulpfile.js 执行构建任务
    • concats /components/component-a/dist/build.js 和 /components/component-b/dist/build.js(我知道怎么做)

我不知道如何从 /components/component-?/gulpfile.js 执行构建任务。是否有可能或者我应该以其他方式处理这种情况?

【问题讨论】:

  • 您能否接受 Walter Roman 的回答,因为这是正确且有效的?

标签: javascript node.js gulp


【解决方案1】:

require('child_process').spawn;

使用Node's child_process#spawn module 从不同目录运行 Gulpfile 非常简单。

尝试根据您的需要调整以下内容:

// 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 是解决此问题的一种方法,但it has been blacklisted by gulp's maintainers 是...

“执行,太复杂,只是将 gulp 用作 globber”

official blacklist 状态...

“没有理由存在,使用 require-all 模块或节点的 require”

【讨论】:

    【解决方案2】:

    也许这对某人也有帮助,所以我会试一试。我不知道 OP 具​​体打算做什么,但问题与我的问题类似,我的解决方案是以另一种方式访问​​另一个 gulp 文件,例如使用项目的预构建事件,例如:

    cd $(SolutionDir)\ProjectThatHasTargetGulpFile
    gulp theTaskNeedToRun
    

    考虑到 Gulp 和 NodeJs 已正确安装在机器中(可以在 cmd 中运行)。

    【讨论】:

      猜你喜欢
      • 2016-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-08
      • 1970-01-01
      • 2017-10-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多