Skip to content

Commit

Permalink
fix(angular): stop using npmScope as a prefix for component and direc…
Browse files Browse the repository at this point in the history
…tive selectors (nrwl#21828)
  • Loading branch information
leosvelperez authored Feb 21, 2024
1 parent fe72ab4 commit cfa0815
Show file tree
Hide file tree
Showing 41 changed files with 364 additions and 155 deletions.
6 changes: 6 additions & 0 deletions docs/generated/cli/create-nx-workspace.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ Default: `npm`

Package manager to use

### prefix

Type: `string`

Prefix to use for Angular component and directive selectors.

### preset

Type: `string`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"type": "string",
"format": "html-selector",
"description": "The prefix to apply to generated selectors.",
"default": "app",
"alias": "p"
},
"skipTests": {
Expand Down
6 changes: 6 additions & 0 deletions docs/generated/packages/nx/documents/create-nx-workspace.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ Default: `npm`

Package manager to use

### prefix

Type: `string`

Prefix to use for Angular component and directive selectors.

### preset

Type: `string`
Expand Down
4 changes: 4 additions & 0 deletions docs/generated/packages/workspace/generators/new.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@
"description": "Enable Server-Side Rendering (SSR) and Static Site Generation (SSG/Prerendering) for the Angular application.",
"type": "boolean",
"default": false
},
"prefix": {
"description": "The prefix to use for Angular component and directive selectors.",
"type": "string"
}
},
"additionalProperties": true,
Expand Down
4 changes: 4 additions & 0 deletions docs/generated/packages/workspace/generators/preset.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@
"description": "Enable Server-Side Rendering (SSR) and Static Site Generation (SSG/Prerendering) for the Angular application.",
"type": "boolean",
"default": false
},
"prefix": {
"description": "The prefix to use for Angular component and directive selectors.",
"type": "string"
}
},
"required": ["preset", "name"],
Expand Down
4 changes: 2 additions & 2 deletions e2e/angular-module-federation/src/module-federation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ describe('Angular Module Federation', () => {
import { isEven } from '${remote}/${module}';
@Component({
selector: 'proj-root',
selector: 'app-root',
template: \`<div class="host">{{title}}</div>\`,
standalone: true
})
Expand Down Expand Up @@ -433,7 +433,7 @@ describe('Angular Module Federation', () => {
import { isEven } from '${childRemote}/${module}';
@Component({
selector: 'proj-${remote}-entry',
selector: 'app-${remote}-entry',
template: \`<div class="childremote">{{title}}</div>\`,
standalone: true
})
Expand Down
6 changes: 6 additions & 0 deletions e2e/utils/create-project-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ export function runCreateWorkspace(
e2eTestRunner,
ssr,
framework,
prefix,
}: {
preset: string;
appName?: string;
Expand All @@ -251,6 +252,7 @@ export function runCreateWorkspace(
e2eTestRunner?: 'cypress' | 'playwright' | 'jest' | 'detox' | 'none';
ssr?: boolean;
framework?: string;
prefix?: string;
}
) {
projName = name;
Expand Down Expand Up @@ -317,6 +319,10 @@ export function runCreateWorkspace(
command += ` --ssr=${ssr}`;
}

if (prefix !== undefined) {
command += ` --prefix=${prefix}`;
}

try {
const create = execSync(`${command}${isVerbose() ? ' --verbose' : ''}`, {
cwd,
Expand Down
4 changes: 2 additions & 2 deletions e2e/workspace-create/src/create-nx-workspace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ describe('create-nx-workspace', () => {

it('should fail correctly when preset errors', () => {
// Using Angular Preset as the example here to test
// It will error when npmScope is of form `<char>-<num>-<char>`
// Due to a validation error Angular will throw.
// It will error when prefix is not valid
const wsName = uniq('angular-1-test');
const appName = uniq('app');
expect(() =>
Expand All @@ -171,6 +170,7 @@ describe('create-nx-workspace', () => {
e2eTestRunner: 'none',
bundler: 'webpack',
ssr: false,
prefix: '1-one',
})
).toThrow();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ exports[`app --minimal should skip "nx-welcome.component.ts" file and references
"import { Component } from '@angular/core';
@Component({
selector: 'proj-root',
selector: 'app-root',
templateUrl: './app.component.html',
styleUrl: './app.component.css',
})
Expand Down Expand Up @@ -83,7 +83,7 @@ exports[`app --minimal should skip "nx-welcome.component.ts" file and references
"import { Component } from '@angular/core';
@Component({
selector: 'proj-root',
selector: 'app-root',
templateUrl: './app.component.html',
styleUrl: './app.component.css',
})
Expand Down Expand Up @@ -127,7 +127,7 @@ import { RouterModule } from '@angular/router';
@Component({
standalone: true,
imports: [RouterModule],
selector: 'proj-root',
selector: 'app-root',
templateUrl: './app.component.html',
styleUrl: './app.component.css',
})
Expand Down Expand Up @@ -170,7 +170,7 @@ exports[`app --minimal should skip "nx-welcome.component.ts" file and references
@Component({
standalone: true,
imports: [],
selector: 'proj-root',
selector: 'app-root',
templateUrl: './app.component.html',
styleUrl: './app.component.css',
})
Expand Down Expand Up @@ -210,7 +210,7 @@ exports[`app --project-name-and-root-format=derived should generate correctly wh
{
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
"name": "my-dir-my-app",
"prefix": "proj",
"prefix": "app",
"projectType": "application",
"root": "apps/my-dir/my-app",
"sourceRoot": "apps/my-dir/my-app/src",
Expand Down Expand Up @@ -409,7 +409,7 @@ exports[`app --project-name-and-root-format=derived should generate correctly wh
{
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"name": "my-app",
"prefix": "proj",
"prefix": "app",
"projectType": "application",
"root": "apps/my-app",
"sourceRoot": "apps/my-app/src",
Expand Down Expand Up @@ -641,7 +641,7 @@ import { NxWelcomeComponent } from './nx-welcome.component';
@Component({
standalone: true,
imports: [NxWelcomeComponent, RouterModule],
selector: 'proj-root',
selector: 'app-root',
templateUrl: './app.component.html',
styleUrl: './app.component.css',
})
Expand Down Expand Up @@ -709,7 +709,7 @@ import { NxWelcomeComponent } from './nx-welcome.component';
@Component({
standalone: true,
imports: [NxWelcomeComponent, ],
selector: 'proj-root',
selector: 'app-root',
templateUrl: './app.component.html',
styleUrl: './app.component.css',
})
Expand Down Expand Up @@ -850,7 +850,7 @@ exports[`app format files should format files 2`] = `
"import { Component } from '@angular/core';
@Component({
selector: 'proj-root',
selector: 'app-root',
templateUrl: './app.component.html',
styleUrl: './app.component.css',
})
Expand Down Expand Up @@ -903,7 +903,7 @@ exports[`app nested should create project configs 1`] = `
{
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"name": "my-app",
"prefix": "proj",
"prefix": "app",
"projectType": "application",
"root": "my-dir/my-app",
"sourceRoot": "my-dir/my-app/src",
Expand Down Expand Up @@ -1015,7 +1015,7 @@ exports[`app not nested should create project configs 1`] = `
{
"$schema": "../node_modules/nx/schemas/project-schema.json",
"name": "my-app",
"prefix": "proj",
"prefix": "app",
"projectType": "application",
"root": "my-app",
"sourceRoot": "my-app/src",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ describe('app', () => {
await generateApp(appTree, 'my-app', { directory: 'my-dir/my-app' });
expect(
appTree.read('my-dir/my-app/src/app/app.component.html', 'utf-8')
).toContain('<proj-nx-welcome></proj-nx-welcome>');
).toContain('<app-nx-welcome></app-nx-welcome>');
});

it("should update `template`'s property of AppComponent with Nx content", async () => {
Expand All @@ -459,7 +459,7 @@ describe('app', () => {
});
expect(
appTree.read('my-dir/my-app/src/app/app.component.ts', 'utf-8')
).toContain('<proj-nx-welcome></proj-nx-welcome>');
).toContain('<app-nx-welcome></app-nx-welcome>');
});

it('should create Nx specific `nx-welcome.component.ts` file', async () => {
Expand Down Expand Up @@ -598,15 +598,15 @@ describe('app', () => {
"@angular-eslint/component-selector": [
"error",
{
"prefix": "proj",
"prefix": "app",
"style": "kebab-case",
"type": "element",
},
],
"@angular-eslint/directive-selector": [
"error",
{
"prefix": "proj",
"prefix": "app",
"style": "camelCase",
"type": "attribute",
},
Expand Down
10 changes: 8 additions & 2 deletions packages/angular/src/generators/application/lib/create-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getRelativePathToRootTsConfig, getRootTsConfigFileName } from '@nx/js';
import { createTsConfig } from '../../utils/create-ts-config';
import { UnitTestRunner } from '../../../utils/test-runners';
import { getInstalledAngularVersionInfo } from '../../utils/version-utils';
import { validateHtmlSelector } from '../../utils/selector';

export async function createFiles(
tree: Tree,
Expand All @@ -15,8 +16,13 @@ export async function createFiles(
const isUsingApplicationBuilder =
angularMajorVersion >= 17 && options.bundler === 'esbuild';

const rootSelector = `${options.prefix}-root`;
validateHtmlSelector(rootSelector);
const nxWelcomeSelector = `${options.prefix}-nx-welcome`;
validateHtmlSelector(nxWelcomeSelector);

const substitutions = {
rootSelector: `${options.prefix}-root`,
rootSelector,
appName: options.name,
inlineStyle: options.inlineStyle,
inlineTemplate: options.inlineTemplate,
Expand All @@ -25,7 +31,7 @@ export async function createFiles(
unitTesting: options.unitTestRunner !== UnitTestRunner.None,
routing: options.routing,
minimal: options.minimal,
nxWelcomeSelector: `${options.prefix}-nx-welcome`,
nxWelcomeSelector,
rootTsConfig: joinPathFragments(rootOffset, getRootTsConfigFileName(tree)),
angularMajorVersion,
rootOffset,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { joinPathFragments, type Tree } from '@nx/devkit';
import { determineProjectNameAndRootOptions } from '@nx/devkit/src/generators/project-name-and-root-utils';
import { Linter } from '@nx/eslint';
import { getNpmScope } from '@nx/js/src/utils/package-json/get-npm-scope';
import { E2eTestRunner, UnitTestRunner } from '../../../utils/test-runners';
import { normalizeNewProjectPrefix } from '../../utils/project';
import type { Schema } from '../schema';
import type { NormalizedSchema } from './normalized-schema';
import { getInstalledAngularVersionInfo } from '../../utils/version-utils';
Expand Down Expand Up @@ -34,12 +32,6 @@ export async function normalizeOptions(
? options.tags.split(',').map((s) => s.trim())
: [];

const prefix = normalizeNewProjectPrefix(
options.prefix,
getNpmScope(host),
'app'
);

let bundler = options.bundler;
if (!bundler) {
const { major: angularMajorVersion } = getInstalledAngularVersionInfo(host);
Expand All @@ -60,7 +52,7 @@ export async function normalizeOptions(
strict: true,
standalone: true,
...options,
prefix,
prefix: options.prefix || 'app',
name: appProjectName,
appProjectRoot,
appProjectSourceRoot: `${appProjectRoot}/src`,
Expand Down
1 change: 1 addition & 0 deletions packages/angular/src/generators/application/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"type": "string",
"format": "html-selector",
"description": "The prefix to apply to generated selectors.",
"default": "app",
"alias": "p"
},
"skipTests": {
Expand Down
Loading

0 comments on commit cfa0815

Please sign in to comment.