-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathwrite-index-html.ts
95 lines (84 loc) · 2.69 KB
/
write-index-html.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
/**
* @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 { EmittedFiles } from '@angular-devkit/build-webpack';
import { normalize, virtualFs } from '@angular-devkit/core';
import { dirname, join } from 'path';
import { ExtraEntryPoint } from '../../browser/schema';
import { generateEntryPoints } from '../package-chunk-sort';
import { stripBom } from '../strip-bom';
import { CrossOriginValue, FileInfo, augmentIndexHtml } from './augment-index-html';
type ExtensionFilter = '.js' | '.css';
export interface WriteIndexHtmlOptions {
host: virtualFs.Host;
outputPath: string;
indexPath: string;
files?: EmittedFiles[];
noModuleFiles?: EmittedFiles[];
moduleFiles?: EmittedFiles[];
baseHref?: string;
deployUrl?: string;
sri?: boolean;
scripts?: ExtraEntryPoint[];
styles?: ExtraEntryPoint[];
postTransforms: IndexHtmlTransform[];
crossOrigin?: CrossOriginValue;
lang?: string;
}
export type IndexHtmlTransform = (content: string) => Promise<string>;
export async function writeIndexHtml({
host,
outputPath,
indexPath,
files = [],
noModuleFiles = [],
moduleFiles = [],
baseHref,
deployUrl,
sri = false,
scripts = [],
styles = [],
postTransforms,
crossOrigin,
lang,
}: WriteIndexHtmlOptions): Promise<void> {
const readFile = async (filePath: string) =>
virtualFs.fileBufferToString(await host.read(normalize(filePath)).toPromise());
let content = await augmentIndexHtml({
input: outputPath,
inputContent: stripBom(await readFile(indexPath)),
baseHref,
deployUrl,
crossOrigin,
sri,
lang,
entrypoints: generateEntryPoints({ scripts, styles }),
files: filterAndMapBuildFiles(files, ['.js', '.css']),
noModuleFiles: filterAndMapBuildFiles(noModuleFiles, '.js'),
moduleFiles: filterAndMapBuildFiles(moduleFiles, '.js'),
loadOutputFile: filePath => readFile(join(dirname(outputPath), filePath)),
});
for (const transform of postTransforms) {
content = await transform(content);
}
await host.write(normalize(outputPath), virtualFs.stringToFileBuffer(content)).toPromise();
}
function filterAndMapBuildFiles(
files: EmittedFiles[],
extensionFilter: ExtensionFilter | ExtensionFilter[],
): FileInfo[] {
const filteredFiles: FileInfo[] = [];
const validExtensions: string[] = Array.isArray(extensionFilter)
? extensionFilter
: [extensionFilter];
for (const { file, name, extension, initial } of files) {
if (name && initial && validExtensions.includes(extension)) {
filteredFiles.push({ file, extension, name });
}
}
return filteredFiles;
}