-
Notifications
You must be signed in to change notification settings - Fork 62
/
test.js
540 lines (482 loc) · 16.4 KB
/
test.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/* global it describe */
'use strict';
var assert = require('assert');
var filter = require('gulp-filter');
var Vinyl = require('vinyl');
var path = require('path');
var rev = require('gulp-rev');
var es = require('event-stream');
var revReplace = require('./index');
var utils = require('./utils');
var svgFileBody = '<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg xmlns="http://www.w3.org/2000/svg"></svg>';
var cssFileBody = '@font-face { font-family: \'test\'; src: url(\'/fonts/font.svg\'); }\nbody { color: red; }';
var jsFileBody = 'console.log("Hello world"); //# sourceMappingURL=app.js.map';
var htmlFileBody = '<html><head><link rel="stylesheet" href="/css/style.css" /></head><body><img src="images/image.png" /><img src="images/image.png" /></body></html>';
it('should by default replace filenames in .css and .html files', function (cb) {
var filesToRevFilter = filter(['**/*.css', '**/*.svg', '**/*.png'], {restore: true});
var stream = filesToRevFilter
.pipe(rev())
.pipe(filesToRevFilter.restore)
.pipe(revReplace());
var fileCount = 0;
var unreplacedCSSFilePattern = /style\.css/;
var unreplacedSVGFilePattern = /font\.svg/;
var unreplacedPNGFilePattern = /image\.png/;
stream.on('data', function(file) {
var contents = file.contents.toString();
var extension = path.extname(file.path);
if (extension === '.html') {
assert(
!unreplacedCSSFilePattern.test(contents),
'The renamed CSS file\'s name should be replaced'
);
assert(
!unreplacedPNGFilePattern.test(contents),
'The renamed PNG file\'s name should be globally replaced'
);
} else if (extension === '.css') {
assert(
!unreplacedSVGFilePattern.test(contents),
'The renamed SVG file\'s name should be replaced'
);
} else if (extension === '.svg') {
assert(
contents === svgFileBody,
'The SVG file should not be modified'
);
}
fileCount++;
});
stream.on('end', function() {
assert.equal(fileCount, 4, 'Only four files should pass through the stream');
cb();
});
filesToRevFilter.write(new Vinyl({
path: path.join('css', 'style.css'),
contents: new Buffer(cssFileBody)
}));
filesToRevFilter.write(new Vinyl({
path: path.join('fonts', 'font.svg'),
contents: new Buffer(svgFileBody)
}));
filesToRevFilter.write(new Vinyl({
path: 'images/image.png',
contents: new Buffer('PNG')
}));
filesToRevFilter.write(new Vinyl({
path: 'index.html',
contents: new Buffer(htmlFileBody)
}));
filesToRevFilter.end();
});
it('should not replace filenames in extensions not in replaceInExtensions', function (cb) {
var filesToRevFilter = filter(['**/*.css'], {restore: true});
var stream = filesToRevFilter
.pipe(rev())
.pipe(filesToRevFilter.restore)
.pipe(revReplace({replaceInExtensions: ['.svg']}));
var unreplacedCSSFilePattern = /style\.css/;
stream.on('data', function(file) {
var contents = file.contents.toString();
var extension = path.extname(file.path);
if (extension === '.html') {
assert(
unreplacedCSSFilePattern.test(contents),
'The renamed CSS file\'s name should not be replaced'
);
}
});
stream.on('end', function() {
cb();
});
filesToRevFilter.write(new Vinyl({
path: 'css\\style.css',
contents: new Buffer(cssFileBody)
}));
filesToRevFilter.write(new Vinyl({
path: 'index.html',
contents: new Buffer(htmlFileBody)
}));
filesToRevFilter.end();
});
it('should not canonicalize URIs when option is off', function (cb) {
var filesToRevFilter = filter(['**/*.css'], {restore: true});
var stream = filesToRevFilter
.pipe(rev())
.pipe(filesToRevFilter.restore)
.pipe(revReplace({canonicalUris: false}));
var unreplacedCSSFilePattern = /style\.css/;
stream.on('data', function(file) {
var contents = file.contents.toString();
var extension = path.extname(file.path);
if (extension === '.html') {
assert(
unreplacedCSSFilePattern.test(contents),
'The renamed CSS file\'s name should not be replaced'
);
}
});
stream.on('end', function() {
cb();
});
filesToRevFilter.write(new Vinyl({
path: 'css\\style.css',
contents: new Buffer(cssFileBody)
}));
filesToRevFilter.write(new Vinyl({
path: 'index.html',
contents: new Buffer(htmlFileBody)
}));
filesToRevFilter.end();
});
it('should add prefix to path', function (cb) {
var filesToRevFilter = filter(['**/*.css'], {restore: true});
var stream = filesToRevFilter
.pipe(rev())
.pipe(filesToRevFilter.restore)
.pipe(revReplace({prefix: 'http://example.com'}));
var replacedCSSFilePattern = /"http:\/\/example\.com\/css\/style-[^\.]+\.css"/;
stream.on('data', function(file) {
var contents = file.contents.toString();
var extension = path.extname(file.path);
if (extension === '.html') {
assert(
replacedCSSFilePattern.test(contents),
'The prefix should be added in to the file url'
);
}
});
stream.on('end', function() {
cb();
});
filesToRevFilter.write(new Vinyl({
path: 'css/style.css',
contents: new Buffer(cssFileBody)
}));
filesToRevFilter.write(new Vinyl({
path: 'index.html',
contents: new Buffer(htmlFileBody)
}));
filesToRevFilter.end();
});
it('should stop at first longest replace', function(cb) {
var jsFileBody = 'var loadFile = "nopestyle.css"';
var replacedJsFileBody = 'var loadFile = "nopestyle-19269897ba.css"';
var filesToRevFilter = filter(['**/*.css'], {restore: true});
var stream = filesToRevFilter
.pipe(rev())
.pipe(filesToRevFilter.restore)
.pipe(revReplace({canonicalUris: false}));
stream.on('data', function(file) {
if (file.path === 'script.js') {
assert.equal(
file.contents.toString(),
replacedJsFileBody,
'It should have replaced using the longest string match (nopestyle)'
);
}
});
stream.on('end', function() {
cb();
});
filesToRevFilter.write(new Vinyl({
path: 'style.css',
contents: new Buffer(cssFileBody)
}));
filesToRevFilter.write(new Vinyl({
path: 'nopestyle.css',
contents: new Buffer('boooooo')
}));
filesToRevFilter.write(new Vinyl({
path: 'script.js',
contents: new Buffer(jsFileBody)
}));
filesToRevFilter.end();
});
describe('manifest option', function () {
it('should replace filenames from manifest files', function (cb) {
var manifest = es.readArray([
new Vinyl({
path: '/project/rev-manifest.json',
contents: new Buffer(JSON.stringify({
'/css/style.css': '/css/style-12345.css'
}))
}),
new Vinyl({
path: '/project/rev-image-manifest.json',
contents: new Buffer(JSON.stringify({
'images/image.png': 'images/image-12345.png',
'/fonts/font.svg': '/fonts/font-12345.svg'
}))
})
]);
var stream = revReplace({manifest: manifest});
var replacedCSSFilePattern = /style-12345\.css/;
var replacedSVGFilePattern = /font-12345\.svg/;
var replacedPNGFilePattern = /image-12345\.png/;
stream.on('data', function(file) {
var contents = file.contents.toString();
var extension = path.extname(file.path);
if (extension === '.html') {
assert(
replacedCSSFilePattern.test(contents),
'The renamed CSS file\'s name should be replaced'
);
assert(
replacedPNGFilePattern.test(contents),
'The renamed PNG file\'s name should be globally replaced'
);
} else if (extension === '.css') {
assert(
replacedSVGFilePattern.test(contents),
'The renamed SVG file\'s name should be replaced'
);
} else if (extension === '.svg') {
assert(
contents === svgFileBody,
'The SVG file should not be modified'
);
}
});
stream.on('end', function() {
cb();
});
stream.write(new Vinyl({
path: path.join('css', 'style.css'),
contents: new Buffer(cssFileBody)
}));
stream.write(new Vinyl({
path: path.join('fonts', 'font.svg'),
contents: new Buffer(svgFileBody)
}));
stream.write(new Vinyl({
path: 'index.html',
contents: new Buffer(htmlFileBody)
}));
stream.end();
});
it('should add prefix to path', function (cb) {
var manifest = es.readArray([
new Vinyl({
path: '/project/rev-manifest.json',
contents: new Buffer(JSON.stringify({
'/css/style.css': '/css/style-12345.css'
}))
})
]);
var stream = revReplace({prefix: 'http://example.com', manifest: manifest});
var replacedCSSFilePattern = /"http:\/\/example\.com\/css\/style-12345\.css"/;
stream.on('data', function(file) {
var contents = file.contents.toString();
var extension = path.extname(file.path);
if (extension === '.html') {
assert(
replacedCSSFilePattern.test(contents),
'The prefix should be added in to the file url'
);
}
});
stream.on('end', function() {
cb();
});
stream.write(new Vinyl({
path: 'index.html',
contents: new Buffer(htmlFileBody)
}));
stream.end();
});
});
describe('utils.byLongestUnreved', function() {
it('should arrange renames from longest to shortest', function() {
var renames = [{
unreved: 'data/favicon.ico',
reved: 'data/favicon-15d0f308.ico'
}, {
unreved: 'fonts/FontAwesome.otf',
reved: 'fonts/FontAwesome-0b462f5c.otf'
}, {
unreved: 'fonts/fontawesome-webfont.eot',
reved: 'fonts/fontawesome-webfont-f7c2b4b7.eot'
}, {
unreved: 'fonts/fontawesome-webfont.svg',
reved: 'fonts/fontawesome-webfont-29800836.svg'
}, {
unreved: 'fonts/fontawesome-webfont.ttf',
reved: 'fonts/fontawesome-webfont-706450d7.ttf'
}, {
unreved: 'fonts/fontawesome-webfont.woff',
reved: 'fonts/fontawesome-webfont-d9ee23d5.woff'
}, {
unreved: 'fonts/fontawesome-webfont.woff2',
reved: 'fonts/fontawesome-webfont-97493d3f.woff2'
}, {
unreved: 'fonts/glyphicons-halflings-regular.eot',
reved: 'fonts/glyphicons-halflings-regular-f4769f9b.eot'
}, {
unreved: 'fonts/glyphicons-halflings-regular.svg',
reved: 'fonts/glyphicons-halflings-regular-89889688.svg'
}, {
unreved: 'fonts/glyphicons-halflings-regular.ttf',
reved: 'fonts/glyphicons-halflings-regular-e18bbf61.ttf'
}, {
unreved: 'fonts/glyphicons-halflings-regular.woff',
reved: 'fonts/glyphicons-halflings-regular-fa277232.woff'
}, {
unreved: 'fonts/glyphicons-halflings-regular.woff2',
reved: 'fonts/glyphicons-halflings-regular-448c34a5.woff2'
}, {
unreved: 'images/busy-indicator-lg-dark.gif',
reved: 'images/busy-indicator-lg-dark-8f372b90.gif'
}, {
unreved: 'images/busy-indicator-lg-light.gif',
reved: 'images/busy-indicator-lg-light-25050875.gif'
}, {
unreved: 'images/busy-indicator-sm-light.gif',
reved: 'images/busy-indicator-sm-light-b464283c.gif'
}, {
unreved: 'images/footer-logo.png',
reved: 'images/footer-logo-df3d73ed.png'
}, {
unreved: 'images/icon-progress-indicator.png',
reved: 'images/icon-progress-indicator-b342c570.png'
}, {
unreved: 'images/scripps-swirl.png',
reved: 'images/scripps-swirl-65b0319e.png'
}, {
unreved: 'images/sprite.png',
reved: 'images/sprite-9e275087.png'
}, {
unreved: 'images/sprite_2x.png',
reved: 'images/sprite_2x-c7af344b.png'
}, {
unreved: 'scripts/app.js', reved: 'scripts/app-137924b0.js'
}, {
unreved: 'styles/app.css', reved: 'styles/app-4858235a.css'
}, {
unreved: 'env/deploy/features.json',
reved: 'env/deploy/features-2a501331.json'
}];
var expected = [{
unreved: 'fonts/glyphicons-halflings-regular.woff2',
reved: 'fonts/glyphicons-halflings-regular-448c34a5.woff2'
}, {
unreved: 'fonts/glyphicons-halflings-regular.woff',
reved: 'fonts/glyphicons-halflings-regular-fa277232.woff'
}, {
unreved: 'fonts/glyphicons-halflings-regular.svg',
reved: 'fonts/glyphicons-halflings-regular-89889688.svg'
}, {
unreved: 'fonts/glyphicons-halflings-regular.ttf',
reved: 'fonts/glyphicons-halflings-regular-e18bbf61.ttf'
}, {
unreved: 'fonts/glyphicons-halflings-regular.eot',
reved: 'fonts/glyphicons-halflings-regular-f4769f9b.eot'
}, {
unreved: 'images/busy-indicator-lg-light.gif',
reved: 'images/busy-indicator-lg-light-25050875.gif'
}, {
unreved: 'images/busy-indicator-sm-light.gif',
reved: 'images/busy-indicator-sm-light-b464283c.gif'
}, {
unreved: 'images/icon-progress-indicator.png',
reved: 'images/icon-progress-indicator-b342c570.png'
}, {
unreved: 'images/busy-indicator-lg-dark.gif',
reved: 'images/busy-indicator-lg-dark-8f372b90.gif'
}, {
unreved: 'fonts/fontawesome-webfont.woff2',
reved: 'fonts/fontawesome-webfont-97493d3f.woff2'
}, {
unreved: 'fonts/fontawesome-webfont.woff',
reved: 'fonts/fontawesome-webfont-d9ee23d5.woff'
}, {
unreved: 'fonts/fontawesome-webfont.eot',
reved: 'fonts/fontawesome-webfont-f7c2b4b7.eot'
}, {
unreved: 'fonts/fontawesome-webfont.ttf',
reved: 'fonts/fontawesome-webfont-706450d7.ttf'
}, {
unreved: 'fonts/fontawesome-webfont.svg',
reved: 'fonts/fontawesome-webfont-29800836.svg'
}, {
unreved: 'env/deploy/features.json',
reved: 'env/deploy/features-2a501331.json'
}, {
unreved: 'images/scripps-swirl.png',
reved: 'images/scripps-swirl-65b0319e.png'
}, {
unreved: 'images/footer-logo.png',
reved: 'images/footer-logo-df3d73ed.png'
}, {
unreved: 'fonts/FontAwesome.otf',
reved: 'fonts/FontAwesome-0b462f5c.otf'
}, {
unreved: 'images/sprite_2x.png',
reved: 'images/sprite_2x-c7af344b.png'
}, {
unreved: 'images/sprite.png',
reved: 'images/sprite-9e275087.png'
}, {
unreved: 'data/favicon.ico',
reved: 'data/favicon-15d0f308.ico'
}, {
unreved: 'scripts/app.js', reved: 'scripts/app-137924b0.js'
}, {
unreved: 'styles/app.css', reved: 'styles/app-4858235a.css'
}];
assert.deepEqual(renames.sort(utils.byLongestUnreved), expected);
});
});
describe('modifyUnreved and modifyReved options', function() {
it('should modify the names of reved and un-reved files', function(cb) {
var manifest = es.readArray([
new Vinyl({
path: '/project/rev-manifest.json',
contents: new Buffer(JSON.stringify({
'js/app.js.map': 'js/app-12345.js.map',
'css/style.css': 'css/style-12345.css'
}))
})
]);
function replaceJsIfMap(filename) {
if (filename.indexOf('.map') > -1) {
return filename.replace('js/', '');
}
return filename;
}
var stream = revReplace({
manifest: manifest,
modifyUnreved: replaceJsIfMap,
modifyReved: replaceJsIfMap
});
var replacedJSMapFilePattern = /sourceMappingURL\=app-12345\.js\.map/;
var replacedCSSFilePattern = /css\/style-12345\.css/;
stream.on('data', function(file) {
var contents = file.contents.toString();
var extension = path.extname(file.path);
if (extension === '.js') {
assert(
replacedJSMapFilePattern.test(contents),
'The source map has been correctly replaced using modifyReved and modifyUnreved'
);
} else if (extension === '.html') {
assert(
replacedCSSFilePattern.test(contents),
'The renamed CSS file\'s name should be replaced and not affected by modifyReved or modifyUnreved'
);
}
});
stream.on('end', function() {
cb();
});
stream.write(new Vinyl({
path: path.join('js', 'app.js'),
contents: new Buffer(jsFileBody)
}));
stream.write(new Vinyl({
path: 'index.html',
contents: new Buffer(htmlFileBody)
}));
stream.end();
});
});