-
Notifications
You must be signed in to change notification settings - Fork 62
/
index.js
111 lines (88 loc) · 2.95 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
105
106
107
108
109
110
111
'use strict';
module.exports = plugin;
var path = require('path');
var PluginError = require('plugin-error');
var through = require('through2');
var utils = require('./utils');
function plugin(options) {
var renames = [];
var cache = [];
options = options || {};
if (typeof options.canonicalUris === 'undefined') {
options.canonicalUris = true;
}
options.prefix = options.prefix || '';
options.replaceInExtensions = options.replaceInExtensions || ['.js', '.css', '.html', '.hbs'];
return through.obj(function collectRevs(file, enc, cb) {
if (file.isNull()) {
this.push(file);
return cb();
}
if (file.isStream()) {
this.emit('error', new PluginError('gulp-rev-replace', 'Streaming not supported'));
return cb();
}
// Collect renames from reved files.
if (file.revOrigPath) {
renames.push({
unreved: fmtPath(file.revOrigBase, file.revOrigPath),
reved: options.prefix + fmtPath(file.base, file.path)
});
}
if (options.replaceInExtensions.indexOf(path.extname(file.path)) > -1) {
// file should be searched for replaces
cache.push(file);
} else {
// nothing to do with this file
this.push(file);
}
cb();
}, function replaceInFiles(cb) {
var stream = this;
if (options.manifest) {
// Read manifest file for the list of renames.
options.manifest.on('data', function (file) {
var manifest = JSON.parse(file.contents.toString());
Object.keys(manifest).forEach(function (srcFile) {
renames.push({
unreved: canonicalizeUri(srcFile),
reved: options.prefix + canonicalizeUri(manifest[srcFile])
});
});
});
options.manifest.on('end', replaceContents);
}
else {
replaceContents();
}
function replaceContents() {
renames = renames.sort(utils.byLongestUnreved);
// Once we have a full list of renames, search/replace in the cached
// files and push them through.
cache.forEach(function replaceInFile(file) {
var contents = file.contents.toString();
renames.forEach(function replaceOnce(rename) {
var unreved = options.modifyUnreved ? options.modifyUnreved(rename.unreved) : rename.unreved;
var reved = options.modifyReved ? options.modifyReved(rename.reved) : rename.reved;
contents = contents.split(unreved).join(reved);
if (options.prefix) {
contents = contents.split('/' + options.prefix).join(options.prefix + '/');
}
});
file.contents = new Buffer(contents);
stream.push(file);
});
cb();
}
});
function fmtPath(base, filePath) {
var newPath = path.relative(base, filePath);
return canonicalizeUri(newPath);
}
function canonicalizeUri(filePath) {
if (path.sep !== '/' && options.canonicalUris) {
filePath = filePath.split(path.sep).join('/');
}
return filePath;
}
}