Skip to content

Commit

Permalink
feat(misc): conditionally allow starter template inclusion (#14268)
Browse files Browse the repository at this point in the history
Co-authored-by: Colum Ferry <cferry09@gmail.com>
  • Loading branch information
DirkoOdendaal and Coly010 authored Jan 27, 2023
1 parent dba4a06 commit ee909cf
Show file tree
Hide file tree
Showing 23 changed files with 275 additions and 859 deletions.
5 changes: 5 additions & 0 deletions docs/generated/packages/angular/generators/application.json
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@
"default": false,
"hidden": true,
"x-priority": "internal"
},
"minimal": {
"description": "Generate a Angular app with a minimal setup.",
"type": "boolean",
"default": false
}
},
"additionalProperties": false,
Expand Down
5 changes: 5 additions & 0 deletions docs/generated/packages/react/generators/host.json
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@
"description": "Whether to configure SSR for the host application",
"type": "boolean",
"default": false
},
"minimal": {
"description": "Generate a React app with a minimal setup. No nx starter template.",
"type": "boolean",
"default": false
}
},
"required": ["name"],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`app --minimal should skip Nx specific \`nx-welcome.component.ts\` file creation 1`] = `
"import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
"
`;

exports[`app --standalone should generate a standalone app correctly with routing 1`] = `
"import { bootstrapApplication } from '@angular/platform-browser';
import { provideRouter, withEnabledBlockingInitialNavigation } from '@angular/router';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,26 @@ export async function applicationGenerator(
updateConfigFiles(host, options);
updateAppComponentTemplate(host, options);

// Create the NxWelcomeComponent
const angularComponentSchematic = wrapAngularDevkitSchematic(
'@schematics/angular',
'component'
);
await angularComponentSchematic(host, {
name: 'NxWelcome',
inlineTemplate: true,
inlineStyle: true,
prefix: options.prefix,
skipTests: true,
style: options.style,
flat: true,
viewEncapsulation: 'None',
project: options.name,
standalone: options.standalone,
});
updateNxComponentTemplate(host, options);
if (!options.minimal) {
// Create the NxWelcomeComponent
const angularComponentSchematic = wrapAngularDevkitSchematic(
'@schematics/angular',
'component'
);
await angularComponentSchematic(host, {
name: 'NxWelcome',
inlineTemplate: true,
inlineStyle: true,
prefix: options.prefix,
skipTests: true,
style: options.style,
flat: true,
viewEncapsulation: 'None',
project: options.name,
standalone: options.standalone,
});
updateNxComponentTemplate(host, options);
}

if (options.addTailwind) {
await setupTailwindGenerator(host, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ export interface Schema {
skipPackageJson?: boolean;
standalone?: boolean;
rootProject?: boolean;
minimal?: boolean;
}
13 changes: 13 additions & 0 deletions packages/angular/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,19 @@ describe('app', () => {
.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.`);
});

describe('--minimal', () => {
it('should skip Nx specific `nx-welcome.component.ts` file creation', async () => {
await generateApp(appTree, 'plain', { minimal: true });

expect(
appTree.read('apps/plain/src/app/app.module.ts', 'utf-8')
).toMatchSnapshot();
expect(
appTree.exists('apps/plain/src/app/nx-welcome.component.ts')
).toBeFalsy();
});
});
});

