Goodmornig,
我正在使用grunt替换(https://github.com/outaTiME/grunt-replace)在我的gruntfile中,通过从json文件加载json对象来替换html文件中的一些字符串。
我想为这种方法增加一些灵活性,我定制了另一个名为"setopts"的任务,它只需为grunt添加一些属性。我在"替换"任务中使用的选项如下:
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")%>/'},
]
}
}
这里是我的"setopts"任务:
grunt.registerTask('setopts', function (domain) {
locales = grunt.file.readJSON('src/locales/domain/myfile.json');
grunt.option('locales', locales);
grunt.option('domain', domain);
}
我运行以下任务:
grunt.registerTask('maintask', [ 'setopts:mydomain', 'replace:common']);
经过一些尝试,我发现"替换"任务中的"files"属性工作正常,但我在"patterns"属性中得到了一个错误:
正在处理源。。。错误警告:处理"public/sites/xxxxxx/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 %>/'},
]
}
}
请注意locales
变量用作json
属性值的方式。
这是setopts
任务:
grunt.registerTask('setopts', function (domain) {
locales = grunt.file.readJSON('src/locales/domain/myfile.json');
grunt.config.set('locales', locales);
grunt.config.set('domain', domain);
}
希望它能帮助到某人:)
这个问题帮助我找到了答案。程序化地将论点传递给咕哝的任务?