-
-
Notifications
You must be signed in to change notification settings - Fork 53
/
index.js
61 lines (51 loc) · 2.44 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
'use strict';
var buildLiveReloadPath = require('clean-base-url');
module.exports = {
name: 'live-reload-middleware',
contentFor: function(type) {
var liveReloadPort = process.env.EMBER_CLI_INJECT_LIVE_RELOAD_PORT;
var baseURL = process.env.EMBER_CLI_INJECT_LIVE_RELOAD_BASEURL;
if (liveReloadPort && type === 'head') {
return '<script src="' + baseURL + 'ember-cli-live-reload.js" type="text/javascript"></script>';
}
},
dynamicScript: function(options) {
var liveReloadOptions = options.liveReloadOptions;
if (liveReloadOptions && liveReloadOptions.snipver === undefined) {
liveReloadOptions.snipver = 1;
}
var liveReloadPath = buildLiveReloadPath(options.liveReloadPrefix) || '/';
let liveReloadPort;
if (options.liveReloadPort !== options.port) {
liveReloadPort = options.liveReloadPort
}
return `(function() {${liveReloadOptions ? "\n window.LiveReloadOptions = " + JSON.stringify(liveReloadOptions) + ";" : ''}
var srcUrl = ${options.liveReloadJsUrl ? "'" + options.liveReloadJsUrl + "'" : null};
var host= location.hostname || 'localhost';
var liveReloadPort = ${liveReloadPort};
var defaultPort = location.protocol === 'https:' ? 443 : 80;
var port = liveReloadPort || location.port || defaultPort;
var prefixURL = '${liveReloadPort ? "(location.protocol || 'http:') + '//' + host + ':' + " + liveReloadPort : ''}';
var src = srcUrl || prefixURL + '${liveReloadPath + 'livereload.js?port='}' + port + '&host=' + host;
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = src;
document.getElementsByTagName('head')[0].appendChild(script);
}());`;
},
serverMiddleware: function(config) {
var self = this;
var app = config.app;
var options = config.options;
// maintaining `baseURL` for backwards compatibility. See: http://emberjs.com/blog/2016/04/28/baseURL.html
var baseURL = options.liveReloadBaseUrl || options.rootURL || options.baseURL;
if (options.liveReload !== true) { return; }
process.env.EMBER_CLI_INJECT_LIVE_RELOAD_PORT = options.liveReloadPort;
process.env.EMBER_CLI_INJECT_LIVE_RELOAD_BASEURL = baseURL;
var baseURLWithoutHost = baseURL.replace(/^https?:\/\/[^\/]+/, '');
app.use(baseURLWithoutHost + 'ember-cli-live-reload.js', function(request, response, next) {
response.contentType('text/javascript');
response.send(self.dynamicScript(options));
});
}
};