为了将文件复制到目标,我使用简单的 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/