我有幾個grunt任務,我試圖在這些任務之間共享全局變量,並且遇到了問題。使用全局變量在Grunt中設置生成輸出路徑
我寫了一些自定義任務,根據構建類型設置正確的輸出路徑。這似乎是正確設置的東西。
// Set Mode (local or build)
grunt.registerTask("setBuildType", "Set the build type. Either build or local", function (val) {
// grunt.log.writeln(val + " :setBuildType val");
global.buildType = val;
});
// SetOutput location
grunt.registerTask("setOutput", "Set the output folder for the build.", function() {
if (global.buildType === "tfs") {
global.outputPath = MACHINE_PATH;
}
if (global.buildType === "local") {
global.outputPath = LOCAL_PATH;
}
if (global.buildType === "release") {
global.outputPath = RELEASE_PATH;
}
if (grunt.option("target")) {
global.outputPath = grunt.option("target");
}
grunt.log.writeln("Output folder: " + global.outputPath);
});
grunt.registerTask("globalReadout", function() {
grunt.log.writeln(global.outputPath);
});
所以,我試圖然後引用global.outputPath在後續任務,並運行到錯誤。
如果我從命令行調用grunt test
,它會輸出正確的路徑沒有問題。
但是,如果我有這樣的任務: 清潔:{ 版本:{ 源:global.outputPath }}
它引發以下錯誤: Warning: Cannot call method 'indexOf' of undefined Use --force to continue.
而且,我的常量在setOutput任務設置在我的Gruntfile.js頂部
有什麼想法?我在這裏做錯了什麼?
我想這可能與global.outputPath被設置在grunt.initConfig({})之外,而我試圖訪問grunt.initConfig ({}) – ThePuzzleMaster 2013-03-26 21:19:04