Skip to content

Commit

Permalink
fix(@schematics/angular): wrap bootstrapping code in an HMR compatibl…
Browse files Browse the repository at this point in the history
…e manner

With this change we update the universal schematic bootstrap code to handle HMR properly. Previously, the bootstrapping code was called only on `DOMContentLoaded` which is not triggered when running in HMR.

Closes #21932

(cherry picked from commit 08687d1)
  • Loading branch information
alan-agius4 committed Oct 13, 2021
1 parent d2e24d3 commit f6ed31f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
18 changes: 13 additions & 5 deletions packages/schematics/angular/universal/index.ts
Expand Up @@ -141,9 +141,17 @@ function wrapBootstrapCall(mainFile: string): Rule {
// indent contents
const triviaWidth = bootstrapCall.getLeadingTriviaWidth();
const beforeText =
`document.addEventListener('DOMContentLoaded', () => {\n` +
' '.repeat(triviaWidth > 2 ? triviaWidth + 1 : triviaWidth);
const afterText = `\n${triviaWidth > 2 ? ' '.repeat(triviaWidth - 1) : ''}});`;
`function bootstrap() {\n` + ' '.repeat(triviaWidth > 2 ? triviaWidth + 1 : triviaWidth);
const afterText =
`\n${triviaWidth > 2 ? ' '.repeat(triviaWidth - 1) : ''}};\n` +
`
if (document.readyState === 'complete') {
bootstrap();
} else {
document.addEventListener('DOMContentLoaded', bootstrap);
}
`;

// in some cases we need to cater for a trailing semicolon such as;
// bootstrap().catch(err => console.log(err));
Expand Down Expand Up @@ -236,8 +244,8 @@ export default function (options: UniversalOptions): Rule {
throw targetBuildNotFoundError();
}

const clientBuildOptions = ((clientBuildTarget.options ||
{}) as unknown) as BrowserBuilderOptions;
const clientBuildOptions = (clientBuildTarget.options ||
{}) as unknown as BrowserBuilderOptions;

const clientTsConfig = normalize(clientBuildOptions.tsConfig);
const tsConfigExtends = basename(clientTsConfig);
Expand Down
6 changes: 2 additions & 4 deletions packages/schematics/angular/universal/index_spec.ts
Expand Up @@ -191,7 +191,7 @@ describe('Universal Schematic', () => {
.toPromise();
const filePath = '/projects/bar/src/main.ts';
const contents = tree.readContent(filePath);
expect(contents).toMatch(/document.addEventListener\('DOMContentLoaded', \(\) => {/);
expect(contents).toContain(`document.addEventListener('DOMContentLoaded', bootstrap);`);
});

it('should wrap the bootstrap declaration in a DOMContentLoaded event handler', async () => {
Expand Down Expand Up @@ -221,9 +221,7 @@ describe('Universal Schematic', () => {
.runSchematicAsync('universal', defaultOptions, appTree)
.toPromise();
const contents = tree.readContent(filePath);
expect(contents).toMatch(
/document.addEventListener\('DOMContentLoaded', \(\) => {[\n\r\s]+bootstrap\(\)/,
);
expect(contents).toContain(`document.addEventListener('DOMContentLoaded', bootstrap);`);
});

it('should install npm dependencies', async () => {
Expand Down

0 comments on commit f6ed31f

Please sign in to comment.