Skip to content
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

basePath & regExp now can be configured #9

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
110 changes: 63 additions & 47 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,80 @@ var gutil = require('gulp-util');
var PluginError = gutil.PluginError;
var map = require('event-stream').map;

var FILE_DECL = /(?:href=|src=|url\()['|"]([^\s>"']+?)\?rev=([^\s>"']+?)['|"]/gi;
var revPlugin = function revPlugin(params) {

var revPlugin = function revPlugin() {
var defaultParams = {
basePath: null,
regExp: /(?:href=|src=|url\()['|"]([^\s>"']+?)\?rev=(@@hash)['|"]/gi
};
params = extend({}, defaultParams, params);

return map(function(file, cb) {

var contents;
var lines;
var i, length;
var line;
var groups;
var dependencyPath;
var data, hash;

if(!file) {
throw new PluginError('gulp-rev-append', 'Missing file option for gulp-rev-append.');
}

if(!file.contents) {
throw new PluginError('gulp-rev-append', 'Missing file.contents required for modifying files using gulp-rev-append.');
function extend(target) {
var sources = [].slice.call(arguments, 1);
sources.forEach(function (source) {
for (var prop in source) {
target[prop] = source[prop];
}
});
return target;
}

contents = file.contents.toString();
lines = contents.split('\n');
length = lines.length;
return map(function (file, cb) {
var contents;
var lines;
var i, length;
var line;
var groups;
var dependencyPath;
var data, hash;

for(i = 0; i < length; i++) {
line = lines[i];
groups = FILE_DECL.exec(line);
if(groups && groups.length > 1) {
// are we an "absoulte path"? (e.g. /js/app.js)
var normPath = path.normalize(groups[1]);
if (normPath.indexOf(path.sep) === 0) {
dependencyPath = path.join(file.base, normPath);
}
else {
dependencyPath = path.resolve(path.dirname(file.path), normPath);
if (!file) {
throw new PluginError('gulp-rev-append', 'Missing file option for gulp-rev-append.');
}

try {
data = fs.readFileSync(dependencyPath);
hash = crypto.createHash('md5');
hash.update(data.toString(), 'utf8');
line = line.replace(groups[2], hash.digest('hex'));
if (!file.contents) {
throw new PluginError('gulp-rev-append', 'Missing file.contents required for modifying files using gulp-rev-append.');
}
catch(e) {
// fail silently.

contents = file.contents.toString();
lines = contents.split('\n');
length = lines.length;

for (i = 0; i < length; i++) {
line = lines[i];
groups = params.regExp.exec(line);
if (groups && groups.length > 1) {
// are we an "absoulte path"? (e.g. /js/app.js)
var normPath = path.normalize(groups[1]);
if (normPath.indexOf(path.sep) === 0) { //we have absolute path
if (params.basePath !== null) {
dependencyPath = path.join(params.basePath, normPath);
} else {
dependencyPath = path.join(file.base, normPath);
}
}
else {
dependencyPath = path.resolve(path.dirname(file.path), normPath);
}

try {
data = fs.readFileSync(dependencyPath);
hash = crypto.createHash('md5');
hash.update(data.toString(), 'utf8');
line = line.replace(groups[2], hash.digest('hex'));
} catch (e) {
// fail silently.
}
}
lines[i] = line;
params.regExp.lastIndex = 0;
}
}
lines[i] = line;
FILE_DECL.lastIndex = 0;
}

file.contents = new Buffer(lines.join('\n'));
cb(null, file);
file.contents = new Buffer(lines.join('\n'));
cb(null, file);

});
});

};

module.exports = revPlugin;
module.exports = revPlugin;