【发布时间】:2017-06-01 12:21:05
【问题描述】:
我有几个繁重的任务,我试图在这些任务之间共享全局变量,但我遇到了问题。
我编写了一些自定义任务,根据构建类型设置正确的输出路径。这似乎设置正确。
// 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 的顶部
有什么想法吗?我在这里做错了吗?
【问题讨论】:
-
我认为这可能与在 grunt.initConfig({}) 之外设置的 global.outputPath 有关,而我正在尝试访问 grunt.initConfig({} )
标签: javascript gruntjs