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 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
10 | exports.IndexHtmlWebpackPlugin = void 0;
|
---|
11 | const path_1 = require("path");
|
---|
12 | const webpack_1 = require("webpack");
|
---|
13 | const index_html_generator_1 = require("../../utils/index-file/index-html-generator");
|
---|
14 | const webpack_diagnostics_1 = require("../../utils/webpack-diagnostics");
|
---|
15 | const PLUGIN_NAME = 'index-html-webpack-plugin';
|
---|
16 | class IndexHtmlWebpackPlugin extends index_html_generator_1.IndexHtmlGenerator {
|
---|
17 | constructor(options) {
|
---|
18 | super(options);
|
---|
19 | this.options = options;
|
---|
20 | }
|
---|
21 | get compilation() {
|
---|
22 | if (this._compilation) {
|
---|
23 | return this._compilation;
|
---|
24 | }
|
---|
25 | throw new Error('compilation is undefined.');
|
---|
26 | }
|
---|
27 | apply(compiler) {
|
---|
28 | compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
|
---|
29 | this._compilation = compilation;
|
---|
30 | compilation.hooks.processAssets.tapPromise({
|
---|
31 | name: PLUGIN_NAME,
|
---|
32 | stage: webpack_1.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE + 1,
|
---|
33 | }, callback);
|
---|
34 | });
|
---|
35 | const callback = async (assets) => {
|
---|
36 | var _a;
|
---|
37 | // Get all files for selected entrypoints
|
---|
38 | const files = [];
|
---|
39 | const noModuleFiles = [];
|
---|
40 | const moduleFiles = [];
|
---|
41 | try {
|
---|
42 | for (const [entryName, entrypoint] of this.compilation.entrypoints) {
|
---|
43 | const entryFiles = (_a = entrypoint === null || entrypoint === void 0 ? void 0 : entrypoint.getFiles()) === null || _a === void 0 ? void 0 : _a.filter((f) => !f.endsWith('.hot-update.js')).map((f) => ({
|
---|
44 | name: entryName,
|
---|
45 | file: f,
|
---|
46 | extension: path_1.extname(f),
|
---|
47 | }));
|
---|
48 | if (!entryFiles) {
|
---|
49 | continue;
|
---|
50 | }
|
---|
51 | if (this.options.noModuleEntrypoints.includes(entryName)) {
|
---|
52 | noModuleFiles.push(...entryFiles);
|
---|
53 | }
|
---|
54 | else if (this.options.moduleEntrypoints.includes(entryName)) {
|
---|
55 | moduleFiles.push(...entryFiles);
|
---|
56 | }
|
---|
57 | else {
|
---|
58 | files.push(...entryFiles);
|
---|
59 | }
|
---|
60 | }
|
---|
61 | const { content, warnings, errors } = await this.process({
|
---|
62 | files,
|
---|
63 | noModuleFiles,
|
---|
64 | moduleFiles,
|
---|
65 | outputPath: path_1.dirname(this.options.outputPath),
|
---|
66 | baseHref: this.options.baseHref,
|
---|
67 | lang: this.options.lang,
|
---|
68 | });
|
---|
69 | assets[this.options.outputPath] = new webpack_1.sources.RawSource(content);
|
---|
70 | warnings.forEach((msg) => webpack_diagnostics_1.addWarning(this.compilation, msg));
|
---|
71 | errors.forEach((msg) => webpack_diagnostics_1.addError(this.compilation, msg));
|
---|
72 | }
|
---|
73 | catch (error) {
|
---|
74 | webpack_diagnostics_1.addError(this.compilation, error.message);
|
---|
75 | }
|
---|
76 | };
|
---|
77 | }
|
---|
78 | async readAsset(path) {
|
---|
79 | const data = this.compilation.assets[path_1.basename(path)].source();
|
---|
80 | return typeof data === 'string' ? data : data.toString();
|
---|
81 | }
|
---|
82 | async readIndex(path) {
|
---|
83 | return new Promise((resolve, reject) => {
|
---|
84 | this.compilation.inputFileSystem.readFile(path, (err, data) => {
|
---|
85 | var _a;
|
---|
86 | if (err) {
|
---|
87 | reject(err);
|
---|
88 | return;
|
---|
89 | }
|
---|
90 | this.compilation.fileDependencies.add(path);
|
---|
91 | resolve((_a = data === null || data === void 0 ? void 0 : data.toString()) !== null && _a !== void 0 ? _a : '');
|
---|
92 | });
|
---|
93 | });
|
---|
94 | }
|
---|
95 | }
|
---|
96 | exports.IndexHtmlWebpackPlugin = IndexHtmlWebpackPlugin;
|
---|