如何使用map函数为每个循环生成基于Object.keys的动态任务
首先,我只想定义常量
像
const js = pkg.jscripts,
其变量如
importScss: {
filetype: {
ext: "/**/*.scss",},bootstrap: {
filename: "bootstrap",filepath: "node_modules/bootstrap-sass/assets/stylesheets/bootstrap",dest: pkg.name + "/css/bootstrap"
},bootstrapSelect: {
filename: "bootstrap-select",filepath: "node_modules/bootstrap-select/sass",dest: pkg.name + "/css/bootstrap-select"
},lightgallery: {
filename: "lightgallery",filepath: "node_modules/lightgallery/src/sass",dest: pkg.name + "/css/lightgallery"
},owlCarousel: {
filename: "owl.carousel",filepath: "node_modules/owl.carousel/src/scss",dest: pkg.name + "/css/owl.carousel"
},fontawesome: {
filename: "fontawesome",filepath: "node_modules/@fortawesome/fontawesome-free/scss/",dest: pkg.name + "/css/fontawesome"
}
},
我尝试正确执行任务
var jsTasks = Object.keys(js);
jsTasks.forEach(function (libName) {
//console.log(libName);
gulp.task(libName,function () {
//console.log(js[libName].filepath);
return gulp.src(js[libName].filepath)
//.pipe(plugins.jshint()) // if you want it
//.pipe(plugins.uglify()) // if you like it
.pipe(plugins.concat(js[libName].filename)) // .min.js if you Uglified it
.pipe(header(jsbanner,{ pkg: pkg }))
.pipe(header(banner,{ pkg: pkg }))
.pipe(gulp.dest(js[libName].dest));
});
});
gulp.task('js',gulp.parallel(
jsTasks.map(
function (jscript) {
console.log(jscript);
return jscript;
})
//console.log(jsTasks.map(function (jscript) { return jscript; }))
)
);
它正常工作
但是我想喜欢这样的基于动态任务的Object。带有地图的每个循环的键
function aruntask(keys) {
//console.log(keys)
var myobj = Object.keys(keys);
//console.log(mytask);
myobj.forEach(function (libName) {
//console.log(keys[libName].filepath);
gulp.task(libName,function () {
console.log(keys[libName].filepath);
return gulp.src(keys[libName].filepath)
//.pipe(plugins.jshint()) // if you want it
//.pipe(plugins.uglify()) // if you like it
.pipe(plugins.concat(keys[libName].filename)) // .min.js if you Uglified it
.pipe(header(jsbanner,{ pkg: pkg }))
.pipe(gulp.dest(keys[libName].dest));
});
});
}
gulp.task('js',gulp.parallel(
jsTasks.map(function (jscript) { return jscript; })
)
);