Skip to content

Instantly share code, notes, and snippets.

@WagnerMoreira
Last active February 3, 2018 00:50
Show Gist options
  • Save WagnerMoreira/fffd28415eb927a57760d4513944b706 to your computer and use it in GitHub Desktop.
Save WagnerMoreira/fffd28415eb927a57760d4513944b706 to your computer and use it in GitHub Desktop.
How to Build and Publish an Angular Module - Notes

Resources:

How to Build and Publish Angular Modules

Quickstart Options:
Importants rules while building a Angular Module:

First, never import BrowserModule. Your module is a feature module, only the final user should import BrowserModule, in the app root module. If you need the common directives (*ngIf, *ngFor…), import CommonModule.

If your module is about creating new components, directives or pipes, do not forget to export them. Declared ones are only accessible inside your module.

Respect the Angular rule: never use browser-specific APIs (like the DOM) directly. If you do so, your module won’t be compatible with Angular Universal(SSR) and other Angular advanced options. If you really need to use browser-specific APIs (localStorage…), you should try/catch errors.

Most importantly, do not mix components/directives/pipes and services in the same module. Why?

A service provided in a module that will be available everywhere in the app, so your module should be imported only once, in the user app root module (like the Http module).

An exported component/directive/pipe will only be available in the module importing yours, so your module should be imported in every user module (root and/or feature modules) that need them (like the CommonModule).

Don't use a name like @my/module those are scoped modules currently only unscoped, primary entry-point libraries are supported.

Good article to go deep about ng modules and its scopes: https://medium.com/@cyrilletuzi/understanding-angular-modules-ngmodule-and-their-scopes-81e4ed6f7407

Finally publishing NPM Packages: https://docs.npmjs.com/getting-started/publishing-npm-packages

GOOD GUIDE http://angularlicio.us/custom-angular-modules

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment