File tree Expand file tree Collapse file tree 2 files changed +15
-9
lines changed
packages/schematics/angular/universal Expand file tree Collapse file tree 2 files changed +15
-9
lines changed Original file line number Diff line number Diff line change @@ -141,9 +141,17 @@ function wrapBootstrapCall(mainFile: string): Rule {
141
141
// indent contents
142
142
const triviaWidth = bootstrapCall . getLeadingTriviaWidth ( ) ;
143
143
const beforeText =
144
- `document.addEventListener('DOMContentLoaded', () => {\n` +
145
- ' ' . repeat ( triviaWidth > 2 ? triviaWidth + 1 : triviaWidth ) ;
146
- const afterText = `\n${ triviaWidth > 2 ? ' ' . repeat ( triviaWidth - 1 ) : '' } });` ;
144
+ `function bootstrap() {\n` + ' ' . repeat ( triviaWidth > 2 ? triviaWidth + 1 : triviaWidth ) ;
145
+ const afterText =
146
+ `\n${ triviaWidth > 2 ? ' ' . repeat ( triviaWidth - 1 ) : '' } };\n` +
147
+ `
148
+
149
+ if (document.readyState === 'complete') {
150
+ bootstrap();
151
+ } else {
152
+ document.addEventListener('DOMContentLoaded', bootstrap);
153
+ }
154
+ ` ;
147
155
148
156
// in some cases we need to cater for a trailing semicolon such as;
149
157
// bootstrap().catch(err => console.log(err));
@@ -236,8 +244,8 @@ export default function (options: UniversalOptions): Rule {
236
244
throw targetBuildNotFoundError ( ) ;
237
245
}
238
246
239
- const clientBuildOptions = ( ( clientBuildTarget . options ||
240
- { } ) as unknown ) as BrowserBuilderOptions ;
247
+ const clientBuildOptions = ( clientBuildTarget . options ||
248
+ { } ) as unknown as BrowserBuilderOptions ;
241
249
242
250
const clientTsConfig = normalize ( clientBuildOptions . tsConfig ) ;
243
251
const tsConfigExtends = basename ( clientTsConfig ) ;
Original file line number Diff line number Diff line change @@ -191,7 +191,7 @@ describe('Universal Schematic', () => {
191
191
. toPromise ( ) ;
192
192
const filePath = '/projects/bar/src/main.ts' ;
193
193
const contents = tree . readContent ( filePath ) ;
194
- expect ( contents ) . toMatch ( / d o c u m e n t .a d d E v e n t L i s t e n e r \ (' D O M C o n t e n t L o a d e d ' , \( \) = > { / ) ;
194
+ expect ( contents ) . toContain ( ` document.addEventListener('DOMContentLoaded', bootstrap);` ) ;
195
195
} ) ;
196
196
197
197
it ( 'should wrap the bootstrap declaration in a DOMContentLoaded event handler' , async ( ) => {
@@ -221,9 +221,7 @@ describe('Universal Schematic', () => {
221
221
. runSchematicAsync ( 'universal' , defaultOptions , appTree )
222
222
. toPromise ( ) ;
223
223
const contents = tree . readContent ( filePath ) ;
224
- expect ( contents ) . toMatch (
225
- / d o c u m e n t .a d d E v e n t L i s t e n e r \( ' D O M C o n t e n t L o a d e d ' , \( \) = > { [ \n \r \s ] + b o o t s t r a p \( \) / ,
226
- ) ;
224
+ expect ( contents ) . toContain ( `document.addEventListener('DOMContentLoaded', bootstrap);` ) ;
227
225
} ) ;
228
226
229
227
it ( 'should install npm dependencies' , async ( ) => {
You can’t perform that action at this time.
0 commit comments