gulp-rev-all
Advanced tools
Comparing version 0.1.5 to 0.2.0
19
index.js
@@ -11,5 +11,6 @@ var fs = require('fs'); | ||
var first = true; | ||
options = options || {}; | ||
var first = true; | ||
var revIndexHtml = !options.noIndexHtmlRev; | ||
options.hashLength = options.hashLength || 8; | ||
options.ignore = options.ignore || options.ignoredExtensions || [ /^\/favicon.ico$/g ]; | ||
@@ -20,7 +21,4 @@ return through.obj(function (file, enc, callback) { | ||
if (options.rootDir === undefined) options.rootDir = file.base; | ||
if (options.ignoredExtensions === undefined) options.ignoredExtensions = []; | ||
if (first) { | ||
options.rootDir = options.rootDir || file.base; | ||
gutil.log('gulp-rev-all:', 'Root directory [', options.rootDir, ']'); | ||
@@ -41,10 +39,7 @@ first = !first; | ||
case '.html': | ||
tools.revReferencesInFile(file, options.rootDir); | ||
tools.revReferencesInFile(file); | ||
} | ||
var indexPath = path.join(options.rootDir, 'index.html') | ||
if (path.normalize(file.path) !== path.normalize(indexPath) || revIndexHtml) { | ||
// Rename this file with the revion hash | ||
// Rename this file with the revion hash if doesn't match ignore list | ||
if (!tools.isFileIgnored(file)) { | ||
var filenameReved = path.basename(tools.revFile(file.path)); | ||
@@ -51,0 +46,0 @@ var base = path.dirname(file.path); |
{ | ||
"name": "gulp-rev-all", | ||
"version": "0.1.5", | ||
"version": "0.2.0", | ||
"description": "Static asset revisioning by appending content hash to filenames: unicorn.css => unicorn-098f6bcd.css, also re-writes references in each file to new reved name.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -73,6 +73,10 @@ # [gulp](https://github.com/wearefractal/gulp)-rev-all [![Build Status](https://travis-ci.org/smysnk/gulp-rev-all.png?branch=master)](https://travis-ci.org/smysnk/gulp-rev-all) | ||
``` | ||
** Note: I have submitted a [pull request](https://github.com/nkostelnik/gulp-s3/pull/7) to gulp-s3 as it currently does not support file contents from streams, which makes it incompatible with [gulp-gzip](https://github.com/jstuckey/gulp-gzip). In the mean time you can use my forked version [here](https://github.com/smysnk/gulp-s3). | ||
## Ignoring Extensions | ||
## API | ||
#### options.ignore | ||
Type: `Array of RegEx or String` | ||
Default: `[ /^\/favicon.ico$/ ]` | ||
In some cases, you may not want to rev your `*.html` files: | ||
@@ -83,3 +87,3 @@ | ||
gulp.src('dist/**') | ||
.pipe(revall({ ignoredExtensions: ['.html'] })) | ||
.pipe(revall({ ignore: [/^\/favicon.ico$/g, '.html'] })) | ||
.pipe(gulp.dest('dist')) | ||
@@ -89,4 +93,17 @@ }); | ||
## Variable Hash Length | ||
Every html file except the root `/index.html` file: | ||
```js | ||
gulp.task('default', function () { | ||
gulp.src('dist/**') | ||
.pipe(revall({ ignore: [/^\/favicon.ico$/g, /^\/index.html/g] })) | ||
.pipe(gulp.dest('dist')) | ||
}); | ||
``` | ||
#### options.hashLength | ||
Type: `hashLength` | ||
Default: `8` | ||
Change the length of the hash appended to the end of each file: | ||
@@ -93,0 +110,0 @@ |
@@ -18,3 +18,3 @@ var revall = require("../index"); | ||
var tools = toolsFactory({}); | ||
var tools = toolsFactory({hashLength: 8, ignore: ['favicon.ico'], rootDir: 'test/fixtures/config1'}); | ||
@@ -67,12 +67,9 @@ describe('should process images', function() { | ||
describe("noIndexHtmlRev", function () { | ||
it('should not rename index.html if noIndexHtmlRev is set', function(done) { | ||
describe("hash length", function() { | ||
stream = revall({rootDir: 'test/fixtures/config1', noIndexHtmlRev: true}); | ||
it("should have proper length when specified", function(done) { | ||
stream = revall({rootDir: 'test/fixtures/config1', hashLength: 4, ignore: []}); | ||
stream.on('data', function (file) { | ||
path.basename(file.path).should.not.match(/\index-[a-z0-9]{8}\.html$/); | ||
if (file.path == 'index.html') { | ||
var revedReference = path.basename(tools.revFile('test/fixtures/config1/css/style.css')); | ||
String(file.contents).should.containEql(revedReference); | ||
} | ||
path.basename(file.path).should.match(/\-[a-z0-9]{4}\.[a-z]{2,4}$/); | ||
}); | ||
@@ -87,9 +84,10 @@ | ||
describe("hash length", function() { | ||
it("should have proper length when specified", function(done) { | ||
describe("ignore", function() { | ||
stream = revall({rootDir: 'test/fixtures/config1', hashLength: 4}); | ||
it("should not rename favicon.ico by default", function (done) { | ||
stream = revall({rootDir: 'test/fixtures/config1'}); | ||
stream.on('data', function (file) { | ||
path.basename(file.path).should.match(/\-[a-z0-9]{4}\.[a-z]{2,4}$/); | ||
path.basename(file.path).should.not.match(/favicon\-[a-z0-9]{8}\.ico$/); | ||
}); | ||
@@ -102,10 +100,16 @@ | ||
}); | ||
it("should rename nested index", function (done) { | ||
stream = revall({rootDir: 'test/fixtures/config1', ignore: [ /^\/index.html/g ]}); | ||
stream.on('data', function (file) { | ||
file.path.should.not.match(/nested\/index\.html$/); | ||
file.path.should.not.match(/config1\/index\-[a-z0-9]{8}\.html$/); | ||
}); | ||
stream.on('end', done); | ||
describe("ignore extension", function() { | ||
writeFile(); | ||
}); | ||
it("should not rename html files when specified", function (done) { | ||
stream = revall({rootDir: 'test/fixtures/config1', ignoredExtensions: ['.html']}); | ||
stream = revall({rootDir: 'test/fixtures/config1', ignore: ['.html']}); | ||
stream.on('data', function (file) { | ||
@@ -122,3 +126,3 @@ path.basename(file.path).should.not.match(/\-[a-z0-9]{8}\.html$/); | ||
it("should not rename js files when specified", function (done) { | ||
stream = revall({rootDir: 'test/fixtures/config1', ignoredExtensions: ['.js']}); | ||
stream = revall({rootDir: 'test/fixtures/config1', ignore: ['.js']}); | ||
stream.on('data', function (file) { | ||
@@ -134,3 +138,3 @@ path.basename(file.path).should.not.match(/\-[a-z0-9]{8}\.js$/); | ||
it("should not rename woff files when specified", function (done) { | ||
stream = revall({rootDir: 'test/fixtures/config1', ignoredExtensions: ['.woff']}); | ||
stream = revall({rootDir: 'test/fixtures/config1', ignore: ['.woff']}); | ||
stream.on('data', function (file) { | ||
@@ -143,2 +147,13 @@ path.basename(file.path).should.not.match(/\-[a-z0-9]{8}\.woff$/); | ||
}); | ||
it("should rename all files when ignore not specified", function (done) { | ||
stream = revall({rootDir: 'test/fixtures/config1'}); | ||
stream.on('data', function (file) { | ||
path.basename(file.path).should.match(/(\-[a-z0-9]{8}\.[a-z]{2,4}$|favicon\.ico$)/); | ||
}); | ||
stream.on('end', done); | ||
writeFile(); | ||
}); | ||
}); | ||
@@ -163,2 +178,12 @@ | ||
it("should resolve absolute path reference", function(done) { | ||
stream.on('data', function (file) { | ||
var revedReference = path.basename(tools.revFile('test/fixtures/config1/index.html')); | ||
String(file.contents).should.containEql("'/" + revedReference); | ||
done(); | ||
}); | ||
writeFile(); | ||
}); | ||
it("should resolve reference to css", function(done) { | ||
@@ -165,0 +190,0 @@ stream.on('data', function (file) { |
46
tools.js
@@ -10,4 +10,3 @@ var fs = require('fs'); | ||
var fileMap = {}; | ||
var hashLength = options.hashLength || 8; | ||
// Taken from gulp-rev: https://github.com/sindresorhus/gulp-rev | ||
@@ -18,2 +17,14 @@ var md5 = function (str) { | ||
var isFileIgnored = function (file) { | ||
var filename = (typeof file === 'string') ? file : file.path; | ||
filename = filename.substr(options.rootDir.length); | ||
for (var i = options.ignore.length; i--;) { | ||
var regex = (options.ignore[i] instanceof RegExp) ? options.ignore[i] : new RegExp(options.ignore[i] + '$', "ig"); | ||
if (filename.match(regex)) return true; | ||
} | ||
return false; | ||
}; | ||
// Taken from gulp-rev: https://github.com/sindresorhus/gulp-rev | ||
@@ -29,9 +40,9 @@ var revFile = function (filePath) { | ||
if (typeof options.ignoredExtensions === 'undefined' || options.ignoredExtensions.indexOf(ext) === -1) { | ||
if (isFileIgnored(filePath)) { | ||
filename = path.basename(filePath); | ||
} else { | ||
var contents = fs.readFileSync(filePath).toString(); | ||
var hash = md5(contents).slice(0, hashLength); | ||
var hash = md5(contents).slice(0, options.hashLength); | ||
filename = path.basename(filePath, ext) + '-' + hash + ext; | ||
} else { | ||
filename = path.basename(filePath); | ||
} | ||
} | ||
@@ -44,6 +55,5 @@ filePathReved = path.join(path.dirname(filePath), filename); | ||
var revReferencesInFile = function (file, rootDir) { | ||
var revReferencesInFile = function (file) { | ||
var replaceMap = {}; | ||
rootDir = rootDir || path.dirname(file.path); | ||
@@ -61,9 +71,7 @@ gutil.log('gulp-rev-all:', 'Finding references in [', file.path, ']'); | ||
// In the case where the referenced file is relative to the base path | ||
if (rootDir) { | ||
var fullpath = path.join(rootDir, result[1]); | ||
if (fs.existsSync(fullpath)) { | ||
replaceMap[result[1]] = path.dirname(result[1]) + '/' + path.basename(revFile(fullpath)); | ||
gutil.log('gulp-rev-all:', 'Found root reference [', result[1], '] -> [', replaceMap[result[1]], ']'); | ||
continue; | ||
} | ||
var fullpath = path.join(options.rootDir, result[1]); | ||
if (fs.existsSync(fullpath)) { | ||
replaceMap[result[1]] = path.join(path.dirname(result[1]), path.basename(revFile(fullpath))); | ||
gutil.log('gulp-rev-all:', 'Found root reference [', result[1], '] -> [', replaceMap[result[1]], ']'); | ||
continue; | ||
} | ||
@@ -74,3 +82,3 @@ | ||
if (fs.existsSync(fullpath)) { | ||
replaceMap[result[1]] = path.dirname(result[1]) + '/' + path.basename(revFile(fullpath)); | ||
replaceMap[result[1]] = path.join(path.dirname(result[1]), path.basename(revFile(fullpath))); | ||
gutil.log('gulp-rev-all:', 'Found relative reference [', result[1], '] -> [', replaceMap[result[1]], ']'); | ||
@@ -91,7 +99,9 @@ continue; | ||
return { | ||
revFile: revFile, | ||
revReferencesInFile: revReferencesInFile | ||
revReferencesInFile: revReferencesInFile, | ||
isFileIgnored: isFileIgnored | ||
}; | ||
}; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
467374
25
417
124