Javascript 在Grunt中生成全局对象
我正在尝试为grunt使用includereplace插件。我试图通过使用下面的代码获取JSON,为它提供Javascript 在Grunt中生成全局对象,javascript,json,node.js,gruntjs,Javascript,Json,Node.js,Gruntjs,我正在尝试为grunt使用includereplace插件。我试图通过使用下面的代码获取JSON,为它提供globals属性的值: grunt.registerTask('fetchProperties', 'Fetches the properties.json file', function() { properties = grunt.file.readJSON('properties.json'); grunt.log.writeln(['Properties file
globals
属性的值:
grunt.registerTask('fetchProperties', 'Fetches the properties.json file', function() {
properties = grunt.file.readJSON('properties.json');
grunt.log.writeln(['Properties file loaded']);
});
现在,当我执行properties.var\u a
时,它会正确返回var\u 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
任务替换的值是未定义的
。
如何缓解这个问题?
此外,我还尝试使用grunt.config
设置变量,但问题是,我有一个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'
}]
}
}
});
grunt.initConfig({
属性:grunt.file.readJSON('properties.json'),
包括地点:{
地区:{
选项:{
全球:{
变量A:“”,
变量B:“”
},
},
档案:[{
src:“../myFiles/path/to/some/file/main.txt”,
dest:“../myOtherFiles/path/to/some/file/mainrep.txt”
}]
}
}
});
用于此