npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ngx-decorators

v1.0.2

Published

Manage your Angular components like a boss.

Downloads

14

Readme

Decorators for Angular (ngx-decorators)

Manage your Angular components like a boss.

NOTE! Decorators for Angular is not compatible with Angular AOT compilation.

Installation

Install using NPM CLI

npm install --save ngx-decorators

or using Yarn CLI

yarn add ngx-decorators

Use cases

Custom component decorator

Do you find yourself copying and pasting the same decorator configuration options for your components?

Create a custom component decorator and use it as a drop-in replacement for Component from @angular/core. You only need to change the import statement.

Customize defaults

The Angular component decorator defaults to emulated view encapsulation and the default change detection strategy.

Create a custom component decorator with no view encapsulation, native view encapsulation (shadow DOM) and the OnPush change detection strategy. Set defaults or enforce your choices.

Build your own component options combinators

We have included helpers for managing change detection and view encapsulation as well as adding a common class to the host component.

Create your own component options combinators to manage white space preservation in component templates, add host element attributes, host styles or any other current or future component option.

Usage

Angular Material uses TSLint rules to enforce common component decorator options. Let us create a custom component decorator to enforce the common options.

import {
  ChangeDetectionStrategy,
  Component as ComponentOptions,
  ViewEncapsulation,
} from '@angular/core';
import {
  ComponentOptionsCombinator,
  createComponentDecorator,
  CustomComponentDecorator,
  forceChangeDetection,
  forceViewEncapsulation,
} from 'ngx-decorators';

const detectChangesOnPush: ComponentOptionsCombinator =
  forceChangeDetection(ChangeDetectionStrategy.OnPush);
const noViewEncapsulation: ComponentOptionsCombinator =
  forceViewEncapsulation(ViewEncapsulation.None);
const alwaysMinifyTemplate: ComponentOptionsCombinator =
  (options: ComponentOptions): ComponentOptions => ({
    ...options,
    preserveWhitespaces: false,
  });

export const Component: CustomComponentDecorator = createComponentDecorator([
  detectChangesOnPush,
  noViewEncapsulation,
  alwaysMinifyTemplate,
]);

You can omit the types if you find it less distracting:

// src/app/decorators/component.decorator.ts
import {
  ChangeDetectionStrategy,
  Component as ComponentOptions,
  ViewEncapsulation,
} from '@angular/core';
import {
  createComponentDecorator,
  forceChangeDetection,
  forceViewEncapsulation,
} from 'ngx-decorators';

const detectChangesOnPush =
  forceChangeDetection(ChangeDetectionStrategy.OnPush);
const noViewEncapsulation =
  forceViewEncapsulation(ViewEncapsulation.None);
const alwaysMinifyTemplate = options => ({
    ...options,
    preserveWhitespaces: false,
  });

export const Component = createComponentDecorator([
  detectChangesOnPush,
  noViewEncapsulation,
  alwaysMinifyTemplate,
]);

Now we are ready to use it in our project.

Before we had

(sample from Angular Material version 5)

import { Component } from '@angular/core';

@Component({
  moduleId: module.id,
  selector: 'mat-card',
  exportAs: 'matCard',
  templateUrl: 'card.html',
  styleUrls: ['card.css'],
  encapsulation: ViewEncapsulation.None,
  preserveWhitespaces: false,
  changeDetection: ChangeDetectionStrategy.OnPush,
  host: {'class': 'mat-card'}
})
export class MatCard {}

Now we are left with

import { Component } from '../decorators/component.decorator';

@Component({
  moduleId: module.id,
  selector: 'mat-card',
  exportAs: 'matCard',
  templateUrl: 'card.html',
  styleUrls: ['card.css'],
  host: {'class': 'mat-card'}
})
export class MatCard {}

Even if we by accident add the default change detection strategy or emulated view encapsulation, our custom component decorator takes care of it for us. This is of course entirely customizable.

Look at the example app in this repository to see our various helpers in-use.

Supported Angular versions

Decorators for Angular has been tested with Angular versions 2 through 6. It is unlikely that it will stop working until Angular's component decorator or the public API of @angular/core gets breaking changes.

Caveats

Angular AOT compilation

Angular AOT compilation does not support arrow functions or function expressions. While the arrow functions can easily be converted to function declarations, this library does not contribute anything without its combinator creators that are using function expressions.

Node.js

Do not use a custom component decorator for your root component, i.e. AppComponent unless you are using Node.js version 9 or newer.

Attributions

Thank you Mahmoud Abduljawad for your support, ideation and testing.