Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 Inheritance super() injection breaks with Webpack

I have a base class:

import {DomSanitizer} from '@angular/platform-browser';

export class Base {
    constructor(protected sanitizer: DomSanitizer){
    }

    //.....lots more stuff
}

This class is extended by the following derived class:

import {DomSanitizer} from '@angular/platform-browser';

export class ChildClass extends Base{
    constructor(protected sanitizer: DomSanitizer) {
        super(sanitizer);
    }
}

My module is importing BrowserModule.

I'm using Webpack to compile and am getting the following error at runtime:

Uncaught ReferenceError: platform_browser_1 is not defined -- app.bundle.js:58139

app.bundle.js is output by webpack, and I can see in the compiled source that indeed platform_browser_1 is not available within that chunk.

I'm not splitting my app up in any purposeful manner. Is there something I don't know about how inheritance could cause Webpack to erroneously not import a required class or something?

This is definitely a Webpack issue, because if I remove the inheritance-based usave of the DomSanitizer and just put it into the child component directly, it works fine.

To clarify, platform-browser is the module from which the DomSanitizer is imported.

Does anyone have any idea what's going on? Any help is appreciated.

like image 487
dudewad Avatar asked Oct 30 '25 15:10

dudewad


1 Answers

Okay so basically, I tried a number of things to see how to get Webpack to output the proper code. I noticed that Webpack would, with other modules, output a variable in the transpiled block as: var platform_browser_1 = ...

However, in the chunk for my base class, it was not even outputting it at all. So, I thought there must be a reason it was ignoring it. Since these blocks get annotated, I figured that perhaps adding an @Component annotation to the base class would work, and it did. So, I went from:

export class Base {....

To:

@Component({}) //empty component decorator
export class Base {
    //....
}

Problem solved, for whatever reason. I have no explanation as to why, unfortunately, but if anyone feels like investigating, it's probably because @Component provides metadata per my research on the NG docs site.

Hope this helps somebody!!

like image 163
dudewad Avatar answered Nov 02 '25 08:11

dudewad