Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ng build does not include component class code of library modules

Tags:

angular

I have created a library that has modules that have components in them.

My library public_api.ts has this:

export * from './src/app/modules/my-mod-1/my-mod-1.module';

Now this module has components that are exported and declared in the module.ts file

@NgModule({
    imports:      [
        ...
    ],
    declarations: [
        MyOneComponent
    ],
    exports:      [
        MyOneComponent
    ]
})

Now I create the package using ng-packagr and import it in another project using npm install from the .tgz file.

The component works fine with all lifecycle hooks when I run it in development mode.

But when I create a build using ng build, it does not call the lifecycle hooks. Although it does print everything in the html file of the component, but the component class code seems to be missing.

I checked in vendor.js file when running the development mode, and there I can easily find the component class code (actually I searched for a console.log code that I printed in the component class code and it was showing up there)

But same code is missing from main.xxxxxx.js that is built by the build command. And the build command does not create a vendor.js file at all because vendor.js is included in main.js

So the problem is that library classes code is not included in the project build, why would that happen?

There is so much code in the projects that I cannot think of what parts of code should I paste here to help you understand the issue. So please do let me know what else I should copy here.

Thanks

like image 717
M.Imran Mamda Avatar asked May 04 '26 23:05

M.Imran Mamda


1 Answers

I got some help from here: https://github.com/angular/angular-cli/issues/11394

It appears that:

1) you should export all components and modules used by the library in the public_api.ts file.

2) apparently deleting the node_modules folder and then re-installing it by npm install seems to work, it appears that the library cached files were corrupt

Now my app works in production as well!

like image 142
M.Imran Mamda Avatar answered May 07 '26 16:05

M.Imran Mamda