async function generateApp(
Expand Down
38 changes: 20 additions & 18 deletions packages/angular/src/generators/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,24 +92,26 @@ export async function applicationGenerator(
updateConfigFiles(tree, options);
updateAppComponentTemplate(tree, options);

// Create the NxWelcomeComponent
const angularComponentSchematic = wrapAngularDevkitSchematic(
'@schematics/angular',
'component'
);
await angularComponentSchematic(tree, {
name: 'NxWelcome',
inlineTemplate: true,
inlineStyle: true,
prefix: options.prefix,
skipTests: true,
style: options.style,
flat: true,
viewEncapsulation: 'None',
project: options.name,
standalone: options.standalone,
});
updateNxComponentTemplate(tree, options);
if (!options.minimal) {
// Create the NxWelcomeComponent
const angularComponentSchematic = wrapAngularDevkitSchematic(
'@schematics/angular',
'component'
);
await angularComponentSchematic(tree, {
name: 'NxWelcome',
inlineTemplate: true,
inlineStyle: true,
prefix: options.prefix,
skipTests: true,
style: options.style,
flat: true,
viewEncapsulation: 'None',
project: options.name,
standalone: options.standalone,
});
updateNxComponentTemplate(tree, options);
}

if (options.addTailwind) {
await setupTailwindGenerator(tree, {
Expand Down
1 change: 1 addition & 0 deletions packages/angular/src/generators/application/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ export interface Schema {
skipPackageJson?: boolean;
standalone?: boolean;
rootProject?: boolean;
minimal?: boolean;
}
5 changes: 5 additions & 0 deletions packages/angular/src/generators/application/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@
"default": false,
"hidden": true,
"x-priority": "internal"
},
"minimal": {
"description": "Generate a Angular app with a minimal setup.",
"type": "boolean",
"default": false
}
},
"additionalProperties": false,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`app --minimal should create default application without Nx welcome component 1`] = `
"
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import styles from './app.module.css';
export function App() {
return (
<>
<h1>
<span> Hello there, </span>
Welcome plain 👋
</h1>
<div />
</>);
}
export default App;
if (import.meta.vitest) {
// add tests related to your file here
// For more information please visit the Vitest docs site here: https://vitest.dev/guide/in-source.html
const { it, expect, beforeEach } = import.meta.vitest;
let render: any;
beforeEach(async () => {
render = (await import('@testing-library/react')).render;
});
it('should render successfully', () => {
const { baseElement } = render(<App />);
expect(baseElement).toBeTruthy();
});
it('should have a greeting as the title', () => {
const { getByText } = render(<App />);
expect(getByText(/Welcome plain/gi)).toBeTruthy();
});
}
"
`;

exports[`app not nested should generate files 1`] = `
"
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import styles from './app.module.css';
import NxWelcome from \\"./nx-welcome\\";
export function App() {
return (
<>
<NxWelcome title=\\"my-app\\"/>
<div />
</>);
}
export default App;
"
`;

exports[`app should create Nx specific template 1`] = `
"
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import styles from './app.module.css';
import NxWelcome from \\"./nx-welcome\\";
export function App() {
return (
<>
<NxWelcome title=\\"my-dir-my-app\\"/>
<div />
</>);
}
export default App;
"
`;
21 changes: 19 additions & 2 deletions packages/react/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ describe('app', () => {
expect(appTree.exists('apps/my-app/.babelrc')).toBeTruthy();
expect(appTree.exists('apps/my-app/src/main.tsx')).toBeTruthy();
expect(appTree.exists('apps/my-app/src/app/app.tsx')).toBeTruthy();
expect(
appTree.read('apps/my-app/src/app/app.tsx', 'utf-8')
).toMatchSnapshot();
expect(appTree.exists('apps/my-app/src/app/app.spec.tsx')).toBeTruthy();
expect(appTree.exists('apps/my-app/src/app/app.module.css')).toBeTruthy();

Expand Down Expand Up @@ -235,8 +238,8 @@ describe('app', () => {
await applicationGenerator(appTree, { ...schema, directory: 'myDir' });

expect(
appTree.read('apps/my-dir/my-app/src/app/app.tsx').toString()
).toContain(`<NxWelcome title="my-dir-my-app"/>`);
appTree.read('apps/my-dir/my-app/src/app/app.tsx', 'utf-8')
).toMatchSnapshot();
expect(
appTree.read('apps/my-dir/my-app/src/app/nx-welcome.tsx').toString()
).toContain('Hello there');
Expand Down Expand Up @@ -835,6 +838,20 @@ describe('app', () => {
});
});

describe('--minimal', () => {
it('should create default application without Nx welcome component', async () => {
await applicationGenerator(appTree, {
...schema,
name: 'plain',
minimal: true,
});
expect(appTree.exists('apps/plain/src/app/nx-welcome.tsx')).toBeFalsy();
expect(
appTree.read('apps/plain/src/app/app.tsx', 'utf-8')
).toMatchSnapshot();
});
});

describe('--js', () => {
it('generates JS files', async () => {
await applicationGenerator(appTree, {
Expand Down
Loading

1 comment on commit ee909cf

@vercel
Copy link

@vercel vercel bot commented on ee909cf Jan 27, 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
nx-dev-git-master-nrwl.vercel.app
nx-dev-nrwl.vercel.app

Please sign in to comment.