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 1ee34cdc4a06..d27516c8c3af 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 @@ -87,7 +87,7 @@ export async function augmentIndexHtml(params: AugmentIndexHtmlOptions): Promise } } - const scriptTags: string[] = []; + let scriptTags: string[] = []; for (const script of scripts) { const attrs = [`src="${deployUrl}${script}"`]; @@ -124,7 +124,7 @@ export async function augmentIndexHtml(params: AugmentIndexHtmlOptions): Promise scriptTags.push(``); } - const linkTags: string[] = []; + let linkTags: string[] = []; for (const stylesheet of stylesheets) { const attrs = [ `rel="stylesheet"`, @@ -180,19 +180,30 @@ export async function augmentIndexHtml(params: AugmentIndexHtmlOptions): Promise for (const linkTag of linkTags) { rewriter.emitRaw(linkTag); } + + linkTags = []; break; case 'body': // Add script tags for (const scriptTag of scriptTags) { rewriter.emitRaw(scriptTag); } + + scriptTags = []; break; } rewriter.emitEndTag(tag); }); - return transformedContent; + const content = await transformedContent; + + if (linkTags.length || scriptTags.length) { + // In case no body/head tags are not present (dotnet partial templates) + return linkTags.join('') + scriptTags.join('') + content; + } + + return content; } function generateSriAttributes(content: string): string { diff --git a/packages/angular_devkit/build_angular/src/utils/index-file/augment-index-html_spec.ts b/packages/angular_devkit/build_angular/src/utils/index-file/augment-index-html_spec.ts index 90eb93673670..29fac3a9a084 100644 --- a/packages/angular_devkit/build_angular/src/utils/index-file/augment-index-html_spec.ts +++ b/packages/angular_devkit/build_angular/src/utils/index-file/augment-index-html_spec.ts @@ -166,4 +166,27 @@ describe('augment-index-html', () => { `); }); + + it(`should add script and link tags even when body and head element doesn't exist`, async () => { + const source = augmentIndexHtml({ + ...indexGeneratorOptions, + html: ``, + files: [ + { file: 'styles.css', extension: '.css', name: 'styles' }, + { file: 'runtime.js', extension: '.js', name: 'main' }, + { file: 'main.js', extension: '.js', name: 'main' }, + { file: 'runtime.js', extension: '.js', name: 'polyfills' }, + { file: 'polyfills.js', extension: '.js', name: 'polyfills' }, + ], + }); + + const html = await source; + expect(html).toEqual(oneLineHtml` + + + + + + `); + }); });