diff --git a/README.md b/README.md index 0cc60ac..ec1fe68 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/index.js b/index.js index ea345c0..69b8711 100644 --- a/index.js +++ b/index.js @@ -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 + '/'); } diff --git a/test.js b/test.js index 06761ab..586bae2 100644 --- a/test.js +++ b/test.js @@ -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(); + }); +});