Skip to content

Commit 9908b59

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

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
@@ -87,7 +87,7 @@ export async function augmentIndexHtml(params: AugmentIndexHtmlOptions): Promise
8787
}
8888
}
8989

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

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

127-
const linkTags: string[] = [];
127+
let linkTags: string[] = [];
128128
for (const stylesheet of stylesheets) {
129129
const attrs = [
130130
`rel="stylesheet"`,
@@ -180,19 +180,30 @@ export async function augmentIndexHtml(params: AugmentIndexHtmlOptions): Promise
180180
for (const linkTag of linkTags) {
181181
rewriter.emitRaw(linkTag);
182182
}
183+
184+
linkTags = [];
183185
break;
184186
case 'body':
185187
// Add script tags
186188
for (const scriptTag of scriptTags) {
187189
rewriter.emitRaw(scriptTag);
188190
}
191+
192+
scriptTags = [];
189193
break;
190194
}
191195

192196
rewriter.emitEndTag(tag);
193197
});
194198

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

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

0 commit comments

Comments
 (0)