Skip to content

Commit

Permalink
Merge pull request #1 from solidusjs/filerev-manifest
Browse files Browse the repository at this point in the history
New addManifest plugin
  • Loading branch information
joanniclaborde committed May 1, 2015
2 parents 2e297e8 + 99315a3 commit 98659a9
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 3 deletions.
40 changes: 38 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ styles-22222222.css: body {background: url("/loader-11111111.gif")}
index-33333333.html: <img src="/loader-11111111.gif" /> <link href="/styles-22222222.css" type="text/css" />
```

## Usage:
## Usage

```javascript
var filerevReplace = require('gulp-filerev-replace');
Expand All @@ -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...
Expand Down
21 changes: 20 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand All @@ -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;

Expand Down Expand Up @@ -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);

Expand Down
45 changes: 45 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 98659a9

Please sign in to comment.