【问题标题】:grunt environment specific options for a task任务的 grunt 环境特定选项
【发布时间】:2016-08-11 21:29:51
【问题描述】:

为了保持 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']
    }

 }

如果生产甚至可以将目标名称设置为 custom-output.min.js,那就更理想了。

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

【问题讨论】:

    标签: gruntjs grunt-contrib-uglify


    【解决方案1】:

    由于 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

    对不起,我不太明白这个问题

    如果生产甚至可以有目的地名称 custom-output.min.js 会更理想。

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

    编辑

    似乎您正在尝试做的是将重复部分从 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 %>'
          }
        }
      }
    }
    

    编辑:11-08-2016,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 %>'
        }
      }
    }
    

    这就是诀窍。

    【讨论】:

    • 我正在尝试将任务编译为文件的缩小生产版本或普通美化开发版本。您编写的示例适用于第一部分。现在只有文件名应该是用于开发的 vendor.js 或用于生产版本的 vender.min.js。
    • 似乎在production 任务下嵌套vendorcustom 会引发Warning: pattern.indexOf is not a function 错误。删除那个额外的级别就可以了。
    【解决方案2】:

    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 {};
            }
        }
    }
    

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

    【讨论】:

    • 生产和开发选项不应该被抓到这个条件下吗,基于grunt任务uglify:production或者uglify:development
    • 这也是可能的。您还可以设置一个变量,如:grunt uglify --type=production 并收集它:grunt.option('type') // = production
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-05
    • 2016-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-10
    相关资源
    最近更新 更多