Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Apr 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
67 changes: 66 additions & 1 deletion packages/angular/src/generators/remote/remote.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 {}`;
Comment on lines +69 to +73
Copy link
Member

Choose a reason for hiding this comment

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

imho, it would be better if we have the full template for the component here rather than splitting by a property based on what was previously generated. It's a small component, so the template would be small. It should be clearer, and we'd also reduce the potential maintenance associated with relying on a previous generation that could change and that we don't need to rely on. Regardless of a previous generation, we know the content of what we want in this component.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Very good point. I will add this as an additional cleanup PR


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 {}`
);
}