diff --git a/README.md b/README.md index 3df70c1..6871f32 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ styles-22222222.css: body {background: url("/loader-11111111.gif")} index-33333333.html: ``` -## Usage: +## Usage ```javascript var filerevReplace = require('gulp-filerev-replace'); @@ -40,12 +40,48 @@ gulp.src('/mysite/**/*', {base: '/mysite'}) ... ``` -### Options: +### Options - `filerev`: [minimatch](https://github.com/isaacs/minimatch) pattern to filter the files to filerev. - `replace`: [minimatch](https://github.com/isaacs/minimatch) pattern to filter the files to replace. - `base`: Directory from where the files are served by the web server. Optional, defaults to the [file's base](https://github.com/gulpjs/gulp/blob/master/docs/API.md#optionsbase); +Manifest +==================== + +A second plugin is also available that adds a new file to the stream. This file contains a JSON list of all files that were filereved. + +## Usage + +```javascript +var filerevReplace = require('gulp-filerev-replace'); + +// Include all files to filerev and/or replace, making sure the base is correct +gulp.src('/mysite/**/*', {base: '/mysite'}) + .pipe(filerevReplace({ + filerev: ['assets/**/*'], // Select the files to filerev + replace: ['views/**/*'], // Select the files to replace + base: 'assets' // Filerevved files are served from the assets directory by the web server + })) + .pipe(filerevReplace.addManifest({path: 'assets/filerev.json'})) + ... +``` + +The example above would add a new file in the stream, with path `assets/filerev.json`, containing something like: + +```javascript +{ + "assets/loader.gif": "assets/loader-11111111.gif", + "assets/styles.css": "assets/styles-22222222.css" +} +``` + +### Options + +`options` is an object containing the [vinyl file options](https://github.com/wearefractal/vinyl#file) to use for the manifest file. It defaults to: + +- `path`: `filerev-replace-manifest.json` + ## TODO - Try to break this into simpler plugins... diff --git a/index.js b/index.js index b5fb42d..81e04a2 100644 --- a/index.js +++ b/index.js @@ -9,6 +9,7 @@ var istextorbinary = require('istextorbinary'); const PLUGIN_NAME = 'gulp-filerev-replace'; const STARTING_DELIMITER = '(\\\\?\'|\\\\?"|\\\\?\\(\\s*)'; const ENDING_DELIMITER = '(\\\\?\'|\\\\?"|\\s*\\\\?\\)|\\?|#)'; +const MANIFEST_OPTIONS = {path: 'filerev-replace-manifest.json'}; function plugin(options) { var self = {}; @@ -21,6 +22,23 @@ function plugin(options) { )(); }; +plugin.addManifest = function(options) { + var manifest = {}; + var manifest_file = new gutil.File(options || MANIFEST_OPTIONS); + + return through.obj(function(file, enc, cb) { + if (file.old_relative) manifest[file.old_relative] = file.relative; + this.push(file); + cb(); + }, function(cb) { + manifest_file.contents = new Buffer(JSON.stringify(manifest, null, 2)); + this.push(manifest_file); + cb(); + }); +}; + +// PRIVATE + function initialize(options) { var self = this; @@ -101,7 +119,8 @@ function filerevAndReplace() { } if (self.filerev[old_relative]) { - file.path = filerevFile(file); + file.path = filerevFile(file); + file.old_relative = old_relative; gutil.log(PLUGIN_NAME, 'Filerevved: ' + old_relative + ' -> ' + file.relative); diff --git a/test/test.js b/test/test.js index 036ec70..3216447 100644 --- a/test/test.js +++ b/test/test.js @@ -123,6 +123,51 @@ describe('gulp-filerev-replace', function() { done(); }); }); + + describe('.addManifest', function() { + it('adds a manifest file to the stream', function(done) { + gulp + .src(fixtures('basic/*')) + .pipe(filerevReplace()) + .pipe(filerevReplace.addManifest()) + .pipe(sa.last(function(file) { + assert.equal(file.relative, 'filerev-replace-manifest.json'); + assert.deepEqual(JSON.parse(file.contents), { + 'loader.gif': 'loader-aef3c727.gif', + 'styles.css': 'styles-b442055f.css', + 'index.html': 'index-42012d94.html' + }); + })) + .pipe(sa.end(done)); + }); + + it('with folders', function(done) { + gulp + .src(fixtures('basic/*'), {base: fixtures('')}) + .pipe(filerevReplace({base: fixtures('basic')})) + .pipe(filerevReplace.addManifest()) + .pipe(sa.last(function(file) { + assert.equal(file.relative, 'filerev-replace-manifest.json'); + assert.deepEqual(JSON.parse(file.contents), { + 'basic/loader.gif': 'basic/loader-aef3c727.gif', + 'basic/styles.css': 'basic/styles-b442055f.css', + 'basic/index.html': 'basic/index-42012d94.html' + }); + })) + .pipe(sa.end(done)); + }); + + it('with options', function(done) { + gulp + .src(fixtures('basic/*')) + .pipe(filerevReplace()) + .pipe(filerevReplace.addManifest({path: 'folder/file.txt'})) + .pipe(sa.last(function(file) { + assert.equal(file.relative, 'folder/file.txt'); + })) + .pipe(sa.end(done)); + }); + }); }); var fixtures = function(glob) {