0
我似乎有任務如何被存儲在我的變量或什麼的問題。我基於在給定的構建中有多少個css文件,js文件和網站,動態創建任務。這些都是陣列IM處理:爲什麼我的Gulp構建任務沒有正確設置?
var styleNames = ['main'];
var scriptNames = ['main', 'head', 'libs'];
var siteNames = ['my-app', 'my-other-app'];
所以,我想爲每個站點名一的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.task('default', buildTasks);
所以當任務運行「我的應用程序內 - 主 - SCSS」,在執行console.log讀取裏面的任務不正確的TASKNAME和不正確的siteName [site]。它被設置爲數組中的最後一項。這裏是我的整個代碼jsfiddle:https://jsfiddle.net/zrcbzp99/ 這些任務是如何不正確設置?
這工作!儘管如此,我仍然不完全瞭解它是如何成爲一種競爭條件。根據我的理解,for循環在siteNames上運行,用特定的siteNames [site](例如:my-app)設置單個任務(例如:my-app-main-scss),然後運行默認構建任務已經設定。我沒有看到任務設置中的siteNames [site]或taskName引用如何丟失/覆蓋。這讀也可能幫助我或某人https://github.com/gulpjs/gulp/issues/1581。但這工作:) – lscmaro