forked from ember-cli/ember-cli-inject-live-reload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
72 lines (62 loc) · 2.85 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
'use strict';
const buildLiveReloadPath = require('clean-base-url');
const VersionChecker = require('ember-cli-version-checker');
module.exports = {
name: 'live-reload-middleware',
contentFor: function(type) {
let liveReloadPort = process.env.EMBER_CLI_INJECT_LIVE_RELOAD_PORT;
let 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) {
let liveReloadOptions = options.liveReloadOptions;
if (liveReloadOptions && liveReloadOptions.snipver === undefined) {
liveReloadOptions.snipver = 1;
}
let liveReloadPath = buildLiveReloadPath(options.liveReloadPrefix) || '/';
let liveReloadPort;
let path = '';
if (options.liveReloadPort !== options.port) {
liveReloadPort = options.liveReloadPort
}
if (options.isLatestEmber && options.liveReloadPrefix) {
path = `&path=${options.liveReloadPrefix}/livereload`;
}
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 path = '${path}';
var prefixURL = ${liveReloadPort ? "(location.protocol || 'http:') + '//' + host + ':' + " + liveReloadPort : "''"};
var src = srcUrl || prefixURL + '${liveReloadPath + 'livereload.js?port='}' + port + '&host=' + host + path;
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = src;
document.getElementsByTagName('head')[0].appendChild(script);
}());`;
},
serverMiddleware: function(config) {
let options = config.options;
let app = config.app;
let self = this;
// maintaining `baseURL` for backwards compatibility. See: http://emberjs.com/blog/2016/04/28/baseURL.html
let baseURL = options.liveReloadBaseUrl || options.rootURL || options.baseURL;
if (this.parent) {
let checker = new VersionChecker(this.parent);
let depedency = checker.for('ember-cli');
options.isLatestEmber = depedency.gt('3.5.0');
}
if (options.liveReload !== true) { return; }
process.env.EMBER_CLI_INJECT_LIVE_RELOAD_PORT = options.liveReloadPort;
process.env.EMBER_CLI_INJECT_LIVE_RELOAD_BASEURL = baseURL;
let baseURLWithoutHost = baseURL.replace(/^https?:\/\/[^/]+/, '');
app.use(baseURLWithoutHost + 'ember-cli-live-reload.js', function(request, response) {
response.contentType('text/javascript');
response.send(self.dynamicScript(options));
});
}
};