Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude a component from build process depending on env. variable?'

I would like to exclude a component from build process. But this should happen based on environment variable saved in environment.prod.ts.

I have been searching but didn't find a working solution, and the most hints are talking about something like adding exclude property in tsconfig.app.json. But even if this suggestion would work, it can't be conditional based on an environment variable, cause this is not possible in this scenario (tsconfig.app.json).

Example of how the entry in environment could be used:

export const environment = { production: true, buildComponentX: false/true };

Any Hint or idea how such process could be implemented?

like image 263
k.vincent Avatar asked Feb 14 '26 03:02

k.vincent


1 Answers

I didn't test this, this is just a lead.

Since your components are bundled at compilation time, why not create an array that contains your components based on a query ?

const components = [AppComponent, PublicComponent, RestrictedComponent, ProdComponent];
const componentsToExport = components.filter(component => component !== ProdComponent);
export componentsToExport;

Now in your modules, you can import this array and spread it :

@NgModule({
  imports: [...componentsToExport]
  // And the rest
})
export class AppModule {}