source: trip-planner-front/node_modules/@angular-devkit/build-angular/src/utils/index-file/augment-index-html.js

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 5.8 KB
Line 
1"use strict";
2/**
3 * @license
4 * Copyright Google LLC All Rights Reserved.
5 *
6 * Use of this source code is governed by an MIT-style license that can be
7 * found in the LICENSE file at https://angular.io/license
8 */
9Object.defineProperty(exports, "__esModule", { value: true });
10exports.augmentIndexHtml = void 0;
11const crypto_1 = require("crypto");
12const html_rewriting_stream_1 = require("./html-rewriting-stream");
13/*
14 * Helper function used by the IndexHtmlWebpackPlugin.
15 * Can also be directly used by builder, e. g. in order to generate an index.html
16 * after processing several configurations in order to build different sets of
17 * bundles for differential serving.
18 */
19async function augmentIndexHtml(params) {
20 const { loadOutputFile, files, noModuleFiles = [], moduleFiles = [], entrypoints, sri, deployUrl = '', lang, baseHref, html, } = params;
21 let { crossOrigin = 'none' } = params;
22 if (sri && crossOrigin === 'none') {
23 crossOrigin = 'anonymous';
24 }
25 const stylesheets = new Set();
26 const scripts = new Set();
27 // Sort files in the order we want to insert them by entrypoint and dedupes duplicates
28 const mergedFiles = [...moduleFiles, ...noModuleFiles, ...files];
29 for (const entrypoint of entrypoints) {
30 for (const { extension, file, name } of mergedFiles) {
31 if (name !== entrypoint) {
32 continue;
33 }
34 switch (extension) {
35 case '.js':
36 scripts.add(file);
37 break;
38 case '.css':
39 stylesheets.add(file);
40 break;
41 }
42 }
43 }
44 let scriptTags = [];
45 for (const script of scripts) {
46 const attrs = [`src="${deployUrl}${script}"`];
47 if (crossOrigin !== 'none') {
48 attrs.push(`crossorigin="${crossOrigin}"`);
49 }
50 // We want to include nomodule or module when a file is not common amongs all
51 // such as runtime.js
52 const scriptPredictor = ({ file }) => file === script;
53 if (!files.some(scriptPredictor)) {
54 // in some cases for differential loading file with the same name is available in both
55 // nomodule and module such as scripts.js
56 // we shall not add these attributes if that's the case
57 const isNoModuleType = noModuleFiles.some(scriptPredictor);
58 const isModuleType = moduleFiles.some(scriptPredictor);
59 if (isNoModuleType && !isModuleType) {
60 attrs.push('nomodule', 'defer');
61 }
62 else if (isModuleType && !isNoModuleType) {
63 attrs.push('type="module"');
64 }
65 else {
66 attrs.push('defer');
67 }
68 }
69 else {
70 attrs.push('defer');
71 }
72 if (sri) {
73 const content = await loadOutputFile(script);
74 attrs.push(generateSriAttributes(content));
75 }
76 scriptTags.push(`<script ${attrs.join(' ')}></script>`);
77 }
78 let linkTags = [];
79 for (const stylesheet of stylesheets) {
80 const attrs = [`rel="stylesheet"`, `href="${deployUrl}${stylesheet}"`];
81 if (crossOrigin !== 'none') {
82 attrs.push(`crossorigin="${crossOrigin}"`);
83 }
84 if (sri) {
85 const content = await loadOutputFile(stylesheet);
86 attrs.push(generateSriAttributes(content));
87 }
88 linkTags.push(`<link ${attrs.join(' ')}>`);
89 }
90 const { rewriter, transformedContent } = await html_rewriting_stream_1.htmlRewritingStream(html);
91 const baseTagExists = html.includes('<base');
92 rewriter
93 .on('startTag', (tag) => {
94 switch (tag.tagName) {
95 case 'html':
96 // Adjust document locale if specified
97 if (isString(lang)) {
98 updateAttribute(tag, 'lang', lang);
99 }
100 break;
101 case 'head':
102 // Base href should be added before any link, meta tags
103 if (!baseTagExists && isString(baseHref)) {
104 rewriter.emitStartTag(tag);
105 rewriter.emitRaw(`<base href="${baseHref}">`);
106 return;
107 }
108 break;
109 case 'base':
110 // Adjust base href if specified
111 if (isString(baseHref)) {
112 updateAttribute(tag, 'href', baseHref);
113 }
114 break;
115 }
116 rewriter.emitStartTag(tag);
117 })
118 .on('endTag', (tag) => {
119 switch (tag.tagName) {
120 case 'head':
121 for (const linkTag of linkTags) {
122 rewriter.emitRaw(linkTag);
123 }
124 linkTags = [];
125 break;
126 case 'body':
127 // Add script tags
128 for (const scriptTag of scriptTags) {
129 rewriter.emitRaw(scriptTag);
130 }
131 scriptTags = [];
132 break;
133 }
134 rewriter.emitEndTag(tag);
135 });
136 const content = await transformedContent;
137 if (linkTags.length || scriptTags.length) {
138 // In case no body/head tags are not present (dotnet partial templates)
139 return linkTags.join('') + scriptTags.join('') + content;
140 }
141 return content;
142}
143exports.augmentIndexHtml = augmentIndexHtml;
144function generateSriAttributes(content) {
145 const algo = 'sha384';
146 const hash = crypto_1.createHash(algo).update(content, 'utf8').digest('base64');
147 return `integrity="${algo}-${hash}"`;
148}
149function updateAttribute(tag, name, value) {
150 const index = tag.attrs.findIndex((a) => a.name === name);
151 const newValue = { name, value };
152 if (index === -1) {
153 tag.attrs.push(newValue);
154 }
155 else {
156 tag.attrs[index] = newValue;
157 }
158}
159function isString(value) {
160 return typeof value === 'string';
161}
Note: See TracBrowser for help on using the repository browser.