@@ -17,19 +17,16 @@ import {
17
17
} from '@angular/cdk/schematics' ;
18
18
import {
19
19
importsProvidersFrom ,
20
- addModuleImportToStandaloneBootstrap ,
20
+ findBootstrapApplicationCall ,
21
+ addFunctionalProvidersToStandaloneBootstrap ,
22
+ callsProvidersFunction ,
21
23
} from '@schematics/angular/private/components' ;
22
24
import { getWorkspace , ProjectDefinition } from '@schematics/angular/utility/workspace' ;
23
25
import { ProjectType } from '@schematics/angular/utility/workspace-models' ;
24
26
import { addFontsToIndex } from './fonts/material-fonts' ;
25
27
import { Schema } from './schema' ;
26
28
import { addThemeToAppStyles , addTypographyClass } from './theming/theming' ;
27
-
28
- /** Name of the Angular module that enables Angular browser animations. */
29
- const browserAnimationsModuleName = 'BrowserAnimationsModule' ;
30
-
31
- /** Name of the module that switches Angular animations to a noop implementation. */
32
- const noopAnimationsModuleName = 'NoopAnimationsModule' ;
29
+ import * as ts from 'typescript' ;
33
30
34
31
/**
35
32
* Scaffolds the basics of a Angular Material application, this includes:
@@ -70,57 +67,60 @@ function addAnimationsModule(options: Schema) {
70
67
return async ( host : Tree , context : SchematicContext ) => {
71
68
const workspace = await getWorkspace ( host ) ;
72
69
const project = getProjectFromWorkspace ( workspace , options . project ) ;
70
+ const mainFilePath = getProjectMainFile ( project ) ;
71
+ const mainSourceFile = ts . createSourceFile (
72
+ mainFilePath ,
73
+ host . readText ( mainFilePath ) ,
74
+ ts . ScriptTarget . Latest ,
75
+ ) ;
73
76
74
- try {
75
- addAnimationsModuleToNonStandaloneApp ( host , project , context , options ) ;
76
- } catch ( e ) {
77
- if ( ( e as { message ?: string } ) . message ?. includes ( 'Bootstrap call not found' ) ) {
78
- addAnimationsModuleToStandaloneApp ( host , project , context , options ) ;
79
- } else {
80
- throw e ;
81
- }
77
+ if ( findBootstrapApplicationCall ( mainSourceFile ) ) {
78
+ addAnimationsToStandaloneApp ( host , mainFilePath , context , options ) ;
79
+ } else {
80
+ addAnimationsToNonStandaloneApp ( host , project , mainFilePath , context , options ) ;
82
81
}
83
82
} ;
84
83
}
85
84
86
85
/** Adds the animations module to an app that is bootstrap using the standalone component APIs. */
87
- function addAnimationsModuleToStandaloneApp (
86
+ function addAnimationsToStandaloneApp (
88
87
host : Tree ,
89
- project : ProjectDefinition ,
88
+ mainFile : string ,
90
89
context : SchematicContext ,
91
90
options : Schema ,
92
91
) {
93
- const mainFile = getProjectMainFile ( project ) ;
92
+ const animationsFunction = 'provideAnimations' ;
93
+ const noopAnimationsFunction = 'provideNoopAnimations' ;
94
94
95
95
if ( options . animations === 'enabled' ) {
96
- // In case the project explicitly uses the NoopAnimationsModule , we should print a warning
96
+ // In case the project explicitly uses provideNoopAnimations , we should print a warning
97
97
// message that makes the user aware of the fact that we won't automatically set up
98
- // animations. If we would add the BrowserAnimationsModule while the NoopAnimationsModule
98
+ // animations. If we would add provideAnimations while provideNoopAnimations
99
99
// is already configured, we would cause unexpected behavior and runtime exceptions.
100
- if ( importsProvidersFrom ( host , mainFile , noopAnimationsModuleName ) ) {
100
+ if ( callsProvidersFunction ( host , mainFile , noopAnimationsFunction ) ) {
101
101
context . logger . error (
102
- `Could not set up "${ browserAnimationsModuleName } " ` +
103
- `because "${ noopAnimationsModuleName } " is already imported .` ,
102
+ `Could not add "${ animationsFunction } " ` +
103
+ `because "${ noopAnimationsFunction } " is already provided .` ,
104
104
) ;
105
105
context . logger . info ( `Please manually set up browser animations.` ) ;
106
106
} else {
107
- addModuleImportToStandaloneBootstrap (
107
+ addFunctionalProvidersToStandaloneBootstrap (
108
108
host ,
109
109
mainFile ,
110
- browserAnimationsModuleName ,
110
+ animationsFunction ,
111
111
'@angular/platform-browser/animations' ,
112
112
) ;
113
113
}
114
114
} else if (
115
115
options . animations === 'disabled' &&
116
- ! importsProvidersFrom ( host , mainFile , browserAnimationsModuleName )
116
+ ! importsProvidersFrom ( host , mainFile , animationsFunction )
117
117
) {
118
- // Do not add the NoopAnimationsModule module if the project already explicitly uses
119
- // the BrowserAnimationsModule .
120
- addModuleImportToStandaloneBootstrap (
118
+ // Do not add the provideNoopAnimations if the project already explicitly uses
119
+ // the provideAnimations .
120
+ addFunctionalProvidersToStandaloneBootstrap (
121
121
host ,
122
122
mainFile ,
123
- noopAnimationsModuleName ,
123
+ noopAnimationsFunction ,
124
124
'@angular/platform-browser/animations' ,
125
125
) ;
126
126
}
@@ -130,13 +130,16 @@ function addAnimationsModuleToStandaloneApp(
130
130
* Adds the animations module to an app that is bootstrap
131
131
* using the non-standalone component APIs.
132
132
*/
133
- function addAnimationsModuleToNonStandaloneApp (
133
+ function addAnimationsToNonStandaloneApp (
134
134
host : Tree ,
135
135
project : ProjectDefinition ,
136
+ mainFile : string ,
136
137
context : SchematicContext ,
137
138
options : Schema ,
138
139
) {
139
- const appModulePath = getAppModulePath ( host , getProjectMainFile ( project ) ) ;
140
+ const browserAnimationsModuleName = 'BrowserAnimationsModule' ;
141
+ const noopAnimationsModuleName = 'NoopAnimationsModule' ;
142
+ const appModulePath = getAppModulePath ( host , mainFile ) ;
140
143
141
144
if ( options . animations === 'enabled' ) {
142
145
// In case the project explicitly uses the NoopAnimationsModule, we should print a warning
0 commit comments