diff --git a/package.json b/package.json
index 02485f9e4ab2..459322e2e3ac 100644
--- a/package.json
+++ b/package.json
@@ -111,6 +111,7 @@
"@types/node": "10.12.30",
"@types/node-fetch": "^2.1.6",
"@types/npm-package-arg": "^6.1.0",
+ "@types/parse5-html-rewriting-stream": "^5.1.2",
"@types/pidusage": "^2.0.1",
"@types/progress": "^2.0.3",
"@types/request": "^2.47.1",
@@ -180,9 +181,7 @@
"open": "7.3.0",
"ora": "5.1.0",
"pacote": "11.1.4",
- "parse5": "6.0.1",
"parse5-html-rewriting-stream": "6.0.1",
- "parse5-htmlparser2-tree-adapter": "6.0.1",
"pidtree": "^0.5.0",
"pidusage": "^2.0.17",
"pnp-webpack-plugin": "1.6.4",
diff --git a/packages/angular/pwa/BUILD.bazel b/packages/angular/pwa/BUILD.bazel
index b3c856a22cb0..525339f9254c 100644
--- a/packages/angular/pwa/BUILD.bazel
+++ b/packages/angular/pwa/BUILD.bazel
@@ -42,7 +42,7 @@ ts_library(
"//packages/angular_devkit/schematics",
"//packages/schematics/angular",
"@npm//@types/node",
- "@npm//parse5-html-rewriting-stream",
+ "@npm//@types/parse5-html-rewriting-stream",
"@npm//rxjs",
],
)
@@ -60,6 +60,7 @@ ts_library(
deps = [
":pwa",
"//packages/angular_devkit/schematics/testing",
+ "@npm//parse5-html-rewriting-stream",
],
)
diff --git a/packages/angular/pwa/pwa/index.ts b/packages/angular/pwa/pwa/index.ts
index f5ad877c73c8..3fac23935f01 100644
--- a/packages/angular/pwa/pwa/index.ts
+++ b/packages/angular/pwa/pwa/index.ts
@@ -22,18 +22,16 @@ import { getWorkspace, updateWorkspace } from '@schematics/angular/utility/works
import { Readable, Writable } from 'stream';
import { Schema as PwaOptions } from './schema';
-const RewritingStream = require('parse5-html-rewriting-stream');
-
function updateIndexFile(path: string): Rule {
- return (host: Tree) => {
+ return async (host: Tree) => {
const buffer = host.read(path);
if (buffer === null) {
throw new SchematicsException(`Could not read index file: ${path}`);
}
- const rewriter = new RewritingStream();
+ const rewriter = new (await import('parse5-html-rewriting-stream'))();
let needsNoScript = true;
- rewriter.on('startTag', (startTag: { tagName: string }) => {
+ rewriter.on('startTag', startTag => {
if (startTag.tagName === 'noscript') {
needsNoScript = false;
}
@@ -41,7 +39,7 @@ function updateIndexFile(path: string): Rule {
rewriter.emitStartTag(startTag);
});
- rewriter.on('endTag', (endTag: { tagName: string }) => {
+ rewriter.on('endTag', endTag => {
if (endTag.tagName === 'head') {
rewriter.emitRaw(' \n');
rewriter.emitRaw(' \n');
diff --git a/packages/angular_devkit/build_angular/BUILD.bazel b/packages/angular_devkit/build_angular/BUILD.bazel
index 6894c5d6129a..c0ca0c55895f 100644
--- a/packages/angular_devkit/build_angular/BUILD.bazel
+++ b/packages/angular_devkit/build_angular/BUILD.bazel
@@ -125,6 +125,7 @@ ts_library(
"@npm//@types/loader-utils",
"@npm//@types/minimatch",
"@npm//@types/node",
+ "@npm//@types/parse5-html-rewriting-stream",
"@npm//@types/rimraf",
"@npm//@types/semver",
"@npm//@types/speed-measure-webpack-plugin",
@@ -160,8 +161,7 @@ ts_library(
"@npm//ng-packagr",
"@npm//open",
"@npm//ora",
- "@npm//parse5",
- "@npm//parse5-htmlparser2-tree-adapter",
+ "@npm//parse5-html-rewriting-stream",
"@npm//pnp-webpack-plugin",
"@npm//postcss",
"@npm//postcss-import",
diff --git a/packages/angular_devkit/build_angular/package.json b/packages/angular_devkit/build_angular/package.json
index afd8be5d37c3..bc5088b3f721 100644
--- a/packages/angular_devkit/build_angular/package.json
+++ b/packages/angular_devkit/build_angular/package.json
@@ -44,8 +44,7 @@
"minimatch": "3.0.4",
"open": "7.3.0",
"ora": "5.1.0",
- "parse5": "6.0.1",
- "parse5-htmlparser2-tree-adapter": "6.0.1",
+ "parse5-html-rewriting-stream": "6.0.1",
"pnp-webpack-plugin": "1.6.4",
"postcss": "7.0.32",
"postcss-import": "12.0.1",
diff --git a/packages/angular_devkit/build_angular/src/utils/index-file/augment-index-html.ts b/packages/angular_devkit/build_angular/src/utils/index-file/augment-index-html.ts
index 5c040f281aea..26b3ca7b42c1 100644
--- a/packages/angular_devkit/build_angular/src/utils/index-file/augment-index-html.ts
+++ b/packages/angular_devkit/build_angular/src/utils/index-file/augment-index-html.ts
@@ -7,10 +7,7 @@
*/
import { createHash } from 'crypto';
-import { RawSource, ReplaceSource } from 'webpack-sources';
-
-const parse5 = require('parse5');
-const treeAdapter = require('parse5-htmlparser2-tree-adapter');
+import { htmlRewritingStream } from './html-rewriting-stream';
export type LoadOutputFileFunctionType = (file: string) => Promise;
@@ -59,12 +56,14 @@ export interface FileInfo {
* after processing several configurations in order to build different sets of
* bundles for differential serving.
*/
-// tslint:disable-next-line: no-big-function
export async function augmentIndexHtml(params: AugmentIndexHtmlOptions): Promise {
- const { loadOutputFile, files, noModuleFiles = [], moduleFiles = [], entrypoints } = params;
+ const {
+ loadOutputFile, files, noModuleFiles = [], moduleFiles = [], entrypoints,
+ sri, deployUrl = '', lang, baseHref, inputContent,
+ } = params;
let { crossOrigin = 'none' } = params;
- if (params.sri && crossOrigin === 'none') {
+ if (sri && crossOrigin === 'none') {
crossOrigin = 'anonymous';
}
@@ -90,33 +89,12 @@ export async function augmentIndexHtml(params: AugmentIndexHtmlOptions): Promise
}
}
- // Find the head and body elements
- const document = parse5.parse(params.inputContent, {
- treeAdapter,
- sourceCodeLocationInfo: true,
- });
-
- // tslint:disable: no-any
- const htmlElement = document.children.find((c: any) => c.name === 'html');
- const headElement = htmlElement.children.find((c: any) => c.name === 'head');
- const bodyElement = htmlElement.children.find((c: any) => c.name === 'body');
- // tslint:enable: no-any
-
- if (!headElement || !bodyElement) {
- throw new Error('Missing head and/or body elements');
- }
-
- // Inject into the html
- const indexSource = new ReplaceSource(new RawSource(params.inputContent), params.input);
-
- const scriptsElements = treeAdapter.createDocumentFragment();
+ const scriptTags: string[] = [];
for (const script of scripts) {
- const attrs: { name: string; value: string }[] = [
- { name: 'src', value: (params.deployUrl || '') + script },
- ];
+ const attrs = [`src="${deployUrl}${script}"`];
if (crossOrigin !== 'none') {
- attrs.push({ name: 'crossorigin', value: crossOrigin });
+ attrs.push(`crossorigin="${crossOrigin}"`);
}
// We want to include nomodule or module when a file is not common amongs all
@@ -130,111 +108,115 @@ export async function augmentIndexHtml(params: AugmentIndexHtmlOptions): Promise
const isModuleType = moduleFiles.some(scriptPredictor);
if (isNoModuleType && !isModuleType) {
- attrs.push(
- { name: 'nomodule', value: '' },
- { name: 'defer', value: '' },
- );
+ attrs.push('nomodule', 'defer');
} else if (isModuleType && !isNoModuleType) {
- attrs.push({ name: 'type', value: 'module' });
+ attrs.push('type="module"');
} else {
- attrs.push({ name: 'defer', value: '' });
+ attrs.push('defer');
}
} else {
- attrs.push({ name: 'defer', value: '' });
+ attrs.push('defer');
}
- if (params.sri) {
+ if (sri) {
const content = await loadOutputFile(script);
- attrs.push(_generateSriAttributes(content));
+ attrs.push(generateSriAttributes(content));
}
- const baseElement = treeAdapter.createElement('script', undefined, attrs);
- treeAdapter.setTemplateContent(scriptsElements, baseElement);
+ scriptTags.push(``);
}
- indexSource.insert(
- // parse5 does not provide locations if malformed html is present
- bodyElement.sourceCodeLocation?.endTag?.startOffset || params.inputContent.indexOf('