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 | */
|
---|
9 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
---|
10 | if (k2 === undefined) k2 = k;
|
---|
11 | Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
---|
12 | }) : (function(o, m, k, k2) {
|
---|
13 | if (k2 === undefined) k2 = k;
|
---|
14 | o[k2] = m[k];
|
---|
15 | }));
|
---|
16 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
---|
17 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
---|
18 | }) : function(o, v) {
|
---|
19 | o["default"] = v;
|
---|
20 | });
|
---|
21 | var __importStar = (this && this.__importStar) || function (mod) {
|
---|
22 | if (mod && mod.__esModule) return mod;
|
---|
23 | var result = {};
|
---|
24 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
---|
25 | __setModuleDefault(result, mod);
|
---|
26 | return result;
|
---|
27 | };
|
---|
28 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
29 | exports.IndexHtmlGenerator = void 0;
|
---|
30 | const fs = __importStar(require("fs"));
|
---|
31 | const path_1 = require("path");
|
---|
32 | const strip_bom_1 = require("../strip-bom");
|
---|
33 | const augment_index_html_1 = require("./augment-index-html");
|
---|
34 | const inline_critical_css_1 = require("./inline-critical-css");
|
---|
35 | const inline_fonts_1 = require("./inline-fonts");
|
---|
36 | class IndexHtmlGenerator {
|
---|
37 | constructor(options) {
|
---|
38 | var _a, _b;
|
---|
39 | this.options = options;
|
---|
40 | const extraPlugins = [];
|
---|
41 | if ((_a = this.options.optimization) === null || _a === void 0 ? void 0 : _a.fonts.inline) {
|
---|
42 | extraPlugins.push(inlineFontsPlugin(this));
|
---|
43 | }
|
---|
44 | if ((_b = this.options.optimization) === null || _b === void 0 ? void 0 : _b.styles.inlineCritical) {
|
---|
45 | extraPlugins.push(inlineCriticalCssPlugin(this));
|
---|
46 | }
|
---|
47 | this.plugins = [augmentIndexHtmlPlugin(this), ...extraPlugins, postTransformPlugin(this)];
|
---|
48 | }
|
---|
49 | async process(options) {
|
---|
50 | let content = strip_bom_1.stripBom(await this.readIndex(this.options.indexPath));
|
---|
51 | const warnings = [];
|
---|
52 | const errors = [];
|
---|
53 | for (const plugin of this.plugins) {
|
---|
54 | const result = await plugin(content, options);
|
---|
55 | if (typeof result === 'string') {
|
---|
56 | content = result;
|
---|
57 | }
|
---|
58 | else {
|
---|
59 | content = result.content;
|
---|
60 | if (result.warnings.length) {
|
---|
61 | warnings.push(...result.warnings);
|
---|
62 | }
|
---|
63 | if (result.errors.length) {
|
---|
64 | errors.push(...result.errors);
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|
68 | return {
|
---|
69 | content,
|
---|
70 | warnings,
|
---|
71 | errors,
|
---|
72 | };
|
---|
73 | }
|
---|
74 | async readAsset(path) {
|
---|
75 | return fs.promises.readFile(path, 'utf-8');
|
---|
76 | }
|
---|
77 | async readIndex(path) {
|
---|
78 | return fs.promises.readFile(path, 'utf-8');
|
---|
79 | }
|
---|
80 | }
|
---|
81 | exports.IndexHtmlGenerator = IndexHtmlGenerator;
|
---|
82 | function augmentIndexHtmlPlugin(generator) {
|
---|
83 | const { deployUrl, crossOrigin, sri = false, entrypoints } = generator.options;
|
---|
84 | return async (html, options) => {
|
---|
85 | const { lang, baseHref, outputPath = '', noModuleFiles, files, moduleFiles } = options;
|
---|
86 | return augment_index_html_1.augmentIndexHtml({
|
---|
87 | html,
|
---|
88 | baseHref,
|
---|
89 | deployUrl,
|
---|
90 | crossOrigin,
|
---|
91 | sri,
|
---|
92 | lang,
|
---|
93 | entrypoints,
|
---|
94 | loadOutputFile: (filePath) => generator.readAsset(path_1.join(outputPath, filePath)),
|
---|
95 | noModuleFiles,
|
---|
96 | moduleFiles,
|
---|
97 | files,
|
---|
98 | });
|
---|
99 | };
|
---|
100 | }
|
---|
101 | function inlineFontsPlugin({ options }) {
|
---|
102 | var _a;
|
---|
103 | const inlineFontsProcessor = new inline_fonts_1.InlineFontsProcessor({
|
---|
104 | minify: (_a = options.optimization) === null || _a === void 0 ? void 0 : _a.styles.minify,
|
---|
105 | WOFFSupportNeeded: options.WOFFSupportNeeded,
|
---|
106 | });
|
---|
107 | return async (html) => inlineFontsProcessor.process(html);
|
---|
108 | }
|
---|
109 | function inlineCriticalCssPlugin(generator) {
|
---|
110 | var _a;
|
---|
111 | const inlineCriticalCssProcessor = new inline_critical_css_1.InlineCriticalCssProcessor({
|
---|
112 | minify: (_a = generator.options.optimization) === null || _a === void 0 ? void 0 : _a.styles.minify,
|
---|
113 | deployUrl: generator.options.deployUrl,
|
---|
114 | readAsset: (filePath) => generator.readAsset(filePath),
|
---|
115 | });
|
---|
116 | return async (html, options) => inlineCriticalCssProcessor.process(html, { outputPath: options.outputPath });
|
---|
117 | }
|
---|
118 | function postTransformPlugin({ options }) {
|
---|
119 | return async (html) => (options.postTransform ? options.postTransform(html) : html);
|
---|
120 | }
|
---|