Reputation: 16842
I'm using the grunt-replace
task to replace some content inside the index.html file. But I want to avoid code duplication as much as possible. The following code snippet does not work as it is, but it's an example of what I'm trying to achieve:
replace: {
options: {
patterns: [
{
match: /<(script.*?src=)"(?![\w/]+environment)/g,
replacement: '<$1"//<%= config.cdn[target] %>/'
}
]
},
a: {
files: [
{
src: '<%= config.path.dist.output %>/index.html',
dest: '<%= config.path.dist.output %>/index-a.html'
}
]
},
b: {
files: [
{
src: '<%= config.path.dist.output %>/index.html',
dest: '<%= config.path.dist.output %>/index-b.html'
}
]
}
}
When I call replace:a
I want the replacement pattern to come from config.cdn['a']
and when I call replace:b
I want the replacement pattern to come from config.cdn['b']
.
Is this possible?
Upvotes: 1
Views: 74
Reputation: 1705
This might be a little bit ugly, but it could work or at least help a little bit:
replace: (function () {
var exports = {},
targets = ['a', 'b'],
target,
i,
j;
for (i = 0, j = targets.length; i < j; i++) {
target = targets[i];
exports[target] = {
options: {
patterns: [
{
match: /<(script.*?src=)"(?![\w/]+environment)/g,
replacement: '<$1"//' + config.cdn[target] + '/'
}
]
},
files: [
{
src: config.path.dist.output + '/index.html',
dest: config.path.dist.output + '/index-' + target + '.html'
}
]
}
}
return exports;
}())
Upvotes: 1