forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex-html-webpack-plugin.ts
More file actions
115 lines (98 loc) · 3.31 KB
/
index-html-webpack-plugin.ts
File metadata and controls
115 lines (98 loc) · 3.31 KB
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
/**
* @license
* Copyright Google LLC 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.dev/license
*/
import {
FileInfo,
IndexHtmlGenerator,
IndexHtmlGeneratorOptions,
IndexHtmlGeneratorProcessOptions,
} from '@angular/build/private';
import { basename, dirname, extname } from 'node:path';
import { Compilation, Compiler, sources } from 'webpack';
import { assertIsError } from '../../../utils/error';
import { addError, addWarning } from '../../../utils/webpack-diagnostics';
export interface IndexHtmlWebpackPluginOptions
extends IndexHtmlGeneratorOptions,
Omit<IndexHtmlGeneratorProcessOptions, 'files'> {}
const PLUGIN_NAME = 'index-html-webpack-plugin';
export class IndexHtmlWebpackPlugin extends IndexHtmlGenerator {
private _compilation: Compilation | undefined;
get compilation(): Compilation {
if (this._compilation) {
return this._compilation;
}
throw new Error('compilation is undefined.');
}
constructor(override readonly options: IndexHtmlWebpackPluginOptions) {
super(options);
}
apply(compiler: Compiler) {
compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
this._compilation = compilation;
compilation.hooks.processAssets.tapPromise(
{
name: PLUGIN_NAME,
stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE + 1,
},
callback,
);
});
const callback = async (assets: Record<string, unknown>) => {
const files: FileInfo[] = [];
try {
for (const chunk of this.compilation.chunks) {
for (const file of chunk.files) {
// https://github.com/webpack/webpack/blob/1f99ad6367f2b8a6ef17cce0e058f7a67fb7db18/lib/config/defaults.js#L1000
if (file.endsWith('.hot-update.js') || file.endsWith('.hot-update.mjs')) {
continue;
}
files.push({
name: chunk.name ?? undefined,
file,
extension: extname(file),
});
}
}
const {
csrContent: content,
warnings,
errors,
} = await this.process({
files,
outputPath: dirname(this.options.outputPath),
baseHref: this.options.baseHref,
lang: this.options.lang,
});
assets[this.options.outputPath] = new sources.RawSource(content);
warnings.forEach((msg) => addWarning(this.compilation, msg));
errors.forEach((msg) => addError(this.compilation, msg));
} catch (error) {
assertIsError(error);
addError(this.compilation, error.message);
}
};
}
override async readAsset(path: string): Promise<string> {
const data = this.compilation.assets[basename(path)].source();
return typeof data === 'string' ? data : data.toString();
}
protected override async readIndex(path: string): Promise<string> {
return new Promise<string>((resolve, reject) => {
this.compilation.inputFileSystem.readFile(
path,
(err?: Error | null, data?: string | Buffer) => {
if (err) {
reject(err);
return;
}
this.compilation.fileDependencies.add(path);
resolve(data?.toString() ?? '');
},
);
});
}
}