feat(angular): remove unnecessarily generated code for remotes (#9844)

This commit is contained in:
Colum Ferry 2022-04-19 10:36:56 +01:00 committed by GitHub
parent 4f1c14cb75
commit fc235de1f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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: '<router-outlet></router-outlet>'
})
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 {}`
);
}