Marmicode
Blog Post
Younes Jaaidi

Your Angular Module is a SCAM!

by Younes Jaaidi • 
Mar 12, 2019 • 4 minutes
Your Angular Module is a SCAM!

Every time I am about to add a component to an Angular application, I feel like there is a bunch of people arguing in my head.

  • Does it deserve a new NgModule?
  • Will we re-use it soon?
  • Who cares, we will refactor that when needed anyway.
  • Yeah! But even with a great IDE, how straightforward is the refactoring?

TL;DR

There’s a new schematic to generate Single Component Angular Modules.

yarn add @wishtack/schematics
yarn ng g @wishtack/schematics/scam hello-world

Where It All Started

A while ago, I caught this Pull Request https://github.com/angular/angular/pull/27481 from Minko Gechev which shows that the Angular team might make NgModules optional in the future.

It inspired me to experiment a new tagged template based syntax.

ng-markup

The current problem with this approach is that in addition to involving a new syntax, we will have to wait for optional modules support.

By the way, here’s Igor Minar’s reaction about my previous post and optional modules:

New Template Syntax - Igor Minar Feedback

In the meantime, we have to consider another way to solve this situation.

NgModule Refactoring Hell

One of the main challenges with NgModule is refactoring.

Given the following cute and tiny little module:

@NgModule({ declarations: [ SandwichFormComponent, SandwichPreviewComponent ], imports: [ MatButtonModule, MatCardModule, ReactiveFormsModule ] }) export class CuteLittleSandwichModule {}

It is already a challenge to move SandwichFormComponent to another module.

For instance, it is hard to tell if MatButtonModule & MatCardModule should be kept, moved, or duplicated to the new module.

SCAM!!!

What if we created one module for each component?

That is what the SCAM backronym means. It stands for Single Component Angular Module.

SCAM stands for Single Component Angular Module

As far as I know, this term was coined by Lars Gyrup Brink Nielsen in his article about Tree-Shakable Component and Optional NgModules.

I loved the concept so much that I created a simple schematic for that.

You can use it right away like this:

yarn add @wishtack/schematics
yarn ng g @wishtack/schematics:scam sandwich/sandwich-preview

This will generate the following file structure:

app/
  sandwich/
    sandwich-preview/
      sandwich-preview.component.ts|html|...

sandwich directory is just for regrouping components, there is no sandwich.module.ts ! SandwichPreviewModule is inside sandwich-preview.component.ts.

🧹 Say bye to .module.ts

In a response to this article, Alex Rickabaugh from the Angular Core Team suggested to just move the NgModule to the .component.ts file and simply get rid of the .module.ts.

It totally makes sense for two reasons:

  • The component’s directory will be less cluttered.
  • It should naturally discourage us from adding anything else to the module.
  • Using IVy, when importing the component we will also trigger the build of the module without having to explicitly import it.

What about directives & pipes

Well, same applies for directives & pipes. SDAM & SPAM?

WebStorm Auto-Import

For those who are worried about having to import lots of modules, you should definitely try the latest version of WebStorm as it will automatically auto-import modules while you are writing your template. Ain’t that crazy... when you think back to that time when selector auto-completion was something! 😱

WebStorm Angular Module Auto-Import

For the moment, I am not aware of any similar feature on VSCode but I am sure it will come soon 😊


The source code & documentation are here: https://github.com/wishtack/wishtack-steroids/tree/master/packages/schematics.