-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathindex-html-generator.ts
120 lines (101 loc) · 3.17 KB
/
index-html-generator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { join } from 'path';
import { readFile } from '../fs';
import { NormalizeOptimizationOptions } from '../normalize-optimization';
import { stripBom } from '../strip-bom';
import { CrossOriginValue, FileInfo, augmentIndexHtml } from './augment-index-html';
import { InlineFontsProcessor } from './inline-fonts';
type IndexHtmlGeneratorPlugin = (html: string, options: IndexHtmlGeneratorProcessOptions) => Promise<string>;
export interface IndexHtmlGeneratorProcessOptions {
lang: string | undefined;
baseHref: string | undefined;
outputPath: string;
files: FileInfo[];
noModuleFiles: FileInfo[];
moduleFiles: FileInfo[];
}
export interface IndexHtmlGeneratorOptions {
indexPath: string;
deployUrl?: string;
sri?: boolean;
entrypoints: string[];
postTransform?: IndexHtmlTransform;
crossOrigin?: CrossOriginValue;
optimization?: NormalizeOptimizationOptions;
WOFFSupportNeeded: boolean;
}
export type IndexHtmlTransform = (content: string) => Promise<string>;
export class IndexHtmlGenerator {
private readonly plugins: IndexHtmlGeneratorPlugin[];
constructor(readonly options: IndexHtmlGeneratorOptions) {
const extraPlugins: IndexHtmlGeneratorPlugin[] = [];
if (this.options.optimization?.fonts.inline) {
extraPlugins.push(inlineFontsPlugin(this));
}
this.plugins = [
augmentIndexHtmlPlugin(this),
...extraPlugins,
postTransformPlugin(this),
];
}
async process(options: IndexHtmlGeneratorProcessOptions): Promise<string> {
let html = stripBom(await this.readIndex(this.options.indexPath));
for (const plugin of this.plugins) {
html = await plugin(html, options);
}
return html;
}
async readAsset(path: string): Promise<string> {
return readFile(path, 'utf-8');
}
protected async readIndex(path: string): Promise<string> {
return readFile(path, 'utf-8');
}
}
function augmentIndexHtmlPlugin(generator: IndexHtmlGenerator): IndexHtmlGeneratorPlugin {
const {
deployUrl,
crossOrigin,
sri = false,
entrypoints,
} = generator.options;
return async (html, options) => {
const {
lang,
baseHref,
outputPath = '',
noModuleFiles,
files,
moduleFiles,
} = options;
return augmentIndexHtml({
html,
baseHref,
deployUrl,
crossOrigin,
sri,
lang,
entrypoints,
loadOutputFile: filePath => generator.readAsset(join(outputPath, filePath)),
noModuleFiles,
moduleFiles,
files,
});
};
}
function inlineFontsPlugin({ options }: IndexHtmlGenerator): IndexHtmlGeneratorPlugin {
const inlineFontsProcessor = new InlineFontsProcessor({
minifyInlinedCSS: !!options.optimization?.styles,
WOFFSupportNeeded: options.WOFFSupportNeeded,
});
return async html => inlineFontsProcessor.process(html);
}
function postTransformPlugin({ options }: IndexHtmlGenerator): IndexHtmlGeneratorPlugin {
return async html => options.postTransform ? options.postTransform(html) : html;
}