Javascript Grunt替换:参数化json文件
早上好 我在gruntfile中使用grunt replace()通过从json文件加载json对象来替换html文件中的一些字符串 我想为这种方法增加一些灵活性,我定制了另一个名为“setopts”的任务,它只需向grunt.option添加一些属性,我使用“替换”任务的方式如下:Javascript Grunt替换:参数化json文件,javascript,json,gruntjs,Javascript,Json,Gruntjs,早上好 我在gruntfile中使用grunt replace()通过从json文件加载json对象来替换html文件中的一些字符串 我想为这种方法增加一些灵活性,我定制了另一个名为“setopts”的任务,它只需向grunt.option添加一些属性,我使用“替换”任务的方式如下: replace: { common: { options: { patterns: [ { json: '<%=grunt.op
replace: {
common: {
options: {
patterns: [
{
json: '<%=grunt.option("locales")%>'
}
]
},
files: [
{expand: true, flatten: true, src: ['public/sites/<%=grunt.option("domain")%>/index.html'], dest: 'public/sites/<%=grunt.option("domain")%>/'},
]
}
}
我运行以下任务:
grunt.registerTask('maintask', [ 'setopts:mydomain', 'replace:common']);
经过一些尝试后,我发现“替换”任务中的“文件”属性工作正常,但“模式”属性中出现错误:
正在处理源…错误
警告:处理“public/sites/xxxxx/index.html”文件时出错。使用--force继续
这怎么了
谢谢你的评论 我知道我迟到了1.5年,但也许其他人需要答案 我的工作方式是不使用
grunt.option
。相反,我使用了grunt.config.set
replace: {
common: {
options: {
patterns: [
{
json: '<%= locales %>'
}
]
},
files: [
{expand: true, flatten: true, src: ['public/sites/<%= domain %>/index.html'], dest: 'public/sites/<%= domain %>/'},
]
}
}
希望它能帮助某人:)
这个问题帮助我找到了答案
replace: {
common: {
options: {
patterns: [
{
json: '<%= locales %>'
}
]
},
files: [
{expand: true, flatten: true, src: ['public/sites/<%= domain %>/index.html'], dest: 'public/sites/<%= domain %>/'},
]
}
}
grunt.registerTask('setopts', function (domain) {
locales = grunt.file.readJSON('src/locales/domain/myfile.json');
grunt.config.set('locales', locales);
grunt.config.set('domain', domain);
}