gulp-rev-all
Advanced tools
Comparing version 0.2.1 to 0.2.2
{ | ||
"name": "gulp-rev-all", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"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,3 +73,3 @@ # [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) | ||
``` | ||
* See [gulp-s3](https://www.npmjs.org/package/gulp-s3), [gulp-gzip](https://www.npmjs.org/package/gulp-gzip), [gulp-cloudfront](https://www.npmjs.org/package/gulp-cloudfront) | ||
@@ -107,3 +107,3 @@ | ||
Type: `hashLength` | ||
Type: `hashLength` | ||
Default: `8` | ||
@@ -123,6 +123,6 @@ | ||
Type: `prefix` | ||
Type: `prefix` | ||
Default: `none` | ||
Prefixes matched files with a string. Useful for adding a full url path to files. | ||
Prefixes matched files with a string (use `options.transform` for more complicated scenarios). Useful for adding a full url path to files. | ||
@@ -137,2 +137,28 @@ ```js | ||
#### options.transform | ||
Type: `function (rev, source, path)` | ||
Default: `none` | ||
Specify a function to transform the reference path. Useful in instances where the local file structure does not reflect what the remote file structure will be. | ||
The function takes three arguments: | ||
- `rev` - revisioned reference path | ||
- `source` - original reference path | ||
- `path` - path to the file | ||
```js | ||
gulp.task('default', function () { | ||
gulp.src('dist/**') | ||
.pipe(revall({ | ||
transform: function (rev, source, path) { | ||
// on the remote server, image files are served from `/images` | ||
return rev.replace('/img', '/images'); | ||
} | ||
})) | ||
.pipe(gulp.dest('dist')) | ||
}); | ||
``` | ||
## Tips | ||
@@ -139,0 +165,0 @@ |
@@ -182,12 +182,31 @@ var revall = require("../index"); | ||
it ("should prefix replaced references if a prefix is supplied", function(done) { | ||
stream = revall({rootDir:'test/fixtures/config1', prefix: 'http://example.com'}) | ||
stream.on('data', function (file) { | ||
var revedReference = path.basename(tools.revFile('test/fixtures/config1/index.html')); | ||
String(file.contents).should.containEql("http://example.com/" + revedReference); | ||
done(); | ||
}); | ||
stream = revall({ | ||
rootDir:'test/fixtures/config1', | ||
prefix: 'http://example.com/' | ||
}) | ||
stream.on('data', function (file) { | ||
var revedReference = path.basename(tools.revFile('test/fixtures/config1/index.html')); | ||
String(file.contents).should.containEql("http://example.com/" + revedReference); | ||
done(); | ||
}); | ||
writeFile(); | ||
writeFile(); | ||
}); | ||
it ("should replaced references using transform if it is supplied", function(done) { | ||
stream = revall({ | ||
rootDir:'test/fixtures/config1', | ||
transform: function (reved, source, path) { | ||
return this.joinUrlPath('//images.example.com/', reved.replace('img/', '')); | ||
} | ||
}) | ||
stream.on('data', function (file) { | ||
var revedImage = path.basename(tools.revFile('test/fixtures/config1/img/image1.jpg')); | ||
String(file.contents).should.containEql("//images.example.com/" + revedImage.replace('img/', '')); | ||
done(); | ||
}); | ||
writeFile(); | ||
}); | ||
it("should resolve reference to css", function(done) { | ||
@@ -194,0 +213,0 @@ stream.on('data', function (file) { |
59
tools.js
@@ -8,5 +8,11 @@ var fs = require('fs'); | ||
var filepathRegex = /.*?(?:\'|\")([a-z0-9_\-\/\.]+?\.[a-z]{2,})(?:(?:\?|\#)[^'"]*?|)(?:\'|\").*?/ig; | ||
var filepathRegex = /.*?(?:\'|\")([a-z0-9_@\-\/\.]+?\.[a-z]{2,})(?:(?:\?|\#)[^'"]*?|)(?:\'|\").*?/ig; | ||
var fileMap = {}; | ||
var joinUrlPath = function (prefix, path) { | ||
prefix = prefix.replace(/\/$/, ''); | ||
path = path.replace(/^\//, ''); | ||
return [ prefix, path ].join('/'); | ||
} | ||
// Taken from gulp-rev: https://github.com/sindresorhus/gulp-rev | ||
@@ -31,2 +37,3 @@ var md5 = function (str) { | ||
var revFile = function (filePath) { | ||
if (fileMap[filePath]) { | ||
@@ -46,3 +53,3 @@ return fileMap[filePath]; | ||
filename = path.basename(filePath, ext) + '.rev.' + hash + ext; | ||
} | ||
} | ||
@@ -57,4 +64,24 @@ filePathReved = path.join(path.dirname(filePath), filename); | ||
var self = this; | ||
var replaceMap = {}; | ||
var getReplacement = function (source, fullpath, relative) { | ||
if (fs.existsSync(fullpath)) { | ||
var newPath = path.join(path.dirname(source), path.basename(self.revFile(fullpath))); | ||
if (options.transform) { | ||
newPath = options.transform.call(self, newPath, source, fullpath); | ||
} else if (!relative && options.prefix) { | ||
newPath = self.joinUrlPath(options.prefix, newPath); | ||
} | ||
var msg = relative ? 'relative' : 'root'; | ||
gutil.log('gulp-rev-all:', 'Found', msg, 'reference [', source, '] -> [', newPath, ']'); | ||
return newPath; | ||
} | ||
return false; | ||
}; | ||
gutil.log('gulp-rev-all:', 'Finding references in [', file.path, ']'); | ||
@@ -65,26 +92,15 @@ // Create a map of file references and their proper revisioned name | ||
while (result = filepathRegex.exec(contents)) { | ||
var source = result[1]; | ||
// Skip if we've already resolved this reference | ||
if (typeof replaceMap[result[1]] !== 'undefined') continue; | ||
replaceMap[result[1]] = false; | ||
if (typeof replaceMap[source] !== 'undefined') continue; | ||
replaceMap[source] = false; | ||
// In the case where the referenced file is relative to the base path | ||
var fullpath = path.join(options.rootDir, result[1]); | ||
if (fs.existsSync(fullpath)) { | ||
newPath = path.join(path.dirname(result[1]), path.basename(revFile(fullpath))); | ||
if (options.prefix) { | ||
newPath = options.prefix + newPath; | ||
} | ||
replaceMap[result[1]] = newPath; | ||
gutil.log('gulp-rev-all:', 'Found root reference [', result[1], '] -> [', replaceMap[result[1]], ']'); | ||
continue; | ||
} | ||
var fullpath = path.join(options.rootDir, source); | ||
if (replaceMap[source] = getReplacement(source, fullpath)) continue; | ||
// In the case where the file referenced is relative to the file being processed | ||
var fullpath = path.join(path.dirname(file.path), 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 relative reference [', result[1], '] -> [', replaceMap[result[1]], ']'); | ||
continue; | ||
} | ||
fullpath = path.join(path.dirname(file.path), source); | ||
replaceMap[source] = getReplacement(source, fullpath, true); | ||
@@ -104,2 +120,3 @@ } | ||
return { | ||
joinUrlPath: joinUrlPath, | ||
revFile: revFile, | ||
@@ -106,0 +123,0 @@ revReferencesInFile: revReferencesInFile, |
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
470437
457
168