Skip to content

Commit c3bca6c

Browse files
committed
feat(material/schematics): add option not to include animations module in ng-add
Adds a third option to the `ng-add` schematic that allows users to opt out of including any of the animations modules.
1 parent 7cc42f5 commit c3bca6c

File tree

4 files changed

+37
-15
lines changed

4 files changed

+37
-15
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ describe('ng-add schematic', () => {
210210
describe('animations disabled', () => {
211211
it('should add the NoopAnimationsModule to the project module', async () => {
212212
const tree =
213-
await runner.runSchematicAsync('ng-add-setup-project', {animations: false}, appTree)
213+
await runner.runSchematicAsync('ng-add-setup-project', {animations: 'disabled'}, appTree)
214214
.toPromise();
215215
const fileContent = getFileContent(tree, '/projects/material/src/app/app.module.ts');
216216

@@ -237,6 +237,20 @@ describe('ng-add schematic', () => {
237237
});
238238
});
239239

240+
describe('animations excluded', () => {
241+
it('should not add any animations code if animations are excluded', async () => {
242+
const tree =
243+
await runner.runSchematicAsync('ng-add-setup-project', {animations: 'excluded'}, appTree)
244+
.toPromise();
245+
const fileContent = getFileContent(tree, '/projects/material/src/app/app.module.ts');
246+
247+
expect(fileContent).not.toContain('NoopAnimationsModule');
248+
expect(fileContent).not.toContain('BrowserAnimationsModule');
249+
expect(fileContent).not.toContain('@angular/platform-browser/animations');
250+
expect(fileContent).not.toContain('@angular/animations');
251+
});
252+
});
253+
240254
describe('custom project builders', () => {
241255
/** Overwrites a target builder for the workspace in the given tree */
242256
function overwriteTargetBuilder(tree: Tree, targetName: 'build' | 'test', newBuilder: string) {

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,18 @@
3434
"x-prompt": "Set up global Angular Material typography styles?"
3535
},
3636
"animations": {
37-
"type": "boolean",
38-
"default": true,
39-
"description": "Whether Angular browser animations should be set up.",
40-
"x-prompt": "Set up browser animations for Angular Material?"
37+
"type": "string",
38+
"default": "enabled",
39+
"description": "Whether Angular browser animations should be included.",
40+
"x-prompt": {
41+
"message": "Include the Angular animations module?",
42+
"type": "list",
43+
"items": [
44+
{ "value": "enabled", "label": "Include and enable animations" },
45+
{ "value": "disabled", "label": "Include, but disable animations" },
46+
{ "value": "excluded", "label": "Do not include" }
47+
]
48+
}
4149
}
4250
},
4351
"required": []

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ export interface Schema {
1010
/** Name of the project. */
1111
project: string;
1212

13-
/** Whether Angular browser animations should be set up. */
14-
animations: boolean;
13+
/** Whether the Angular browser animations module should be included and enabled. */
14+
animations: 'enabled' | 'disabled' | 'excluded';
1515

1616
/** Name of pre-built theme to install. */
1717
theme: 'indigo-pink' | 'deeppurple-amber' | 'pink-bluegrey' | 'purple-green' | 'custom';

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ function addAnimationsModule(options: Schema) {
6767
const project = getProjectFromWorkspace(workspace, options.project);
6868
const appModulePath = getAppModulePath(host, getProjectMainFile(project));
6969

70-
if (options.animations) {
70+
if (options.animations === 'enabled') {
7171
// In case the project explicitly uses the NoopAnimationsModule, we should print a warning
7272
// message that makes the user aware of the fact that we won't automatically set up
7373
// animations. If we would add the BrowserAnimationsModule while the NoopAnimationsModule
@@ -77,14 +77,14 @@ function addAnimationsModule(options: Schema) {
7777
`Could not set up "${browserAnimationsModuleName}" ` +
7878
`because "${noopAnimationsModuleName}" is already imported.`);
7979
context.logger.info(`Please manually set up browser animations.`);
80-
return;
80+
} else {
81+
addModuleImportToRootModule(host, browserAnimationsModuleName,
82+
'@angular/platform-browser/animations', project);
8183
}
82-
83-
addModuleImportToRootModule(host, browserAnimationsModuleName,
84-
'@angular/platform-browser/animations', project);
85-
} else if (!hasNgModuleImport(host, appModulePath, browserAnimationsModuleName)) {
86-
// Do not add the NoopAnimationsModule module if the project already explicitly uses
87-
// the BrowserAnimationsModule.
84+
} else if (options.animations === 'disabled' &&
85+
// Do not add the NoopAnimationsModule module if the project
86+
// already explicitly uses the BrowserAnimationsModule.
87+
!hasNgModuleImport(host, appModulePath, browserAnimationsModuleName)) {
8888
addModuleImportToRootModule(host, noopAnimationsModuleName,
8989
'@angular/platform-browser/animations', project);
9090
}

0 commit comments

Comments
 (0)