Skip to content

Commit d141f83

Browse files
authored
fix(material/core): remove animations from ng add (#30446)
Since we no longer depend on the animations module, we can remove the prompt and its related code from the `ng add` schematic.
1 parent a68ed6a commit d141f83

File tree

5 files changed

+1
-181
lines changed

5 files changed

+1
-181
lines changed

src/material/schematics/ng-add/index.spec.ts

-128
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,7 @@ describe('ng-add schematic', () => {
4545
.toContain(filePath);
4646
}
4747

48-
/** Removes the specified dependency from the /package.json in the given tree. */
49-
function removePackageJsonDependency(tree: Tree, dependencyName: string) {
50-
const packageContent = JSON.parse(getFileContent(tree, '/package.json')) as PackageJson;
51-
delete packageContent.dependencies[dependencyName];
52-
tree.overwrite('/package.json', JSON.stringify(packageContent, null, 2));
53-
}
54-
5548
it('should update package.json', async () => {
56-
// By default, the Angular workspace schematic sets up "@angular/animations". In order
57-
// to verify that we would set up the dependency properly if someone doesn't have the
58-
// animations installed already, we remove the animations dependency explicitly.
59-
removePackageJsonDependency(appTree, '@angular/animations');
60-
6149
const tree = await runner.runSchematic('ng-add', baseOptions, appTree);
6250
const packageJson = JSON.parse(getFileContent(tree, '/package.json')) as PackageJson;
6351
const dependencies = packageJson.dependencies;
@@ -68,11 +56,6 @@ describe('ng-add schematic', () => {
6856
expect(dependencies['@angular/forms'])
6957
.withContext('Expected the @angular/forms package to have the same version as @angular/core.')
7058
.toBe(angularCoreVersion);
71-
expect(dependencies['@angular/animations'])
72-
.withContext(
73-
'Expected the @angular/animations package to have the same ' + 'version as @angular/core.',
74-
)
75-
.toBe(angularCoreVersion);
7659

7760
expect(Object.keys(dependencies))
7861
.withContext('Expected the modified "dependencies" to be sorted alphabetically.')
@@ -185,73 +168,6 @@ describe('ng-add schematic', () => {
185168
);
186169
});
187170

188-
it('should add provideAnimationsAsync to the project module', async () => {
189-
const tree = await runner.runSchematic('ng-add-setup-project', baseOptions, appTree);
190-
const fileContent = getFileContent(tree, '/projects/material/src/app/app.module.ts');
191-
192-
expect(fileContent).toContain('provideAnimationsAsync()');
193-
expect(fileContent).toContain(
194-
`import { provideAnimationsAsync } from '@angular/platform-browser/animations/async';`,
195-
);
196-
});
197-
198-
it('should add the provideAnimationsAsync to a bootstrapApplication call', async () => {
199-
appTree.delete('/projects/material/src/app/app.module.ts');
200-
appTree.create(
201-
'/projects/material/src/app/app.config.ts',
202-
`
203-
export const appConfig = {
204-
providers: [{ provide: 'foo', useValue: 1 }]
205-
};
206-
`,
207-
);
208-
appTree.overwrite(
209-
'/projects/material/src/main.ts',
210-
`
211-
import { bootstrapApplication } from '@angular/platform-browser';
212-
import { AppComponent } from './app/app.component';
213-
import { appConfig } from './app/app.config';
214-
215-
bootstrapApplication(AppComponent, appConfig);
216-
`,
217-
);
218-
219-
const tree = await runner.runSchematic('ng-add-setup-project', baseOptions, appTree);
220-
const fileContent = getFileContent(tree, '/projects/material/src/app/app.config.ts');
221-
222-
expect(fileContent).toContain(
223-
`import { provideAnimationsAsync } from '@angular/platform-browser/animations/async';`,
224-
);
225-
expect(fileContent).toContain(`[{ provide: 'foo', useValue: 1 }, provideAnimationsAsync()]`);
226-
});
227-
228-
it("should add the provideAnimationAsync('noop') to the project module if animations are disabled", async () => {
229-
const tree = await runner.runSchematic(
230-
'ng-add-setup-project',
231-
{...baseOptions, animations: 'disabled'},
232-
appTree,
233-
);
234-
const fileContent = getFileContent(tree, '/projects/material/src/app/app.module.ts');
235-
236-
expect(fileContent).toContain(`provideAnimationsAsync('noop')`);
237-
expect(fileContent).toContain(
238-
`import { provideAnimationsAsync } from '@angular/platform-browser/animations/async';`,
239-
);
240-
});
241-
242-
it('should not add any animations code if animations are excluded', async () => {
243-
const tree = await runner.runSchematic(
244-
'ng-add-setup-project',
245-
{...baseOptions, animations: 'excluded'},
246-
appTree,
247-
);
248-
const fileContent = getFileContent(tree, '/projects/material/src/app/app.module.ts');
249-
250-
expect(fileContent).not.toContain('provideAnimationsAsync');
251-
expect(fileContent).not.toContain('@angular/platform-browser/animations');
252-
expect(fileContent).not.toContain('@angular/animations');
253-
});
254-
255171
describe('custom project builders', () => {
256172
/** Overwrites a target builder for the workspace in the given tree */
257173
function overwriteTargetBuilder(tree: Tree, targetName: 'build' | 'test', newBuilder: string) {
@@ -576,16 +492,6 @@ describe('ng-add schematic', () => {
576492
'body { margin: 0; font-family: Roboto, "Helvetica Neue", sans-serif; }',
577493
);
578494
});
579-
580-
it('should add the provideAnimationsAsync to the project module', async () => {
581-
const tree = await runner.runSchematic('ng-add-setup-project', baseOptions, appTree);
582-
const fileContent = getFileContent(tree, '/projects/material/src/app/app.module.ts');
583-
584-
expect(fileContent).toContain('provideAnimationsAsync()');
585-
expect(fileContent).toContain(
586-
`import { provideAnimationsAsync } from '@angular/platform-browser/animations/async';`,
587-
);
588-
});
589495
});
590496

591497
describe('using browser-esbuild builder', () => {
@@ -646,16 +552,6 @@ describe('ng-add schematic', () => {
646552
'body { margin: 0; font-family: Roboto, "Helvetica Neue", sans-serif; }',
647553
);
648554
});
649-
650-
it('should add the provideAnimationsAsync to the project module', async () => {
651-
const tree = await runner.runSchematic('ng-add-setup-project', baseOptions, appTree);
652-
const fileContent = getFileContent(tree, '/projects/material/src/app/app.module.ts');
653-
654-
expect(fileContent).toContain('provideAnimationsAsync()');
655-
expect(fileContent).toContain(
656-
`import { provideAnimationsAsync } from '@angular/platform-browser/animations/async';`,
657-
);
658-
});
659555
});
660556

661557
describe('using lower dependency builder', () => {
@@ -693,30 +589,6 @@ describe('ng-add schematic', () => {
693589

694590
expectProjectStyleFile(project, '@angular/material/prebuilt-themes/azure-blue.css');
695591
});
696-
697-
it('should add material app styles', async () => {
698-
const tree = await runner.runSchematic('ng-add-setup-project', baseOptions, appTree);
699-
const workspace = await getWorkspace(tree);
700-
const project = getProjectFromWorkspace(workspace, baseOptions.project);
701-
702-
const defaultStylesPath = getProjectStyleFile(project)!;
703-
const htmlContent = tree.read(defaultStylesPath)!.toString();
704-
705-
expect(htmlContent).toContain('html, body { height: 100%; }');
706-
expect(htmlContent).toContain(
707-
'body { margin: 0; font-family: Roboto, "Helvetica Neue", sans-serif; }',
708-
);
709-
});
710-
711-
it('should add the provideAnimationsAsync to the project module', async () => {
712-
const tree = await runner.runSchematic('ng-add-setup-project', baseOptions, appTree);
713-
const fileContent = getFileContent(tree, '/projects/material/src/app/app.module.ts');
714-
715-
expect(fileContent).toContain('provideAnimationsAsync()');
716-
expect(fileContent).toContain(
717-
`import { provideAnimationsAsync } from '@angular/platform-browser/animations/async';`,
718-
);
719-
});
720592
});
721593
});
722594

src/material/schematics/ng-add/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ export default function (options: Schema): Rule {
5353
materialVersionRange || fallbackMaterialVersionRange,
5454
);
5555
addPackageToPackageJson(host, '@angular/forms', angularDependencyVersion);
56-
addPackageToPackageJson(host, '@angular/animations', angularDependencyVersion);
5756

5857
// Since the Angular Material schematics depend on the schematic utility functions from the
5958
// CDK, we need to install the CDK before loading the schematic files that import from the CDK.

src/material/schematics/ng-add/schema.json

-14
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,6 @@
4444
"default": false,
4545
"description": "Whether to set up global typography styles.",
4646
"x-prompt": "Set up global Angular Material typography styles?"
47-
},
48-
"animations": {
49-
"type": "string",
50-
"default": "enabled",
51-
"description": "Whether Angular browser animations should be included.",
52-
"x-prompt": {
53-
"message": "Include the Angular animations module?",
54-
"type": "list",
55-
"items": [
56-
{"value": "enabled", "label": "Include and enable animations"},
57-
{"value": "disabled", "label": "Include, but disable animations"},
58-
{"value": "excluded", "label": "Do not include"}
59-
]
60-
}
6147
}
6248
},
6349
"required": []

