javascript - Gulp 根据对象数组中的键运行任务

标签 javascript gulp

为了将文件复制到目标,我使用简单的 gulp 的 src 和 dest

我想根据 obj 中的键指定此复制操作:

    var copy = {
            first: {
                dest: 'dist/index.scala.html',
                src: 'app/index.scala.html'
            },
            second: {
                dest: 'dist/setup.scala.html',
                src: 'app/setup.scala.html'
            }
      };

我能够创建复制任务并根据对象中提到的 src 和 dest 复制文件,但我需要类似的东西:

    gulp copy:first //this will only copy from src to dest as specifided under 'first' key

    gulp copy:second //this will only copy from src to dest as specifided under 'second' key

就像我们如何在 grunt 中实现目标一样。

最佳答案

根据this article “不可能在命令行上传递可供该任务使用的参数”。

也就是说,你可以这样做:

const gulp = require("gulp");

const copy = {
    first: {
        dest: 'dist/index.scala.html',
        src: 'app/index.scala.html'
    },
    second: {
        dest: 'dist/setup.scala.html',
        src: 'app/setup.scala.html'
    }
};

for (let key in copy) {
    gulp.task('copy:' + key, cb => {
        console.log('copy[key]: ', copy[key]);
        cb();
    });
}

注意:我已将 copy = [...] 更改为 copy = {...} 因为数组不能将字符串(例如“first”)作为键,但对象可以。

然后运行 ​​gulp 命令:

gulp 复制:首先

gulp 复制:第二个

您可能还想查看Pass Parameter to Gulp Task .

关于javascript - Gulp 根据对象数组中的键运行任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58370972/

相关文章:

Javascript计算时间,包括过夜

javascript - templateUrl 中的相对 url 与 Angular 4 和 sails 不工作

gulp - 每次我运行 gulp 任何东西时,我都会收到断言错误。 - 必须指定任务功能

javascript - 将数据存储在 "blog"风格的系统中

javascript - 避免在 iframe 中重复使用 Javascript

javascript - 如何使用 gulp 忽略文件?

javascript - require 未使用 gulp-browserify 定义

javascript - 无法获取 - 使用 Ionic 刷新后出现错误

node.js - 来自 gulp 的 API post 请求未成功

javascript - angularjs 无法使用 http 连接 php