UNPKG

2.06 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var logger_diagnostics_1 = require("../logger/logger-diagnostics");
4var path = require("path");
5var tinylr = require("tiny-lr");
6var events = require("../util/events");
7function createLiveReloadServer(config) {
8 var liveReloadServer = tinylr();
9 liveReloadServer.listen(config.liveReloadPort, config.host);
10 function fileChange(changedFiles) {
11 // only do a live reload if there are no diagnostics
12 // the notification server takes care of showing diagnostics
13 if (!logger_diagnostics_1.hasDiagnostics(config.buildDir)) {
14 liveReloadServer.changed({
15 body: {
16 files: changedFiles.map(function (changedFile) { return '/' + path.relative(config.wwwDir, changedFile.filePath); })
17 }
18 });
19 }
20 }
21 events.on(events.EventType.FileChange, fileChange);
22 events.on(events.EventType.ReloadApp, function () {
23 fileChange([{ event: 'change', ext: '.html', filePath: 'index.html' }]);
24 });
25}
26exports.createLiveReloadServer = createLiveReloadServer;
27function injectLiveReloadScript(content, host, port) {
28 var contentStr = content.toString();
29 var liveReloadScript = getLiveReloadScript(host, port);
30 if (contentStr.indexOf('/livereload.js') > -1) {
31 // already added script
32 return content;
33 }
34 var match = contentStr.match(/<\/body>(?![\s\S]*<\/body>)/i);
35 if (!match) {
36 match = contentStr.match(/<\/html>(?![\s\S]*<\/html>)/i);
37 }
38 if (match) {
39 contentStr = contentStr.replace(match[0], liveReloadScript + "\n" + match[0]);
40 }
41 else {
42 contentStr += liveReloadScript;
43 }
44 return contentStr;
45}
46exports.injectLiveReloadScript = injectLiveReloadScript;
47function getLiveReloadScript(host, port) {
48 var src = "//" + host + ":" + port + "/livereload.js?snipver=1";
49 return " <!-- Ionic Dev Server: Injected LiveReload Script -->\n <script src=\"" + src + "\" async=\"\" defer=\"\"></script>";
50}