在Grunt中创build全局对象

我试图使用includereplace插件的咕噜声。 我想通过使用下面的代码获取一个JSON为globals属性赋值:

 grunt.registerTask('fetchProperties', 'Fetches the properties.json file', function() { properties = grunt.file.readJSON('properties.json'); grunt.log.writeln(['Properties file loaded']); }); 

现在当我做的properties.var_a var_a正确地返回var_a的值。 所以,我这样做了:

  grunt.registerTask('fetchProperties', 'Fetches the properties.json file', function() { properties = grunt.file.readJSON('properties.json'); grunt.task.run('includereplace'); }) 

这是我的includereplace任务:

 includereplace: { dist: { options: { globals: { VAR_A: properties.var_a, VAR_B: properties.var_b }, }, files: [{ src: '../myFiles/path/to/some/file/main.txt', dest: '../myOtherFiles/path/to/some/file/mainrep.txt' }] } } 

现在,由includereplace任务取代的值是undefined 。 如何缓解这个问题? 此外,我已经尝试使用grunt.config设置variables,但事情是,我有一个JSON文件,加载将返回一个对象,而不是一个单一的值。 如何设置一个全局对象,可以被执行中的所有任务用来设置参数? 我在grunt文件的开头加载JSON文件。 这是我的module.exports = function(grunt) { ...

这工作!

 grunt.initConfig({ properties: grunt.file.readJSON('properties.json'), includereplace: { dist: { options: { globals: { VAR_A: '<%= properties.var_a %>', VAR_B: '<%= properties.var_b %>' }, }, files: [{ src: '../myFiles/path/to/some/file/main.txt', dest: '../myOtherFiles/path/to/some/file/mainrep.txt' }] } } }); 

使用https://stackoverflow.com/a/16792592/2459789为此&#x3002;