Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,40 @@ return gulp.src(opt.distFolder + '**/*.js')
.pipe(gulp.dest(opt.distFolder));
```

#### options.replacer
Type: `Function`

Default: `return contents.split(unreved).join(reved)`

Use a custom method for replacing the file references. The function is provided
with four arguments: the `contents` of the file being processed, the `unreved`
filename whose references are being searched for, the `reved` filename with which
they should be replaced, and the (Vinyl) `file` object that is being searched.
Note that the `replacer` function is called after `modifyUnreved` and `modifyReved`
have been applied.

The function should return the new `contents`.

For example if you only want to replace the reference if it's between (double)
quotes in css files, you could do the following:

```js
function replaceFn(contents, unreved, reved, file) {
if (file.path.indexOf('.css') > -1) {
return contents.replace(new RegExp('"' + unreved + '"', 'g'), '"' + reved + '"');
} else {
return contents;
}
}

return gulp.src(opt.distFolder + '**/*.js')
.pipe(revReplace({
manifest: manifest,
replacer: replaceFn
}))
.pipe(gulp.dest(opt.distFolder));
```

## Contributors

- Chad Jablonski
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function plugin(options) {
renames.forEach(function replaceOnce(rename) {
var unreved = options.modifyUnreved ? options.modifyUnreved(rename.unreved) : rename.unreved;
var reved = options.modifyReved ? options.modifyReved(rename.reved) : rename.reved;
contents = contents.split(unreved).join(reved);
contents = options.replacer ? options.replacer(contents, unreved, reved, file) : contents.split(unreved).join(reved);
if (options.prefix) {
contents = contents.split('/' + options.prefix).join(options.prefix + '/');
}
Expand Down
48 changes: 48 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -538,3 +538,51 @@ describe('modifyUnreved and modifyReved options', function() {
stream.end();
});
});

describe('replacer option', function() {
it('should use the provided replacer method', function(cb) {
var manifest = es.readArray([
new gutil.File({
path: '/project/rev-manifest.json',
contents: new Buffer(JSON.stringify({
'css/style.css': 'css/style-12345.css'
}))
})
]);

function replacerFn(contents, unreved, reved, file) {
assert(
file.path === 'index.html',
'The provided file object should be the file being searched for references'
);
return contents.replace(unreved, '~' + reved + '~');
}

var stream = revReplace({
manifest: manifest,
replacer: replacerFn
});

var replacedCSSFilePattern = /~css\/style-12345\.css~/;

stream.on('data', function(file) {
var contents = file.contents.toString();

assert(
replacedCSSFilePattern.test(contents),
'The renamed CSS file\'s name should be replaced by the replacer function'
);
});

stream.on('end', function() {
cb();
});

stream.write(new gutil.File({
path: 'index.html',
contents: new Buffer(htmlFileBody)
}));

stream.end();
});
});