UNPKG

1.79 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var ionic_global_1 = require("./ionic-global");
4var helpers_1 = require("../util/helpers");
5var path_1 = require("path");
6function updateIndexHtml(context) {
7 var indexPath = path_1.join(context.wwwDir, context.wwwIndex);
8 return helpers_1.readFileAsync(indexPath).then(function (indexHtml) {
9 if (!indexHtml) {
10 return Promise.resolve(null);
11 }
12 indexHtml = injectCoreScripts(context, indexHtml);
13 return helpers_1.writeFileAsync(indexPath, indexHtml);
14 });
15}
16exports.updateIndexHtml = updateIndexHtml;
17function injectCoreScripts(context, indexHtml) {
18 var inject = [];
19 inject.push(" <script data-ionic=\"inject\">");
20 inject.push(" " + ionic_global_1.buildIonicGlobal(context));
21 inject.push(" </script>");
22 return injectCoreHtml(indexHtml, inject.join('\n'));
23}
24exports.injectCoreScripts = injectCoreScripts;
25function injectCoreHtml(indexHtml, inject) {
26 // see if we can find an existing ionic script tag and replace it entirely
27 var existingTag = indexHtml.match(/<script data-ionic="inject">[\s\S]*?<\/script>/gi);
28 if (existingTag) {
29 return indexHtml.replace(existingTag[0], inject.trim());
30 }
31 // see if we can find the head tag and inject it immediate below it
32 var headTag = indexHtml.match(/<head[^>]*>/gi);
33 if (headTag) {
34 return indexHtml.replace(headTag[0], headTag[0] + "\n" + inject);
35 }
36 // see if we can find the html tag and inject it immediate below it
37 var htmlTag = indexHtml.match(/<html[^>]*>/gi);
38 if (htmlTag) {
39 return indexHtml.replace(htmlTag[0], htmlTag[0] + "\n" + inject);
40 }
41 return inject + "\n" + indexHtml;
42}
43exports.injectCoreHtml = injectCoreHtml;