forked from thomasvantuycom/gulp-rev-rewrite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
118 lines (95 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
112
113
114
115
116
117
118
'use strict';
const path = require('path');
const PluginError = require('plugin-error');
const through = require('through2');
const replace = require('./lib/replace');
module.exports = function (options) {
let renames = [];
const cache = [];
options = Object.assign({canonicalUris: true, replaceInExtensions: ['.js', '.css', '.html', '.hbs']}, options);
return through.obj(function (file, enc, cb) {
if (file.isNull()) {
this.push(file);
return cb();
}
if (file.isStream()) {
this.emit('error', new PluginError('gulp-rev-rewrite', 'Streaming not supported'));
return cb();
}
// Collect renames from reved files.
if (file.revOrigPath) {
renames.push({
unreved: fmtPath(file.revOrigBase, file.revOrigPath),
reved: fmtPath(file.base, file.path)
});
}
if (options.replaceInExtensions.includes(path.extname(file.path))) {
// File should be searched for replaces
cache.push(file);
} else {
// Nothing to do with this file
this.push(file);
}
cb();
}, function (cb) {
const stream = this;
if (options.manifest) {
// Read manifest file for the list of renames.
options.manifest.on('data', file => {
const manifest = JSON.parse(file.contents.toString());
Object.keys(manifest).forEach(srcFile => {
renames.push({
unreved: canonicalizeUri(srcFile),
reved: canonicalizeUri(manifest[srcFile])
});
});
});
options.manifest.on('end', replaceContents);
} else {
replaceContents();
}
function replaceContents() {
renames = renames.map(entry => {
const {unreved} = entry;
const reved = options.prefix ? prefixPath(entry.reved, options.prefix) : entry.reved;
return {unreved, reved};
});
// Once we have a full list of renames, search/replace in the cached
// files and push them through.
cache.forEach(file => {
const modifiedRenames = renames.map(entry => {
const unreved = options.modifyUnreved ? options.modifyUnreved(entry.unreved, file) : entry.unreved;
const reved = options.modifyReved ? options.modifyReved(entry.reved, file) : entry.reved;
return {unreved, reved};
});
const contents = file.contents.toString();
let newContents = replace(contents, modifiedRenames);
if (options.prefix) {
newContents = newContents.split('/' + options.prefix).join(options.prefix);
}
file.contents = Buffer.from(newContents);
stream.push(file);
});
cb();
}
});
function fmtPath(base, filePath) {
const newPath = path.relative(base, filePath);
return canonicalizeUri(newPath);
}
function canonicalizeUri(filePath) {
if (options.canonicalUris) {
filePath = filePath.replace(/\\/g, '/');
}
return filePath;
}
function prefixPath(filePath, prefix) {
if (filePath.startsWith('/') && prefix.endsWith('/')) {
return `${prefix}${filePath.substr(1)}`;
}
if (!filePath.startsWith('/') && !prefix.endsWith('/')) {
return `${prefix}/${filePath}`;
}
return `${prefix}${filePath}`;
}
};