我似乎遇到了如何将任务存储在我的变量或其他内容中的问题。我根据给定构建中有多少css文件,js文件和站点动态创建任务。这些是我正在处理的数组:
var styleNames = ['main'];
var scriptNames = ['main', 'head', 'libs'];
var siteNames = ['my-app', 'my-other-app'];
所以,我试图为每个siteNames创建一个main.css和相关的.js文件。
//loop through style array and run function to build the tasks that should run on default
for (var style in styleNames) {
prepareTasks(styleNames[style], "scss");
}
for (var script in scriptNames) {
prepareTasks(scriptNames[script], "js");
}
这些运行:
function prepareTasks(filename, extension) {
//do this in a loop for every site
for (var site in siteNames) {
var taskName = siteNames[site] + '-' + filename + '-' + extension;
buildTasks.push(taskName);
var paths = [];
if (filename === 'main') {
paths = [
'!./src/' + extension + '/**/materialize/components/**',
'./src/' + extension + '/bootstrap/*.' + extension,
'./src/' + extension + '/plugins/*.' + extension,
'./src/' + extension + '/util/*.' + extension,
'!./src/' + extension + '/**/*@*.' + extension,
'./src/' + extension + '/**/*.' + extension,
'./src/modules/**/*.' + extension
]
} else {
paths = './src/' + extension + '/**/*@' + filename + '.' + extension;
}
if (extension === "scss") {
console.log(taskName);
console.log('^^ outside task in sass if')
gulp.task(taskName, function() {
console.log(taskName);
console.log('^^ inside sass task')
setScssTasks(siteNames[site], filename, paths);
});
} else if (extension === "js") {
gulp.task(taskName, function() {
setJSTasks(siteNames[site], filename, paths);
});
}
watchTasks.paths.push(paths);
watchTasks.taskNames.push(taskName);
}
};
在这些运行之后,设置了gulp默认任务:
gulp.task('default', buildTasks);
所以当任务运行' my-app-main-scss'时,console.log会读取任务中不正确的taskName和不正确的siteName [site]。它设置为数组中的最后一项。这是我的整个代码的jsfiddle:https://jsfiddle.net/zrcbzp99/ 这些任务如何设置不正确?
答案 0 :(得分:0)
我认为这里的答案可以帮到你:
creating tasks using a loop [gulp]
在调用/调用gulp任务的功能时,您的For循环siteNames已经完成/到达了最后一个siteName。
尝试这样的事情:
var siteNames = ['my-app', 'my-other-app'];
...
function prepareTasks(filename, extension) {
....
siteNames.forEach(function(siteName) {
var taskName = siteName + '-' + filename + '-' + extension;
....
gulp.task(taskName, function() {
setScssTasks(siteName, filename, paths);
});
});
}