Skip to content

Commit

Permalink
fix(angular): correctly error component when standalone is used in An…
Browse files Browse the repository at this point in the history
…gular < 14.1.0 (#14337)
  • Loading branch information
Coly010 authored Jan 13, 2023
1 parent 9d626b8 commit 46b10c2
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/generated/packages/angular/generators/component.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"alias": "t"
},
"standalone": {
"description": "Whether the generated component is standalone.",
"description": "Whether the generated component is standalone. _Note: This is only supported in Angular versions >= 14.1.0_",
"type": "boolean",
"default": false
},
Expand Down
35 changes: 34 additions & 1 deletion packages/angular/src/generators/component/component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import type { ProjectGraph } from '@nrwl/devkit';
import { addProjectConfiguration, writeJson } from '@nrwl/devkit';
import {
addProjectConfiguration,
stripIndents,
updateJson,
writeJson,
} from '@nrwl/devkit';
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import componentGenerator from './component';

Expand Down Expand Up @@ -737,4 +742,32 @@ describe('component Generator', () => {
expect(indexSource).toBe('');
});
});

it('should error correctly when Angular version does not support standalone', async () => {
// ARRANGE
const tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' });
updateJson(tree, 'package.json', (json) => ({
...json,
dependencies: {
'@angular/core': '14.0.0',
},
}));

addProjectConfiguration(tree, 'lib1', {
projectType: 'library',
sourceRoot: 'libs/lib1/src',
root: 'libs/lib1',
});

// ACT & ASSERT
await expect(
componentGenerator(tree, {
name: 'example',
project: 'lib1',
standalone: true,
})
).rejects
.toThrow(stripIndents`The "standalone" option is only supported in Angular >= 14.1.0. You are currently using 14.0.0.
You can resolve this error by removing the "standalone" option or by migrating to Angular 14.1.0.`);
});
});
13 changes: 13 additions & 0 deletions packages/angular/src/generators/component/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,27 @@ import {
normalizePath,
readNxJson,
readProjectConfiguration,
stripIndents,
} from '@nrwl/devkit';
import { wrapAngularDevkitSchematic } from '@nrwl/devkit/ngcli-adapter';
import { pathStartsWith } from '../utils/path';
import { exportComponentInEntryPoint } from './lib/component';
import { normalizeOptions } from './lib/normalize-options';
import type { NormalizedSchema, Schema } from './schema';
import { getInstalledAngularVersionInfo } from '../utils/angular-version-utils';
import { lt } from 'semver';

export async function componentGenerator(tree: Tree, rawOptions: Schema) {
const installedAngularVersionInfo = getInstalledAngularVersionInfo(tree);

if (
lt(installedAngularVersionInfo.version, '14.1.0') &&
rawOptions.standalone
) {
throw new Error(stripIndents`The "standalone" option is only supported in Angular >= 14.1.0. You are currently using ${installedAngularVersionInfo.version}.
You can resolve this error by removing the "standalone" option or by migrating to Angular 14.1.0.`);
}

const options = await normalizeOptions(tree, rawOptions);
const { projectSourceRoot, ...schematicOptions } = options;

Expand Down
2 changes: 1 addition & 1 deletion packages/angular/src/generators/component/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"alias": "t"
},
"standalone": {
"description": "Whether the generated component is standalone.",
"description": "Whether the generated component is standalone. _Note: This is only supported in Angular versions >= 14.1.0_",
"type": "boolean",
"default": false
},
Expand Down

1 comment on commit 46b10c2

@vercel
Copy link

@vercel vercel bot commented on 46b10c2 Jan 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-five.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx-dev-nrwl.vercel.app
nx.dev

Please sign in to comment.