-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·387 lines (344 loc) · 13 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
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
/* eslint strict: ["error", "global"] */
/**
* shortbread is an asynchronous, non-blocking loading pattern for CSS and JavaScript resources
*
* @see https://github.com/jkphl/shortbread
*
* @author Joschi Kuphal <[email protected]> (https://github.com/jkphl)
* @copyright © 2019 Joschi Kuphal
* @license MIT https://raw.github.com/jkphl/gulp-cache-bust-meta/master/LICENSE
*/
'use strict';
const Vinyl = require('vinyl');
const crypto = require('crypto');
const path = require('path');
const fs = require('fs');
const uglify = require('uglify-js');
const through = require('through2');
const isUrl = require('is-url');
const fileProperties = ['history', 'stat', '_contents'];
/**
* Test if an object is a vinyl file
*
* @param {File|Object} file Vinyl file / object
* @return {Boolean} Object is a Vinyl file
*/
function isVinylFile(file) {
if (Vinyl.isVinyl(file)) {
return true;
}
if (!file || (typeof file !== 'object')) {
return false;
}
const properties = new Set(Object.getOwnPropertyNames(file));
if ((!properties.has('cwd') && !properties.has('_cwd'))
|| fileProperties.filter(p => !properties.has(p)).length
) {
return false;
}
const content = Object.getOwnPropertyDescriptor(file, '_contents');
return (typeof content === 'object') && content.writable && content.enumerable && content.configurable;
}
/**
* Convert a value into a value list
*
* @param {String|Array|Object} val Value
* @return {Array} Value list
*/
function makeList(val) {
if (val === null) {
return [];
}
let ret = val;
if (!Array.isArray(val)) {
if ((typeof val === 'object') && (val.constructor === Object)) {
ret = Object.keys(val).map(key => val[key]);
} else {
ret = [val];
}
}
return ret;
}
/**
* Convert a value into a list of Vinyl files
*
* @param {String|Array|Object} val Value
* @return {Array} Value list
*/
function makeVinylFileList(val) {
return makeList(val).filter(v => isVinylFile(v));
}
/**
* Convert a value into a list of regular expressions
*
* @param {String|Array|Object} val Value
* @return {Array} Regex list
*/
function makeRegexList(val) {
return makeList(val)
.filter(v => ((typeof v === 'string' && v.trim().length) || (typeof v === 'object' && v.constructor === RegExp)))
.map(v => (typeof v === 'string' ? new RegExp(v) : v));
}
/**
* Convert a value into a list of URLs
*
* @param {String|Array|Object} val Value
* @return {Array} URL list
*/
function makeUrlList(val) {
return makeList(val).filter(v => isUrl(v));
}
/**
* Create HTML fragments for assynchronously loading JavaScript and CSS resources
*
* @param {File|Array.<File>|Object.<String, File>} js [OPTIONAL] JavaScript resource(s)
* @param {File|Array.<File>|Object.<String, File>} css [OPTIONAL] CSS resource(s)
* @param {File|Array.<File>|Object.<File>} critical [OPTIONAL] Critical CSS / JS resource(s)
* @param {String} slot [OPTIONAL] Cookie slot
* @param {String} callback [OPTIONAL] Callback(s)
* @param {Object} config [OPTIONAL] Extended configuration
*/
function shortbread(js, css, critical, slot, callback, config) {
const jsFiles = makeVinylFileList(js);
const jsUrls = makeUrlList(js);
const cssFiles = makeVinylFileList(css);
const cssUrls = makeUrlList(css);
const criticalFiles = makeVinylFileList(critical);
const criticalFilePaths = criticalFiles.map(criticalFile => path.resolve(criticalFile.path));
const cookieSlot = (typeof slot === 'string') ? (slot.trim() || null) : null;
const options = Object.assign({
prefix: '',
css: ['\\.css$'],
js: ['\\.js$'],
debug: false,
}, config || {});
const callbackString = callback ? JSON.stringify(callback) : 'null';
if (typeof options.prefix !== 'string') {
options.prefix = '';
}
let initial = '';
let needsPrelink = cssFiles.length || cssUrls.length;
const result = {
initial: '',
subsequent: '',
resources: {},
hash: null,
cookie: `sb${cookieSlot ? `_${cookieSlot}` : ''}`,
};
const clientResources = {};
// Return if no resources are given
if (!jsFiles.length && !jsUrls.length && !cssFiles.length && !cssUrls.length
&& !criticalFiles.length) {
return result;
}
// 1. JavaScript resources
jsFiles.forEach((jsFile) => {
const resourceHash = shortbread.createHash(jsFile.contents.toString());
result.resources[resourceHash] = `${options.prefix}${jsFile.relative}`;
// If this resource is also registered as critical JavaScript
if (criticalFilePaths.indexOf(path.resolve(jsFile.path)) >= 0) {
needsPrelink = true;
clientResources[resourceHash] = 0;
result.initial += `<link rel="prefetch" href="${result.resources[resourceHash]}" id="${resourceHash}" as="script" onload="SHORTBREAD_INSTANCE.onloadScript(this)">`;
} else {
clientResources[resourceHash] = 1;
result.initial += `<script src="${result.resources[resourceHash]}" id="${resourceHash}" async defer onreadystatechange="SHORTBREAD_INSTANCE.onloadScript(this)" onload="SHORTBREAD_INSTANCE.onloadScript(this)"></script>`;
}
result.subsequent += `<script src="${options.prefix}${jsFile.relative}"></script>`;
});
jsUrls.forEach((jsUrl) => {
const resourceHash = shortbread.createHash(jsUrl);
result.resources[resourceHash] = jsUrl;
clientResources[resourceHash] = 1;
result.initial += `<script src="${jsUrl}" id="${resourceHash}" async defer onreadystatechange="SHORTBREAD_INSTANCE.onloadScript(this)" onload="SHORTBREAD_INSTANCE.onloadScript(this)"></script>`;
result.subsequent += `<script src="${jsUrl}"></script>`;
});
// 2.a Critical CSS & JavaScript
criticalFiles.forEach((criticalFile) => {
// Detect whether it's a JavaScript resource
for (const r of options.js) {
if (criticalFile.relative.match(r)) {
result.initial += `<script>${criticalFile.contents}</script>`;
return;
}
}
// Detect whether it's a CSS resource
for (const r of options.css) {
if (criticalFile.relative.match(r)) {
result.initial += `<style>${criticalFile.contents}</style>`;
return;
}
}
});
// 3. Initial head script
if (needsPrelink) {
initial += fs.readFileSync(require.resolve('prelink/build/prelink.min.js'));
}
if (jsFiles.length || jsUrls.length || needsPrelink) {
initial += fs.readFileSync(path.join(__dirname, `build/shortbread${options.debug ? '.debug' : ''}.js`));
}
let synchronousCSS = '';
cssFiles.forEach((cssFile) => {
const resourceHash = shortbread.createHash(cssFile.contents.toString());
result.resources[resourceHash] = `${options.prefix}${cssFile.relative}`;
clientResources[resourceHash] = 1;
result.initial += `<link rel="preload" href="${result.resources[resourceHash]}" id="${resourceHash}" as="style" onload="this.rel='stylesheet';SHORTBREAD_INSTANCE.loaded(this.id)">`;
synchronousCSS += `<link rel="stylesheet" href="${result.resources[resourceHash]}">`;
});
cssUrls.forEach((cssUrl) => {
const resourceHash = shortbread.createHash(cssUrl);
result.resources[resourceHash] = cssUrl;
clientResources[resourceHash] = 1;
result.initial += `<link rel="preload" href="${cssUrl}" id="${resourceHash}" as="style" onload="this.rel='stylesheet';SHORTBREAD_INSTANCE.loaded(this.id)">`;
synchronousCSS += `<link rel="stylesheet" href="${cssUrl}">`;
});
if (synchronousCSS.length) {
result.initial += `<noscript>${synchronousCSS}</noscript>`;
result.subsequent += synchronousCSS;
}
// Calculate the master hash
const resourceHashes = Object.keys(result.resources);
result.hash = resourceHashes.length ? shortbread.createHash(resourceHashes.join('-')) : null;
if (resourceHashes.length) {
initial += `var SHORTBREAD_INSTANCE = new Shortbread(${JSON.stringify(clientResources)}, '${result.hash}', ${JSON.stringify(cookieSlot)}, ${callbackString});`;
}
result.initial = `${initial.length ? `<script>"use strict";${uglify.minify(initial).code}</script>` : ''}${result.initial}`;
result.initial = result.initial.split('SHORTBREAD_INSTANCE').join(`sb${result.hash}`);
return result;
}
/**
* Streaming interface for shortbread
*
* @param {File|Array.<File>|Object.<File>} critical [OPTIONAL] Critical CSS / JS resource(s)
* @param {String} slot [OPTIONAL] Cookie slot (optional)
* @param {String} callback [OPTIONAL] Callback(s)
* @param {Object} config [OPTIONAL] Extended configuration
*/
shortbread.stream = function stream(critical, slot, callback, config) {
const options = Object.assign({
css: ['\\.css$'],
cssUrl: [],
js: ['\\.js$'],
jsUrl: [],
prefix: '',
initial: 'initial.html',
subsequent: 'subsequent.html',
data: false,
debug: false,
}, config || {});
options.css = makeRegexList(options.css);
options.js = makeRegexList(options.js);
options.data = !!options.data;
// Validate the critical CSS or JavaScript resource(s)
if (critical && !makeVinylFileList(critical).length) {
throw new Error('shortbread.stream: Critical resources must be single a Vinyl object, a Vinyl object array or object');
}
// Prepare the fragment paths
const cookieSlot = (typeof slot === 'string') ? (slot.trim() || '') : '';
if (cookieSlot.length) {
// Prepare the initial page load fragment
const initialExt = path.extname(options.initial);
options.initial = `${options.initial.substr(0, options.initial.length - initialExt.length)}.${cookieSlot}${initialExt}`;
// Prepare the subsequent page load fragment
const subsequentExt = path.extname(options.subsequent);
options.subsequent = `${options.subsequent.substr(0, options.subsequent.length - subsequentExt.length)}.${cookieSlot}${subsequentExt}`;
}
// Prepare the resource lists
const js = options.jsUrl;
const css = options.cssUrl;
const other = [];
/**
* Buffer incoming contents
*
* @param {File} file File
* @param enc
* @param {Function} cb Callback
*/
function bufferContents(file, enc, cb) {
// We don't do streams
if (file.isStream()) {
this.emit('error', new Error('shortbread: Streaming not supported'));
cb();
return;
}
// Detect whether it's a JavaScript resource
for (const r of options.js) {
if (file.relative.match(r)) {
js.push(file);
cb();
return;
}
}
// Detect whether it's a CSS resource
for (const r of options.css) {
if (file.relative.match(r)) {
css.push(file);
cb();
return;
}
}
other.push(file);
cb();
}
/**
* End the stream
*
* @param {Function} cb Callback
*/
function endStream(cb) {
const result = shortbread(
js,
css,
critical,
cookieSlot,
callback,
{
prefix: options.prefix,
css: options.css,
js: options.js,
debug: options.debug,
});
// If resources have been specified
if (Object.keys(result.resources).length) {
// Create the initial page load resource
this.push(new Vinyl({
path: options.initial,
contents: new Buffer(result.initial),
}));
// Create the subsequent page load resource
this.push(new Vinyl({
path: options.subsequent,
contents: new Buffer(result.subsequent),
}));
}
other.forEach((file) => {
const fileWithData = file.clone({
deep: true,
contents: true,
});
fileWithData.data = result;
this.push(fileWithData);
})
;
// Create a JSON file with the shortbread result
if (options.data) {
this.push(new Vinyl({
path: `shortbread${cookieSlot ? `.${cookieSlot}` : ''}.json`,
contents: new Buffer(JSON.stringify(result, null, 4)),
}));
}
cb();
}
return through.obj(bufferContents, endStream);
};
/**
* Create a string hash
*
* @param {String} str String
* @param {Number} length Hash length
* @return {String} String hash
*/
shortbread.createHash = function createHash(str, length) {
return crypto.createHash('md5').update(str).digest('hex').substr(0, Math.max(8, parseInt(length || 8, 10)));
};
module.exports = shortbread;