-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
104 lines (88 loc) · 2.5 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
'use strict';
var debug = require('diagnostics')('bigpipe-domain')
, merge = require('lodash.merge')
, format = require('url').format
, path = require('path');
//
// Plugin name.
//
exports.name = 'domain';
//
// Override the default behavior of BigPipe's compiler html function.
// This is used to set the correct path for dependencies.
//
exports.server = function server(bigpipe, options) {
var registerProxy = bigpipe._compiler.register
, pageletProxy = bigpipe._compiler.pagelet
, domain = options('domain', '/')
, mode = typeof domain;
/**
* Join the filename with the provided pathname.
*
* @param {String} file Filename
* @returns {String} Joined path.
* @api private
*/
function join(file) {
return path.join('object' === mode ? domain.pathname : domain, file);
}
/**
* Prepend the filename.
*
* @param {String} file Hashed filename.
* @returns {String} Fully parsed url.
* @api private
*/
function prepend(file) {
var url;
switch (mode) {
case 'object':
url = merge({}, domain);
url.pathname = join(file);
url = format(url);
break;
default:
url = join(file);
break;
}
return url;
}
//
// Remove the buffer reference that was registered and update
// it with the updated file path based on the url.
//
bigpipe._compiler.on('register', function register(file, next) {
var location = file.location
, origin = file.origin;
if (!location || file.external) {
return next();
}
debug('Registering alias %s in buffer', join(location));
this.buffer[join(location)] = file;
if (origin) this.buffer[join(origin)] = file;
next();
});
//
// Overrule the asset resolver of pagelets to prepend the location.
//
bigpipe._compiler.pagelet = function pagelet() {
var assets = pageletProxy.apply(bigpipe._compiler, arguments);
debug('Adding %d JS and %d CSS assets', assets.js.length, assets.css.length);
assets.js = assets.js.map(prepend);
assets.css = assets.css.map(prepend);
return assets;
};
//
// Overrule the default behavior of the depedencies stringifier.
//
bigpipe._compiler.html = function html(file) {
var location = !file.external
? prepend(file.location)
: file.location;
switch (file.extname) {
case '.css': return '<link rel=stylesheet href="'+ location +'" />';
case '.js': return '<script src="'+ location +'"></script>';
default: return '';
}
};
};