Ich habe ein paar Grunt-Aufgaben, und ich versuche, globale Variablen über diese Aufgaben hinweg zu teilen, und ich stoße auf Probleme.Verwenden Sie die globale Variable zum Festlegen des Build-Ausgabepfads in Grunt
Ich habe einige benutzerdefinierte Aufgaben geschrieben, die den richtigen Ausgabepfad je nach Buildtyp festlegen. Dies scheint die Dinge richtig einzustellen.
// 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);
});
Also, ich versuche, dann zu verweisen global.outputPath in einer nachfolgenden Aufgabe, und läuft in Fehler.
Wenn ich grunt test
über die Befehlszeile aufrufen, gibt es den richtigen Pfad kein Problem.
Allerdings, wenn ich eine Aufgabe, wie dieses: sauber: { release: { src: global.outputPath } }
Es wird folgende Fehler wirft: Warning: Cannot call method 'indexOf' of undefined Use --force to continue.
Auch Meine Konstanten in der SetOutput Aufgabe werden oben auf meiner Gruntfile.js gesetzt
Irgendwelche Gedanken? Mache ich hier etwas falsch?
Ich denke, es könnte mit dem global.outputPath im Zusammenhang stehen außerhalb von grunt.initConfig ({}), während ich versuche, auf die Variable in grunt.initConfig zugreifen ({}) – ThePuzzleMaster