1 | 'use strict';
|
2 |
|
3 | const buildLiveReloadPath = require('clean-base-url');
|
4 | const VersionChecker = require('ember-cli-version-checker');
|
5 |
|
6 | module.exports = {
|
7 | name: 'live-reload-middleware',
|
8 |
|
9 | contentFor: function(type) {
|
10 | let liveReloadPort = process.env.EMBER_CLI_INJECT_LIVE_RELOAD_PORT;
|
11 | let baseURL = process.env.EMBER_CLI_INJECT_LIVE_RELOAD_BASEURL;
|
12 |
|
13 | if (liveReloadPort && type === 'head') {
|
14 | return '<script data-embroider-ignore src="' + baseURL + 'ember-cli-live-reload.js" type="text/javascript"></script>';
|
15 | }
|
16 | },
|
17 |
|
18 | dynamicScript: function(options) {
|
19 | let liveReloadOptions = options.liveReloadOptions;
|
20 | if (liveReloadOptions && liveReloadOptions.snipver === undefined) {
|
21 | liveReloadOptions.snipver = 1;
|
22 | }
|
23 |
|
24 | let liveReloadPath = buildLiveReloadPath(options.liveReloadPrefix) || '/';
|
25 | let path = '';
|
26 | if (options.isLatestEmber && options.liveReloadPrefix) {
|
27 | path = `&path=${options.liveReloadPrefix}/livereload`;
|
28 | }
|
29 | return `(function() {${liveReloadOptions ? "\n window.LiveReloadOptions = " + JSON.stringify(liveReloadOptions) + ";" : ''}
|
30 | var srcUrl = ${options.liveReloadJsUrl ? "'" + options.liveReloadJsUrl + "'" : null};
|
31 | var host = location.hostname || 'localhost';
|
32 | var useCustomPort = ${options.liveReloadPort !== options.port} || location.port !== ${options.liveReloadPort};
|
33 | var defaultPort = location.port || (location.protocol === 'https:' ? 443 : 80);
|
34 | var port = useCustomPort ? ${options.liveReloadPort} : defaultPort;
|
35 | var path = '${path}';
|
36 | var prefixURL = useCustomPort ? (location.protocol || 'http:') + '//' + host + ':' + ${options.liveReloadPort} : '';
|
37 | var src = srcUrl || prefixURL + '${liveReloadPath + 'livereload.js?port='}' + port + '&host=' + host + path;
|
38 | var script = document.createElement('script');
|
39 | script.type = 'text/javascript';
|
40 | script.src = src;
|
41 | document.getElementsByTagName('head')[0].appendChild(script);
|
42 | }());`;
|
43 | },
|
44 |
|
45 | serverMiddleware: function(config) {
|
46 | let options = config.options;
|
47 | let app = config.app;
|
48 |
|
49 | let self = this;
|
50 |
|
51 | let baseURL = options.liveReloadBaseUrl || options.rootURL || options.baseURL;
|
52 | if (this.parent) {
|
53 | let checker = new VersionChecker(this.parent);
|
54 | let depedency = checker.for('ember-cli');
|
55 | options.isLatestEmber = depedency.gt('3.5.0');
|
56 | }
|
57 | if (options.liveReload !== true) { return; }
|
58 |
|
59 | process.env.EMBER_CLI_INJECT_LIVE_RELOAD_PORT = options.liveReloadPort;
|
60 | process.env.EMBER_CLI_INJECT_LIVE_RELOAD_BASEURL = baseURL;
|
61 |
|
62 | let baseURLWithoutHost = baseURL.replace(/^https?:\/\/[^/]+/, '');
|
63 | app.use(baseURLWithoutHost + 'ember-cli-live-reload.js', function(request, response) {
|
64 | response.contentType('text/javascript');
|
65 | response.send(self.dynamicScript(options));
|
66 | });
|
67 | }
|
68 | };
|