Skip to content

Commit 6080558

Browse files
committed
feat(ng-add): allow using noop animations
* Currently we always install `@angular/animations` and add the `BrowserAnimationsModule` to the project module. There should be a prompt/option that allows developers to use the `NoopAnimationsModule`. * Also no longer adds the `BrowserAnimationsModule` if the project already uses the `NoopAnimationsModule` (avoiding magic). Same applies for the `NoopAnimationsModule`. We won't add the noop animations module if the browser animations module is configured.
1 parent a620fca commit 6080558

File tree

5 files changed

+58
-10
lines changed

5 files changed

+58
-10
lines changed

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,19 @@ describe('ng-add schematic', () => {
2727
`Expected "${filePath}" to be added to the project styles in the workspace.`);
2828
}
2929

30+
/** Removes the specified dependency from the /package.json in the given tree. */
31+
function removePackageJsonDependency(tree: Tree, dependencyName: string) {
32+
const packageContent = JSON.parse(getFileContent(tree, '/package.json'));
33+
delete packageContent.dependencies[dependencyName];
34+
tree.overwrite('/package.json', JSON.stringify(packageContent, null, 2));
35+
}
36+
3037
it('should update package.json', () => {
38+
// By default, the Angular workspace schematic sets up "@angular/animations". In order
39+
// to verify that we would set up the dependency properly if someone doesn't have the
40+
// animations installed already, we remove the animations dependency explicitly.
41+
removePackageJsonDependency(appTree, '@angular/animations');
42+
3143
const tree = runner.runSchematic('ng-add', {}, appTree);
3244
const packageJson = JSON.parse(getFileContent(tree, '/package.json'));
3345
const dependencies = packageJson.dependencies;
@@ -142,4 +154,28 @@ describe('ng-add schematic', () => {
142154
'Expected the project main file to not contain a HammerJS import.');
143155
});
144156
});
157+
158+
describe('animations disabled', () => {
159+
160+
it('should not add @angular/animations to package.json', () => {
161+
// By default, the Angular workspace schematic sets up "@angular/animations". In order
162+
// to verify that we don't add the animations if the Animations are not installed, we need
163+
// to remove the dependency for this unit test tree.
164+
removePackageJsonDependency(appTree, '@angular/animations');
165+
166+
const tree = runner.runSchematic('ng-add', {animations: false}, appTree);
167+
const packageJson = JSON.parse(getFileContent(tree, '/package.json'));
168+
169+
expect(packageJson.dependencies['@angular/animations'])
170+
.toBeUndefined(`Expected '@angular/animations' to be not added to the package.json`);
171+
});
172+
173+
it('should not add the BrowserAnimationsModule to the project module', () => {
174+
const tree = runner.runSchematic('ng-add', {gestures: false}, appTree);
175+
const fileContent = getFileContent(tree, '/projects/material/src/app/app.module.ts');
176+
177+
expect(fileContent).not.toContain('BrowserAnimationsModule',
178+
'Expected the project app module to not import the "BrowserAnimationsModule".');
179+
});
180+
});
145181
});

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,18 @@ import {hammerjsVersion, materialVersion, requiredAngularVersionRange} from './v
2121
*/
2222
export default function(options: Schema): Rule {
2323
return (host: Tree, context: SchematicContext) => {
24-
// Version tag of the `@angular/core` dependency that has been loaded from the `package.json`
25-
// of the CLI project. This tag should be preferred because all Angular dependencies should
26-
// have the same version tag if possible.
27-
const ngCoreVersionTag = getPackageVersionFromPackageJson(host, '@angular/core');
28-
2924
addPackageToPackageJson(host, '@angular/cdk', `^${materialVersion}`);
3025
addPackageToPackageJson(host, '@angular/material', `^${materialVersion}`);
31-
addPackageToPackageJson(host, '@angular/animations',
32-
ngCoreVersionTag || requiredAngularVersionRange);
26+
27+
if (options.animations) {
28+
// Version tag of the `@angular/core` dependency that has been loaded from the `package.json`
29+
// of the CLI project. This tag should be preferred because all Angular dependencies should
30+
// have the same version tag if possible.
31+
const ngCoreVersionTag = getPackageVersionFromPackageJson(host, '@angular/core');
32+
33+
addPackageToPackageJson(host, '@angular/animations',
34+
ngCoreVersionTag || requiredAngularVersionRange);
35+
}
3336

3437
if (options.gestures) {
3538
addPackageToPackageJson(host, 'hammerjs', hammerjsVersion);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
"default": true,
2323
"description": "Whether gesture support should be set up or not.",
2424
"x-prompt": "Set up HammerJS for gesture recognition?"
25+
},
26+
"animations": {
27+
"type": "boolean",
28+
"default": true,
29+
"description": "Whether animations should be set up or not.",
30+
"x-prompt": "Set up animations for Angular Material?"
2531
}
2632
},
2733
"required": []

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ export interface Schema {
1414
/** Whether gesture support should be set up or not. */
1515
gestures: boolean;
1616

17+
/** Whether animations should be set up or not. */
18+
animations: boolean;
19+
1720
/** Name of pre-built theme to install. */
1821
theme: 'indigo-pink' | 'deeppurple-amber' | 'pink-bluegrey' | 'purple-green' | 'custom';
1922
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ export default function(options: Schema): Rule {
3232
}
3333

3434
return chain([
35+
options && options.animations ? addAnimationsModule(options) : noop(),
3536
options && options.gestures ? addHammerJsToMain(options) : noop(),
3637
addThemeToAppStyles(options),
37-
addAnimationRootConfig(options),
3838
addFontsToIndex(options),
3939
addMaterialAppStyles(options),
4040
]);
4141
}
4242

43-
/** Add browser animation module to the app module file. */
44-
function addAnimationRootConfig(options: Schema) {
43+
/** Adds the BrowserAnimationModule to the app module of the specified project. */
44+
function addAnimationsModule(options: Schema) {
4545
return (host: Tree) => {
4646
const workspace = getWorkspace(host);
4747
const project = getProjectFromWorkspace(workspace, options.project);

0 commit comments

Comments
 (0)