Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing unused Angular module dependencies (imports and providers)

Let's say I have SharedComponents module. In this module there is a SomeView component. This component used a directive SomeDirective from DirectivesModule. I have something like this:

@NgModule({
  imports: [DirectivesModule, ...],
  declarations: [SomeView, ...],
  ...
})

Then I need to move SomeView to other module. This leaves me with:

@NgModule({
  imports: [DirectivesModule, ...],
  declarations: [...],
  ...
})

No other declared element use DirectivesModule. It is not needed anymore, so I would like to remove it. But I cannot tell that it is safe to delete without studying every other declaration.

So my question is: is there a way to find if given module import or provider is safe to delete? In a project I am working on, where one module can load twenty others, keeping it clean is really hard task.

like image 912
Łukasz Szcześniak Avatar asked Feb 20 '18 10:02

Łukasz Szcześniak


Video Answer


1 Answers

So my question is: is there a way to find if given module import or provider is safe to delete? In a project I am working on, where one module can load twenty others, keeping it clean is really hard task.

Unit testing is the only known way of verifying that a module can be removed safely. The explanation of why Angular can't self detect the usage of a module is rather complex, but keep in mind that a module can forward an inner module. So one module may have exports of many child modules. This is made more difficult by the fact that providers can be resolved at run-time with injector.get(myService). Which makes it extremely difficult to find usages, because there is no way of knowing by looking at the source code which injector is being used.

If you had minimal unit tests that just verified a component can be created, then you'd know if removal of the module breaks unit tests.

like image 97
Reactgular Avatar answered Oct 06 '22 07:10

Reactgular