-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
278 lines (232 loc) · 7.63 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
var _, path, through, gutil, chalk, istextorbinary;
module.exports = function (options) {
var assets = [];
// Lazy load dependencies
if (!_) {
_ = require('lodash');
}
if (!path) {
path = require('path');
}
if (!through) {
through = require('through2');
}
// Set default options
options = options || {};
options = _.assign({
regex: /(?:url\(["']?(.*?)['"]?\)|src=["'](.*?)['"]|src=([^\s\>]+)(?:\>|\s)|href=["'](.*?)['"]|href=([^\s\>]+)(?:\>|\s))/g,
debug: 0,
patterns: [],
filterExtensions: [],
skipExtensions: [],
addSrcPrefix: '',
addDestPrefix: '/',
skipUnmentioned: false,
resolveNonRev: true
}, options);
// Set current working directory
if (!options.cwd) {
options.cwd = path.resolve('.');
} else {
options.cwd = path.resolve(options.cwd);
}
// Ensure arrays
if (_.isString(options.patterns)) {
options.patterns = [options.patterns];
}
if (_.isString(options.filterExtensions)) {
options.filterExtensions = [options.filterExtensions];
}
if (_.isString(options.skipExtensions)) {
options.skipExtensions = [options.skipExtensions];
}
// Make patterns relative
for (var i = 0; i < options.patterns.length; i++) {
options.patterns[i] = path.relative(options.cwd, options.patterns[i]);
}
// Resolve base
if (options.base) {
options.base = path.resolve(options.base);
}
// Remove trailing slash
if (options.stripSrcPrefix) {
options.stripSrcPrefix = options.stripSrcPrefix.replace(/^\//, '');
}
if (options.stripDestPrefix) {
options.stripDestPrefix = options.stripDestPrefix.replace(/^\//, '');
}
function normalize(basePath, fullPath) {
if (basePath === fullPath.slice(0, basePath.length)) {
return path.relative(basePath, fullPath);
} else {
return path.relative(options.cwd, fullPath);
}
}
//noinspection JSUnusedLocalSymbols
function transform(file, enc, cb) {
var filePath;
if (file.isStream()) {
if (!gutil) {
gutil = require('gulp-util');
}
cb(new gutil.PluginError('gulp-revsolve', 'Streaming is not supported'));
return;
}
assets.push(file);
// Pass through empty files
if (file.isNull()) {
file.isTextFile = false;
cb(null, file);
return;
}
// Get original file path
if (file.revOrigPath) {
filePath = file.revOrigPath;
} else {
filePath = file.path;
}
// Detect text files
if (!istextorbinary) {
istextorbinary = require('istextorbinary');
}
istextorbinary.isText(filePath, file.contents, function(err, result) {
/* istanbul ignore if */
if (err) {
if (!gutil) {
gutil = require('gulp-util');
}
cb(new gutil.PluginError('gulp-revsolve', 'Wrong content type of file `' + file.path + '`. ' + err));
return;
}
file.isTextFile = result;
// Filter text extensions
if (options.filterExtensions.length > 0) {
if (!_.contains(options.filterExtensions, path.extname(file.path).substr(1))) {
file.isTextFile = false;
}
}
// Skip text extensions
if (options.skipExtensions.length > 0) {
if (_.contains(options.skipExtensions, path.extname(file.path).substr(1))) {
file.isTextFile = false;
}
}
cb();
});
}
function flush(cb) {
var self = this;
// Filter text files
var sources = assets.filter(function (file) {
return file.isTextFile;
});
_.forEach(sources, function (file) {
var content = String(file.contents);
content = content.replace(options.regex, function (str) {
var items, found, url, match, suffix,
oldPath, newPath, absolutePath, relativePath, replaced, matched;
items = _.values(arguments);
items.shift();
items = _.compact(items);
found = _.first(items);
// No regex match found (additional safe guard)
/* istanbul ignore if */
if (!_.isString(found)) {
return str;
}
url = found.replace(/^\//, '');
suffix = '';
// Remove suffix
match = url.split(/[#\?]/);
if (match) {
suffix = url.replace(match[0], '');
url = match[0];
}
// Strip prefix
if (options.stripSrcPrefix && options.stripSrcPrefix === url.slice(0, options.stripSrcPrefix.length)) {
url = url.substring(options.stripSrcPrefix.length, url.length);
}
// Add prefix
url = path.join(options.addSrcPrefix, url);
// Replace in assets
_.forEach(assets, function (asset) {
// Skip file itself
if (asset === file) {
return;
}
// Handle non rev files
if (options.resolveNonRev && !asset.revOrigPath) {
asset.revOrigPath = asset.path;
asset.revOrigBase = asset.base;
} else if (!asset.revOrigPath) {
return;
}
oldPath = normalize(options.cwd, asset.revOrigPath);
newPath = normalize(options.base || path.resolve(asset.base), asset.path);
// Filenames must match
if (path.basename(oldPath) !== path.basename(url)) {
return;
}
absolutePath = oldPath;
relativePath = path.relative(path.dirname(file.revOrigPath || file.path), asset.revOrigPath);
// Match path with url
matched = absolutePath === url || relativePath === url;
if (!matched) {
var minimatch = require('minimatch');
for (var i = 0; i < options.patterns.length; i++) {
if (minimatch(absolutePath, path.join(options.patterns[i], url), {matchBase: true})) {
matched = true;
break;
}
}
}
/* istanbul ignore else */
if (matched) {
if (options.skipUnmentioned) {
self.push(asset);
}
// Strip prefix
newPath = newPath.split(path.sep).join('/');
if (options.stripDestPrefix && options.stripDestPrefix === newPath.slice(0, options.stripDestPrefix.length)) {
newPath = newPath.substring(options.stripDestPrefix.length, newPath.length);
}
replaced = path.join(options.addDestPrefix, newPath).split(path.sep).join('/') + suffix;
str = str.replace(found, replaced);
/* istanbul ignore if */
if (options.debug === 1 || options.debug === 3) {
if (!gutil) {
gutil = require('gulp-util');
}
if (!chalk) {
chalk = require('chalk');
}
gutil.log(chalk.magenta(normalize(file.base, file.path)) + ':');
gutil.log(chalk.green(found) + ' ' + chalk.yellow('->') + ' ' + chalk.green(replaced));
}
} else if (options.debug === 2 || options.debug === 3) {
if (!gutil) {
gutil = require('gulp-util');
}
if (!chalk) {
chalk = require('chalk');
}
gutil.log(chalk.red(normalize(file.base, file.path)) + ':');
gutil.log(chalk.red(url) + ' ' + chalk.red('doesn\'t match') + ' ' + chalk.red(absolutePath) + ', ' + chalk.red(relativePath));
}
});
return str;
});
file.contents = new Buffer(content);
if (options.skipUnmentioned) {
self.push(file);
}
});
if (!options.skipUnmentioned) {
_.forEach(assets, function (asset) {
self.push(asset);
});
}
cb();
}
return through.obj(transform, flush);
};