任务的 grunt 环境特定选项

grunt environment specific options for a task

为了让 gruntfile 保持干爽,我想根据环境更改选项,我是 运行 的 grunt 任务。

例如,如果我想使用两个 grunt 任务:

grunt.task.run('uglify:production');
grunt.task.run('uglify:development');

我希望他们都编译相同的文件,但选项不同。

uglify: {

  production: {
    options: {
      compress: true
    }
  }

  development: {
    options: {
      compress: false,
      beautify: true
    }
  }

  // rather not redeclare these files twice
  files: {

    vendor: {
      // this name should change based on the environment
      'dest/vendor-output.js': ['src/vendor-input1.js', 'src/vendor-input2.js']
    },
    custom: {
      'dest/custom-output.js': ['src/custominput1.js', 'src/custominput2.js']
    }

 }

如果生产甚至可以将目的地名称自定义为output.min.js,那就更理想了。

尝试了 if-else 语句,但在 grunt 任务定义中无法运行。

Grunt 是 Javascript,因此您实际上可以添加 IF/ELSE 语句。

示例:

files: {
    (() => { 
        if (grunt.option('vendor')) {
            return {
                'dest/vendor-output.js': ['src/vendor-input1.js', 'src/vendor-input2.js']
            }
        } else (grunt.option('release')) {
            return {
                'dest/custom-output.js': ['src/custominput1.js', 'src/custominput2.js']
            }
        }
    }())
}

/***
 * OR SOMETHING LIKE
 **/

files: {
    (() => { 
        switch(grunt.option) {
            case 'vendor':
                return {
                    'dest/vendor-output.js': ['src/vendor-input1.js', 'src/vendor-input2.js']
                };
                break;
            case 'release:
                return {
                    'dest/custom-output.js': ['src/custominput1.js', 'src/custominput2.js']
                };
                break;
            default:
                return {};
        }
    }
}

显然,您需要将其更改为您想要的情况,因为现在不知道您如何处理供应商 and/or 发布。

由于 grunt 配置只是 json,您可以使用 grunt 模板并将文件作为配置的 属性。

uglifyFiles: {

  vendor: {
    // this name should change based on the environment
    'dest/vendor-output.js': ['src/vendor-input1.js', 'src/vendor-input2.js']
  },
  custom: {
    'dest/custom-output.js': ['src/custominput1.js', 'src/custominput2.js']
  }
},
uglify: {

  production: {
    options: {
      compress: true
    },
    files: '<%= uglifyFiles %>'
  },

  development: {
    options: {
      compress: false,
      beautify: true
    },
    files: '<%= uglifyFiles %>'
  }
}

http://gruntjs.com/configuring-tasks#templates

抱歉我不太明白这个问题

If production could even have the destination name to custom-output.min.js that would be even more ideal.

您能否提供更多信息,或者以上是您想要实现的目标?

编辑

您尝试做的似乎是从 DRY 中删除重复部分,因为您实际上希望每个部分的代码略有不同。它可以完成,但不能在 json 中完成,您需要使用 js 并使用括号表示法将目标创建为它的键。我认为一个更简单的方法,以及像这样设置 grunt 的目的,就是执行以下操作。

vendorUglifyFiles: ['src/vendor-input1.js', 'src/vendor-input2.js'],
customUglifyFiles: ['src/custominput1.js', 'src/custominput2.js'],
uglify: {

  production: {
    options: {
      compress: true
    },
    files: {
      vendor: {
        'dest/vendor.min.js': '<%= vendorUglifyFiles %>'
      },
      custom: {
        'dest/custom.min.js': '<%= customUglifyFiles %>'
      }
    }
  },

  development: {
    options: {
      compress: false,
      beautify: true
    },
    files: {
      vendor: {
        'dest/vendor.js': '<%= vendorUglifyFiles %>'
      },
      custom: {
        'dest/custom.js': '<%= customUglifyFiles %>'
      }
    }
  }
}

编辑:2016 年 11 月 8 日,15:12

删除了引发 indexOf 错误的级别:

vendorUglifyFiles: ['src/vendor-input1.js', 'src/vendor-input2.js'],
customUglifyFiles: ['src/custominput1.js', 'src/custominput2.js'],
uglify: {

  production: {
    options: {
      compress: true
    },
    files: {
      'dest/vendor.min.js': '<%= vendorUglifyFiles %>',
      'dest/custom.min.js': '<%= customUglifyFiles %>'
    }
  },

  development: {
    options: {
      compress: false,
      beautify: true
    },
    files: {
        'dest/vendor.js': '<%= vendorUglifyFiles %>',
        'dest/custom.js': '<%= customUglifyFiles %>'
    }
  }
}

这就是诀窍。