Skip to content

Pull request for issue #150 #151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 20 commits into from
Closed
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
12 changes: 0 additions & 12 deletions .jshintrc

This file was deleted.

23 changes: 16 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var file = require('vinyl-file');
var revHash = require('rev-hash');
var revPath = require('rev-path');
var sortKeys = require('sort-keys');
var modifyFilename = require('modify-filename');

function relPath(base, filePath) {
if (filePath.indexOf(base) !== 0) {
Expand Down Expand Up @@ -44,7 +45,16 @@ function transformFilename(file) {
file.revOrigPath = file.path;
file.revOrigBase = file.base;
file.revHash = revHash(file.contents);
file.path = revPath(file.path, file.revHash);

file.path = modifyFilename(file.path, function (filename, extension) {
var extIndex = filename.indexOf('.');

filename = extIndex === -1 ?
revPath(filename, file.revHash) :
revPath(filename.slice(0, extIndex), file.revHash) + filename.slice(extIndex);

return filename + extension;
});
}

var plugin = function () {
Expand All @@ -62,7 +72,7 @@ var plugin = function () {
return;
}

// This is a sourcemap, hold until the end
// this is a sourcemap, hold until the end
if (path.extname(file.path) === '.map') {
sourcemaps.push(file);
cb();
Expand All @@ -72,8 +82,8 @@ var plugin = function () {
var oldPath = file.path;
transformFilename(file);
pathMap[oldPath] = file.revHash;
cb(null, file);

cb(null, file);
}, function (cb) {
sourcemaps.forEach(function (file) {
var reverseFilename;
Expand Down Expand Up @@ -115,7 +125,6 @@ plugin.manifest = function (pth, opts) {
merge: false
}, opts, pth);

var firstFileBase = null;
var manifest = {};

return through.obj(function (file, enc, cb) {
Expand All @@ -124,10 +133,10 @@ plugin.manifest = function (pth, opts) {
cb();
return;
}

var base = opts.base || file.base;

firstFileBase = firstFileBase || file.base;

var revisionedFile = relPath(firstFileBase, file.path);
var revisionedFile = relPath(base, file.path);
var originalFile = path.join(path.dirname(revisionedFile), path.basename(file.revOrigPath)).replace(/\\/g, '/');

manifest[originalFile] = revisionedFile;
Expand Down
18 changes: 13 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gulp-rev",
"version": "5.0.1",
"version": "6.0.1",
"description": "Static asset revisioning by appending content hash to filenames: unicorn.css => unicorn-d41d8cd98f.css",
"license": "MIT",
"repository": "sindresorhus/gulp-rev",
Expand All @@ -20,7 +20,7 @@
"node": ">=0.10.0"
},
"scripts": {
"test": "mocha"
"test": "xo && mocha"
},
"files": [
"index.js"
Expand All @@ -42,14 +42,22 @@
],
"dependencies": {
"gulp-util": "^3.0.0",
"object-assign": "^2.0.0",
"modify-filename": "^1.1.0",
"object-assign": "^4.0.1",
"rev-hash": "^1.0.0",
"rev-path": "^1.0.0",
"sort-keys": "^1.0.0",
"through2": "^0.6.1",
"through2": "^2.0.0",
"vinyl-file": "^1.1.0"
},
"devDependencies": {
"mocha": "*"
"mocha": "*",
"xo": "*"
},
"xo": {
"envs": [
"node",
"mocha"
]
}
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ For more info on how to integrate **gulp-rev** into your app, have a look at the
- [gulp-rev-outdated](https://github.com/shonny-ua/gulp-rev-outdated) - Old static asset revision files filter
- [gulp-rev-collector](https://github.com/shonny-ua/gulp-rev-collector) - Static asset revision data collector
- [rev-del](https://github.com/callumacrae/rev-del) - Delete old unused assets

- [gulp-rev-loader](https://github.com/adjavaherian/gulp-rev-loader) - Use rev-manifest with webpack

## License

Expand Down
68 changes: 66 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,23 @@ it('should rev files', function (cb) {
}));
});

it('should add the revision hash before the first `.` in the filename', function (cb) {
var stream = rev();

stream.on('data', function (file) {
assert.equal(file.path, 'unicorn-d41d8cd98f.css.map');
assert.equal(file.revOrigPath, 'unicorn.css.map');
cb();
});

stream.write(new gutil.File({
path: 'unicorn.css.map',
contents: new Buffer('')
}));

stream.end();
});

it('should build a rev manifest file', function (cb) {
var stream = rev.manifest();

Expand Down Expand Up @@ -55,6 +72,7 @@ it('should allow naming the manifest file', function (cb) {
path: 'unicorn-d41d8cd98f.css',
contents: new Buffer('')
});

file.revOrigPath = 'unicorn.css';

stream.write(file);
Expand Down Expand Up @@ -85,7 +103,6 @@ it('should append to an existing rev manifest file', function (cb) {

stream.write(file);
stream.end();

});

it('should not append to an existing rev manifest by default', function (cb) {
Expand All @@ -109,7 +126,6 @@ it('should not append to an existing rev manifest by default', function (cb) {

stream.write(file);
stream.end();

});

it('should sort the rev manifest keys', function (cb) {
Expand Down Expand Up @@ -307,3 +323,51 @@ it('should be okay when the optional sourcemap.file is not defined', function (c
contents: new Buffer(JSON.stringify({}))
}));
});

it('should handle a . in the folder name', function (cb) {
var stream = rev();

stream.on('data', function (file) {
assert.equal(file.path, 'mysite.io/unicorn-d41d8cd98f.css');
assert.equal(file.revOrigPath, 'mysite.io/unicorn.css');
cb();
});

stream.write(new gutil.File({
path: 'mysite.io/unicorn.css',
contents: new Buffer('')
}));
});

it('should use correct base path for each file', function (cb) {
var stream = rev.manifest();

stream.on('data', function (newFile) {
var MANIFEST = {};
MANIFEST[path.join('foo', 'scriptfoo.js')] = path.join('foo', 'scriptfoo-d41d8cd98f.js');
MANIFEST[path.join('bar', 'scriptbar.js')] = path.join('bar', 'scriptbar-d41d8cd98f.js');

assert.deepEqual(JSON.parse(newFile.contents.toString()), MANIFEST);
cb();
});

var fileFoo = new gutil.File({
cwd: 'app/',
base: 'app/',
path: path.join('app', 'foo', 'scriptfoo-d41d8cd98f.js'),
contents: new Buffer('')
});
fileFoo.revOrigPath = 'scriptfoo.js';

var fileBar = new gutil.File({
cwd: 'assets/',
base: 'assets/',
path: path.join('assets', 'bar', 'scriptbar-d41d8cd98f.js'),
contents: new Buffer('')
});
fileBar.revOrigPath = 'scriptbar.js';

stream.write(fileFoo);
stream.write(fileBar);
stream.end();
});