UNPKG

5.5 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11var __importDefault = (this && this.__importDefault) || function (mod) {
12 return (mod && mod.__esModule) ? mod : { "default": mod };
13};
14Object.defineProperty(exports, "__esModule", { value: true });
15const path_1 = __importDefault(require("path"));
16const icons_1 = require("./icons");
17const utils_1 = require("./utils");
18const voidTags = [
19 'area',
20 'base',
21 'br',
22 'col',
23 'embed',
24 'hr',
25 'img',
26 'input',
27 'keygen',
28 'link',
29 'menuitem',
30 'meta',
31 'param',
32 'source',
33 'track',
34 'wbr',
35];
36function createFilename(filenameTemplate, json, shouldFingerprint) {
37 const formatters = [
38 {
39 pattern: /\[hash(:([1-9]|[1-2][0-9]|3[0-2]))?\]/gi,
40 value: (substring, limit = ':32') => {
41 if (!shouldFingerprint)
42 return '';
43 const hash = utils_1.generateFingerprint(json);
44 return hash.substr(0, parseInt(limit.substr(1), 10));
45 },
46 },
47 {
48 pattern: /\[ext\]/gi,
49 value: 'json',
50 },
51 {
52 pattern: /\[name\]/gi,
53 value: 'manifest',
54 },
55 ];
56 // @ts-ignore
57 return formatters.reduce((acc, curr) => acc.replace(curr.pattern, curr.value), filenameTemplate);
58}
59// Create `manifest.json`
60function writeManifestToFile(manifest, options, publicPath) {
61 let content = Object.assign({}, manifest);
62 if (content.orientation === 'omit') {
63 delete content.orientation;
64 }
65 const json = JSON.stringify(content, null, 2);
66 const file = path_1.default.parse(options.filename);
67 const filename = createFilename(file.base, json, options.fingerprints);
68 const output = options.includeDirectory ? path_1.default.join(file.dir, filename) : filename;
69 return {
70 output,
71 url: utils_1.joinURI(publicPath, output),
72 source: json,
73 size: json.length,
74 };
75}
76function buildResourcesAsync(self, publicPath = '') {
77 return __awaiter(this, void 0, void 0, function* () {
78 if (!self.assets || !self.options.inject) {
79 let parsedIconsResult = {};
80 if (!self.options.noResources) {
81 const [results, config] = icons_1.retrieveIcons(self.manifest);
82 self.manifest = config;
83 parsedIconsResult = yield icons_1.parseIconsAsync(self.projectRoot, results, publicPath);
84 }
85 const { icons, assets = [] } = parsedIconsResult;
86 const results = writeManifestToFile(Object.assign(Object.assign({}, self.manifest), { icons }), self.options, publicPath);
87 self.assets = [results, ...assets];
88 return results;
89 }
90 return null;
91 });
92}
93exports.buildResourcesAsync = buildResourcesAsync;
94function generateAppleSplashAndIconTags(assets) {
95 let tags = {};
96 for (let asset of assets) {
97 if (asset.ios && asset.ios.valid) {
98 if (asset.ios.valid === 'startup') {
99 tags = applyTag(tags, 'link', {
100 rel: 'apple-touch-startup-image',
101 media: asset.ios.media,
102 href: asset.ios.href,
103 });
104 }
105 else {
106 tags = applyTag(tags, 'link', {
107 // apple-touch-icon-precomposed
108 rel: 'apple-touch-icon',
109 sizes: asset.ios.size,
110 href: asset.ios.href,
111 });
112 }
113 }
114 }
115 return tags;
116}
117exports.generateAppleSplashAndIconTags = generateAppleSplashAndIconTags;
118function applyTag(obj, tag, content) {
119 if (!content) {
120 return obj;
121 }
122 const current = obj[tag];
123 if (current) {
124 if (Array.isArray(current)) {
125 return Object.assign(Object.assign({}, obj), { [tag]: [...current, content] });
126 }
127 return Object.assign(Object.assign({}, obj), { [tag]: [current, content] });
128 }
129 return Object.assign(Object.assign({}, obj), { [tag]: content });
130}
131exports.applyTag = applyTag;
132function generateHtmlTags(tags) {
133 let html = '';
134 for (let tag in tags) {
135 const attrs = tags[tag];
136 if (Array.isArray(attrs)) {
137 for (let a of attrs) {
138 html = `${html}${generateHtmlTags({
139 [tag]: a,
140 })}`;
141 }
142 }
143 else {
144 html = `${html}<${tag}`;
145 for (let key of Object.keys(attrs)) {
146 // @ts-ignore
147 const attr = attrs[key];
148 html = `${html} ${key}="${attr}"`;
149 }
150 html = voidTags.includes(tag) ? `${html} />` : `${html}></${tag}>`;
151 }
152 }
153 return html;
154}
155exports.generateHtmlTags = generateHtmlTags;
156//# sourceMappingURL=injector.js.map
\No newline at end of file