-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathwrite-index-html.ts
83 lines (77 loc) · 2.45 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
/**
* @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 { Path, basename, getSystemPath, join, virtualFs } from '@angular-devkit/core';
import { Observable } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { ExtraEntryPoint } from '../../../browser/schema';
import { generateEntryPoints } from '../package-chunk-sort';
import { stripBom } from '../strip-bom';
import { FileInfo, augmentIndexHtml } from './augment-index-html';
export interface WriteIndexHtmlOptions {
host: virtualFs.Host;
outputPath: Path;
indexPath: Path;
ES5BuildFiles: EmittedFiles[];
ES2015BuildFiles: EmittedFiles[];
baseHref?: string;
deployUrl?: string;
sri?: boolean;
scripts?: ExtraEntryPoint[];
styles?: ExtraEntryPoint[];
}
export function writeIndexHtml({
host,
outputPath,
indexPath,
ES5BuildFiles,
ES2015BuildFiles,
baseHref,
deployUrl,
sri = false,
scripts = [],
styles = [],
}: WriteIndexHtmlOptions): Observable<void> {
return host.read(indexPath)
.pipe(
map(content => stripBom(virtualFs.fileBufferToString(content))),
switchMap(content => augmentIndexHtml({
input: getSystemPath(outputPath),
inputContent: content,
baseHref,
deployUrl,
sri,
entrypoints: generateEntryPoints({ scripts, styles }),
files: filterAndMapBuildFiles(ES5BuildFiles, '.css'),
noModuleFiles: filterAndMapBuildFiles(ES5BuildFiles, '.js'),
moduleFiles: filterAndMapBuildFiles(ES2015BuildFiles, '.js'),
loadOutputFile: async filePath => {
return host.read(join(outputPath, filePath))
.pipe(
map(data => virtualFs.fileBufferToString(data)),
)
.toPromise();
},
}),
),
map(content => virtualFs.stringToFileBuffer(content.source())),
switchMap(content => host.write(join(outputPath, basename(indexPath)), content)),
);
}
function filterAndMapBuildFiles(
files: EmittedFiles[],
extensionFilter: '.js' | '.css',
): FileInfo[] {
const filteredFiles: FileInfo[] = [];
for (const { file, name, extension, initial } of files) {
if (name && initial && extension === extensionFilter) {
filteredFiles.push({ file, extension, name });
}
}
return filteredFiles;
}