src/material/schematics/ng-add/schema.ts

-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ export interface Schema {
1010
/** Name of the project. */
1111
project: string;
1212

13-
/** Whether the Angular browser animations module should be included and enabled. */
14-
animations: 'enabled' | 'disabled' | 'excluded';
15-
1613
/** Name of pre-built theme to install. */
1714
theme: 'azure-blue' | 'rose-red' | 'magenta-violet' | 'cyan-orange' | 'custom';
1815

src/material/schematics/ng-add/setup-project.ts

+1-35
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import {chain, noop, Rule, SchematicContext, Tree, callRule} from '@angular-devkit/schematics';
9+
import {chain, Rule, SchematicContext, Tree} from '@angular-devkit/schematics';
1010
import {getProjectFromWorkspace, getProjectStyleFile} from '@angular/cdk/schematics';
1111
import {getWorkspace} from '@schematics/angular/utility/workspace';
12-
import {addRootProvider} from '@schematics/angular/utility';
1312
import {ProjectType} from '@schematics/angular/utility/workspace-models';
14-
import {of as observableOf} from 'rxjs';
15-
import {catchError} from 'rxjs/operators';
1613
import {addFontsToIndex} from './fonts/material-fonts';
1714
import {Schema} from './schema';
1815
import {addThemeToAppStyles, addTypographyClass} from './theming/theming';
@@ -21,7 +18,6 @@ import {addThemeToAppStyles, addTypographyClass} from './theming/theming';
2118
* Scaffolds the basics of a Angular Material application, this includes:
2219
* - Add Packages to package.json
2320
* - Adds pre-built themes to styles.ext
24-
* - Adds Browser Animation to app.module
2521
*/
2622
export default function (options: Schema): Rule {
2723
return async (host: Tree, context: SchematicContext) => {
@@ -30,7 +26,6 @@ export default function (options: Schema): Rule {
3026

3127
if (project.extensions['projectType'] === ProjectType.Application) {
3228
return chain([
33-
addAnimations(options),
3429
addThemeToAppStyles(options),
3530
addFontsToIndex(options),
3631
addMaterialAppStyles(options),
@@ -91,32 +86,3 @@ function addMaterialAppStyles(options: Schema) {
9186
host.commitUpdate(recorder);
9287
};
9388
}
94-
95-
/** Adds the animations package to the project based on the conffiguration. */
96-
function addAnimations(options: Schema): Rule {
97-
return (host: Tree, context: SchematicContext) => {
98-
const animationsRule =
99-
options.animations === 'excluded'
100-
? noop()
101-
: addRootProvider(options.project, ({code, external}) => {
102-
return code`${external(
103-
'provideAnimationsAsync',
104-
'@angular/platform-browser/animations/async',
105-
)}(${options.animations === 'disabled' ? `'noop'` : ''})`;
106-
});
107-
108-
// The `addRootProvider` rule can throw in some custom scenarios (see #28640).
109-
// Add some error handling around it so the setup isn't interrupted.
110-
return callRule(animationsRule, host, context).pipe(
111-
catchError(() => {
112-
context.logger.error(
113-
'Failed to add animations to project. Continuing with the Angular Material setup.',
114-
);
115-
context.logger.info(
116-
'Read more about setting up the animations manually: https://angular.dev/guide/animations',
117-
);
118-
return observableOf(host);
119-
}),
120-
);
121-
};
122-
}

0 commit comments

Comments
 (0)