forked from jamesknelson/gulp-rev-replace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
104 lines (84 loc) · 3.57 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
'use strict';
var path = require('path');
var gutil = require('gulp-util');
var through = require('through2');
function relPath(base, filePath) {
var newPath = filePath.replace(base, '');
if (filePath !== newPath && newPath[0] === path.sep) {
return newPath.substr(1);
} else {
return newPath;
}
}
var plugin = function(options) {
var renames = {};
var cache = [];
options = options || {};
if ( typeof(options.debug) === 'undefined' ) options.debug = false;
if ( typeof(options.canonicalUris) === 'undefined' ) options.canonicalUris = true;
if ( typeof(options.skipBaseFiles) === 'undefined' ) options.skipBaseFiles = true;
options.replaceBasePath = options.replaceBasePath || '';
options.replaceInExtensions = options.replaceInExtensions || ['.js', '.html', '.hbs'];
options.replaceSansExtensions = options.replaceSansExtensions || ['.js'];
var sansExt = new RegExp('\.(?:'+options.replaceSansExtensions.join().replace('.','')+')$', 'i');
var revOrigBaseTruncated;
var baseTruncated;
function fmtPath(base, filePath) {
var newPath = relPath(base, filePath);
if (path.sep !== '/' && options.canonicalUris) {
newPath = newPath.split(path.sep).join('/');
}
return newPath;
}
return through.obj(function(file, enc, cb) {
if (file.isNull()) {
this.push(file);
return cb();
}
if (file.isStream()) {
this.emit('error', new gutil.PluginError('gulp-rev-replace-amd', 'Streaming not supported'));
return cb();
}
// Collect renames from reved files.
if (file.revOrigPath) {
renames[fmtPath(file.revOrigBase + options.replaceBasePath, file.revOrigPath)] = fmtPath(file.base + options.replaceBasePath, file.path);
// Also replace reved file references lacking certain extensions.
if (options.replaceSansExtensions.indexOf(path.extname(file.path)) !== -1) {
revOrigBaseTruncated = fmtPath(file.revOrigBase + options.replaceBasePath, file.revOrigPath.replace(sansExt, ''));
if ( !options.skipBaseFiles || revOrigBaseTruncated.split('/').length > 1 ) {
baseTruncated = fmtPath(file.base + options.replaceBasePath, file.path.replace(sansExt, ''));
renames['\''+revOrigBaseTruncated+'\''] = '\''+baseTruncated+'\'';
renames['"'+revOrigBaseTruncated+'"'] = '"'+baseTruncated+'"';
if ( options.debug ) {
console.log(revOrigBaseTruncated + ' => ' + baseTruncated);
}
}
}
}
if (options.replaceInExtensions.indexOf(path.extname(file.path)) > -1) {
// Cache file to perform replacements in it later.
cache.push(file);
} else {
this.push(file);
}
cb();
}, function(cb) {
// Once we have a full list of renames, search/replace in the cached
// files and push them through.
var file;
var contents;
for (var i = 0, ii = cache.length; i !== ii; i++) {
file = cache[i];
contents = file.contents.toString();
for (var rename in renames) {
if (renames.hasOwnProperty(rename)) {
contents = contents.split(rename).join(renames[rename]);
}
}
file.contents = new Buffer(contents);
this.push(file);
}
cb();
});
};
module.exports = plugin;