From 616155ca1ea80c0bc536aa5e1cd7f476623c6514 Mon Sep 17 00:00:00 2001 From: Colum Ferry Date: Thu, 14 Apr 2022 15:23:44 +0100 Subject: [PATCH] feat(angular): remove unnecessarily generated code for remotes --- .../angular/src/generators/remote/remote.ts | 67 ++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/packages/angular/src/generators/remote/remote.ts b/packages/angular/src/generators/remote/remote.ts index 0131ea4863f88..11813644264ec 100644 --- a/packages/angular/src/generators/remote/remote.ts +++ b/packages/angular/src/generators/remote/remote.ts @@ -1,4 +1,4 @@ -import type { Tree } from '@nrwl/devkit'; +import { joinPathFragments, Tree } from '@nrwl/devkit'; import type { Schema } from './schema'; import { getProjects, readProjectConfiguration } from '@nrwl/devkit'; import applicationGenerator from '../application/application'; @@ -36,5 +36,70 @@ export default async function remote(tree: Tree, options: Schema) { port: options.port ?? findNextAvailablePort(tree), }); + removeDeadCode(tree, options); + return installTask; } + +function removeDeadCode(tree: Tree, options: Schema) { + const project = readProjectConfiguration(tree, options.name); + + ['css', 'less', 'scss', 'sass'].forEach((style) => { + const pathToComponentStyle = joinPathFragments( + project.sourceRoot, + `app/app.component.${style}` + ); + if (tree.exists(pathToComponentStyle)) { + tree.delete(pathToComponentStyle); + } + }); + + tree.delete( + joinPathFragments(project.sourceRoot, 'app/nx-welcome.component.ts') + ); + tree.delete( + joinPathFragments(project.sourceRoot, 'app/app.component.spec.ts') + ); + tree.delete(joinPathFragments(project.sourceRoot, 'app/app.component.html')); + + const pathToComponent = joinPathFragments( + project.sourceRoot, + 'app/app.component.ts' + ); + const component = + tree.read(pathToComponent, 'utf-8').split('templateUrl')[0] + + `template: '' +}) +export class AppComponent {}`; + + tree.write(pathToComponent, component); + + tree.write( + joinPathFragments(project.sourceRoot, 'app/app.module.ts'), + `/* +* This RemoteEntryModule is imported here to allow TS to find the Module during +* compilation, allowing it to be included in the built bundle. This is required +* for the Module Federation Plugin to expose the Module correctly. +* */ +import { RemoteEntryModule } from './remote-entry/entry.module'; +import { NgModule } from '@angular/core'; +import { BrowserModule } from '@angular/platform-browser'; + +import { AppComponent } from './app.component'; +import { RouterModule } from '@angular/router'; + +@NgModule({ + declarations: [AppComponent], + imports: [ + BrowserModule, + RouterModule.forRoot([{ + path: '', + loadChildren: () => import('./remote-entry/entry.module').then(m => m.RemoteEntryModule) + }], { initialNavigation: 'enabledBlocking' }), + ], + providers: [], + bootstrap: [AppComponent], +}) +export class AppModule {}` + ); +}