Gruntjs 任务的特定于grunt环境的选项

Gruntjs 任务的特定于grunt环境的选项,gruntjs,grunt-contrib-uglify,Gruntjs,Grunt Contrib Uglify,为了保持gruntfile干燥,我想根据运行grunt任务的环境更改选项 例如,如果我想使用两个grunt任务: grunt.task.run('uglify:production'); grunt.task.run('uglify:development'); 我希望他们都编译相同的文件,但是有不同的选项 uglify: { production: { options: { compress: true } } development: {

为了保持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任务定义内,该语句无法运行。

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配置只是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 %>'
  }
}
uglifyFiles:{
供应商:{
//此名称应根据环境而更改
'dest/vendor output.js':['src/vendor-input1.js','src/vendor-input2.js']
},
自定义:{
'dest/custom output.js':['src/custominput1.js','src/custominput2.js']
}
},
丑陋的:{
制作:{
选项:{
压缩:真
},
文件:“”
},
发展:{
选项:{
压缩:错,
美化:真的
},
文件:“”
}
}

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

如果生产部门甚至可以将目标名称 custom-output.min.js更理想

你能提供更多的信息吗,或者以上是你想要实现的吗

编辑

看起来你要做的是去掉重复部分,因为你实际上希望每个部分的代码略有不同。这可以完成,但不能在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 %>'
      }
    }
  }
}
vendorUglifyFiles:['src/vendor-input1.js','src/vendor-input2.js'],
CustomUglifyFile:['src/custominput1.js','src/custominput2.js'],
丑陋的:{
制作:{
选项:{
压缩:真
},
档案:{
供应商:{
“dest/vendor.min.js”:”
},
自定义:{
“dest/custom.min.js”:”
}
}
},
发展:{
选项:{
压缩:错,
美化:真的
},
档案:{
供应商:{
“dest/vendor.js”:”
},
自定义:{
“dest/custom.js”:”
}
}
}
}
编辑:2016年8月11日,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 %>'
    }
  }
}
vendorUglifyFiles:['src/vendor-input1.js','src/vendor-input2.js'],
CustomUglifyFile:['src/custominput1.js','src/custominput2.js'],
丑陋的:{
制作:{
选项:{
压缩:真
},
档案:{
“dest/vendor.min.js”:”,
“dest/custom.min.js”:”
}
},
发展:{
选项:{
压缩:错,
美化:真的
},
档案:{
'dest/vendor.js':'',
“dest/custom.js”:”
}
}
}

这就是诀窍

基于grunt任务
uglify:production
uglify:development
,生产和开发选项不应该被捕获在这个条件中吗?这也是可能的。您还可以设置一个变量,如:
grunt uglify--type=production
,并使用:
grunt.option('type')/=production
收集它。我正在尝试将一个任务编译成文件的小型生产版本或普通的美化开发版本。您编写的示例适用于第一部分。现在只有文件名应该是vendor.js(用于开发)或vendor.min.js(用于生产版本)。似乎在
生产任务下嵌套
vendor
custom
会抛出一个
警告:pattern.indexOf不是一个函数
错误。移除这个额外的关卡就可以了。