Skip to content

Commit c81d17f

Browse files
alan-agius4filipesilva
authored andcommitted
fix(@angular-devkit/build-angular): handle HTML file with no body and head tags
Closes #19426
1 parent 20ec0e2 commit c81d17f

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

packages/angular_devkit/build_angular/src/utils/index-file/augment-index-html.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export async function augmentIndexHtml(params: AugmentIndexHtmlOptions): Promise
8989
}
9090
}
9191

92-
const scriptTags: string[] = [];
92+
let scriptTags: string[] = [];
9393
for (const script of scripts) {
9494
const attrs = [`src="${deployUrl}${script}"`];
9595

@@ -126,7 +126,7 @@ export async function augmentIndexHtml(params: AugmentIndexHtmlOptions): Promise
126126
scriptTags.push(`<script ${attrs.join(' ')}></script>`);
127127
}
128128

129-
const linkTags: string[] = [];
129+
let linkTags: string[] = [];
130130
for (const stylesheet of stylesheets) {
131131
const attrs = [
132132
`rel="stylesheet"`,
@@ -182,19 +182,30 @@ export async function augmentIndexHtml(params: AugmentIndexHtmlOptions): Promise
182182
for (const linkTag of linkTags) {
183183
rewriter.emitRaw(linkTag);
184184
}
185+
186+
linkTags = [];
185187
break;
186188
case 'body':
187189
// Add script tags
188190
for (const scriptTag of scriptTags) {
189191
rewriter.emitRaw(scriptTag);
190192
}
193+
194+
scriptTags = [];
191195
break;
192196
}
193197

194198
rewriter.emitEndTag(tag);
195199
});
196200

197-
return transformedContent;
201+
const content = await transformedContent;
202+
203+
if (linkTags.length || scriptTags.length) {
204+
// In case no body/head tags are not present (dotnet partial templates)
205+
return linkTags.join('') + scriptTags.join('') + content;
206+
}
207+
208+
return content;
198209
}
199210

200211
function generateSriAttributes(content: string): string {

packages/angular_devkit/build_angular/src/utils/index-file/augment-index-html_spec.ts

+23
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,27 @@ describe('augment-index-html', () => {
167167
</html>
168168
`);
169169
});
170+
171+
it(`should add script and link tags even when body and head element doesn't exist`, async () => {
172+
const source = augmentIndexHtml({
173+
...indexGeneratorOptions,
174+
html: `<app-root></app-root>`,
175+
files: [
176+
{ file: 'styles.css', extension: '.css', name: 'styles' },
177+
{ file: 'runtime.js', extension: '.js', name: 'main' },
178+
{ file: 'main.js', extension: '.js', name: 'main' },
179+
{ file: 'runtime.js', extension: '.js', name: 'polyfills' },
180+
{ file: 'polyfills.js', extension: '.js', name: 'polyfills' },
181+
],
182+
});
183+
184+
const html = await source;
185+
expect(html).toEqual(oneLineHtml`
186+
<link rel="stylesheet" href="styles.css">
187+
<script src="runtime.js" defer></script>
188+
<script src="polyfills.js" defer></script>
189+
<script src="main.js" defer></script>
190+
<app-root></app-root>
191+
`);
192+
});
170193
});

0 commit comments

Comments
 (0)