1

in our project we are building the same angular-application for different areas (lets say intranet and internet). The only thing that changes in those areas is *.scss-files. To be able to add scss-files conditionally, according to build-parameters I started overriding @Component() following this:

https://stackoverflow.com/a/39755547/1053622

which looks like this:

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

export interface ITargetAwareComponentProperties {
  selector: string;
  template?: string;
  templateUrl?: string;
  styles?: string[];
  styleUrls?: string[];
  directives?: any;
  providers?: any;
  encapsulation?: number;
  internetStyles?: string[];
  intranetStyles?: string[];
}

export function TargetAwareComponent(properties: ITargetAwareComponentProperties) {
  try {
    if (!properties.styles) {
      properties.styles = [];
    }

    if (environment.targetType === 'intranet' && properties.intranetStyles) {
      for (const style of properties.intranetStyles) {
        properties.styles.push(style);
      }
    }
    else if (properties.internetStyles) {
      for (const style of properties.internetStyles) {
        properties.styles.push(style);
      }
    }

  } catch (err) {
    console.warn(err);
  }

  return Component(properties);
}

This is working fine until I want to compile it ahead of time with ng build --aot where I am getting the following error message:

ERROR in : Unexpected value 'TextComponent' 
in /Users/.../components/text/text.component.ts' 
declared by the module 'AppCommonModule in /Users/.../common.module.ts'. 
Please add a @Pipe/@Directive/@Component annotation.

I am guessing that angular doesn't understand that @TargetAwareComponent() is a @Component() and therefore does not compile/finds it properly.

I guess it is not really supported to override the @Component() itself, right?

Is there another way to do it?

Otherwise I will need to copy/move/paste/delete *.scss-files before I run ng build --aot

Help is appreciated.

Martin-Mueller
  • 141
  • 1
  • 10

0 Answers0