From 69880ebe6acb85089c7786e6815292f21b3f864e Mon Sep 17 00:00:00 2001 From: Wouter Vanmontfort Date: Thu, 27 Jul 2017 17:55:28 +0200 Subject: [PATCH 1/2] Allow for a custom replace function Adds a 'replacer' options which can be assigned a custom function that is used to replace unreved references with the reved ones, instead of the default `contents.split(unreved).join(reved)`. --- README.md | 30 ++++++++++++++++++++++++++++++ index.js | 2 +- test.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0cc60ac..49f3f0c 100644 --- a/README.md +++ b/README.md @@ -135,6 +135,36 @@ 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 three arguments: the `contents` of the file being processed, the `unreved` +filename whose references are being searched for, and the `reved` filename with +which they should be replaced. +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, you could do the following: + +```js +function replaceFn(contents, unreved, reved) { + return contents.replace(new RegExp('"' + unreved + '"', 'g'), '"' + reved + '"'); +} + +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..98ac9c9 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) : 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..e7e568f 100644 --- a/test.js +++ b/test.js @@ -538,3 +538,47 @@ 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) { + 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(); + }); +}); From 59471b8f027bbbf5f20679fd228b1f3ec21e0bb3 Mon Sep 17 00:00:00 2001 From: Wouter Vanmontfort Date: Mon, 31 Jul 2017 16:50:57 +0200 Subject: [PATCH 2/2] Include the file object as a parameter of the replacer function --- README.md | 16 ++++++++++------ index.js | 2 +- test.js | 6 +++++- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 49f3f0c..ec1fe68 100644 --- a/README.md +++ b/README.md @@ -141,20 +141,24 @@ Type: `Function` Default: `return contents.split(unreved).join(reved)` Use a custom method for replacing the file references. The function is provided -with three arguments: the `contents` of the file being processed, the `unreved` -filename whose references are being searched for, and the `reved` filename with -which they should be replaced. +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, you could do the following: +quotes in css files, you could do the following: ```js -function replaceFn(contents, unreved, reved) { - return contents.replace(new RegExp('"' + unreved + '"', 'g'), '"' + reved + '"'); +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') diff --git a/index.js b/index.js index 98ac9c9..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 = options.replacer ? options.replacer(contents, unreved, reved) : 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 e7e568f..586bae2 100644 --- a/test.js +++ b/test.js @@ -550,7 +550,11 @@ describe('replacer option', function() { }) ]); - function replacerFn(contents, unreved, reved) { + 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 + '~'); }