使用全局变量在Grunt中设置生成输出路径 [英] Use Global Variable to Set Build Output Path in Grunt

查看:397
本文介绍了使用全局变量在Grunt中设置生成输出路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些烦人的任务,我试图在这些任务中共享全局变量,并且遇到了问题。



我写了一些自定义任务,根据构建类型设置适当的输出路径。

  //设置模式(本地或构建)
grunt.registerTask(函数(val){
// grunt.log.writeln(val +:setBuildType val);
global.buildType = valsetBuildType,设置构建类型。 ;
});

// 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; $ b (grunt.option(target))
if(global.buildType ===release){
global.outputPath = RELEASE_PATH;
}
if {
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 ,它会输出正确的路径没有问题。



但是,如果我有这样一个任务:
clean:{
release:{
src:global.outputPath
}


抛出以下错误:
警告:无法调用未定义的方法'indexOf'使用--force继续。



另外,setOutput任务中的常量设置在我的Gruntfile.js的顶部。



有什么想法?我在这里做错了什么?

解决方案

所以,我走在了正确的道路上。问题是模块在全局变量被设置之前导出,所以它们在initConfig()任务中定义的后续任务中都未定义。



我提出的解决方案尽管可能有更好的办法是覆盖grunt.option的值。

我的任务有一个可选的选项--target



working solution看起来像这样:

  grunt.registerTask(setOutput,Set the输出文件夹。,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.option(target,global.outputPath);
grunt.log.writeln(Output path:+ grunt.option( 目标));
});

在initConfig()中定义的任务如下所示:

  clean:{
build:{
src:[<%= grunt.option(\target \)% >]
}
}

如果您愿意,有更好的解决方案。否则,这也许可以帮助其他人。


I have a couple grunt tasks and I am trying to share global variables across those tasks and I am running into issues.

I have written a some custom tasks which set the proper output path depending on the build type. This seems to be setting things correctly.

// 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);
});

So, I'm trying to then reference global.outputPath in a subsequent task, and running into errors.

If I call grunt test from the command line, it outputs the correct path no problem.

However, if I have a task like this: clean: { release: { src: global.outputPath } }

It throws the following error: Warning: Cannot call method 'indexOf' of undefined Use --force to continue.

Also, my constants in the setOutput task are set at the top of my Gruntfile.js

Any thoughts? Am I doing something wrong here?

解决方案

So, I was on the right path. The issue is that the module exports before those global variables get set, so they are all undefined in subsequent tasks defined within the initConfig() task.

The solution I came up with, although, there may be better, is to overwrite a grunt.option value.

I have an optional option for my task --target

working solution looks like this:

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.option("target", global.outputPath);
  grunt.log.writeln("Output path: " + grunt.option("target"));
});

And the task defined in initConfig() looked like this:

clean: {
  build: {
    src: ["<%= grunt.option(\"target\") %>"]
  }
}

Feel free to chime in if you have a better solution. Otherwise, perhaps this may help someone else.

这篇关于使用全局变量在Grunt中设置生成输出路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