Ionic 4: How to exclude page/module from build
I was trying to exclude a page from Ionic build but unfortunately I did not find anything helpful.
It is possible to exclude file/folder from build by adding it in exclude array in tsconfig.app.json file. This works until it has no import anywhere in the project.
TypeScript will include the file/folder in the build even if you add them in exclude array if it is imported anywhere else in the project.
If a page has it’s corresponding route in app-routing.module.ts and even the routes are lazy loaded still you will find your pages in the build.
What if you want only specific pages in a particular build (kind of feature toggle for pages) to reduce bundle/app size?
As we know, app-routing.module.ts has import references to the pages, so technically we cannot exclude a page without manually removing it from routes array. But we can achieve it by simple technique using ternary conditional operator!
Suppose we have four pages: HOME, LOGIN, SETTINGS & TUTORIAL.
And we want exclude SETTINGS & TUTORIAL page from the build.
To do so we need to make changes in three files: environment.ts, app-routing.module.ts & environment.prod.ts. Following are the steps:
#1. Add pages object in environment.ts & environment.prod.ts like this👇

#2. Create a component/page which will display message like PAGE NOT FOUND or something similar. This component/page will be shown if a page is excluded. Although, you could use existing component/page.
#3. Import environment in app-routing.module.ts

Inside routes array, the route you want to exclude, add ternary conditional operator like 👇

I have pointed route to the login page but you could point to the custom component or any other page. Depending upon the value of pages object in environment file, the ternary conditional operator will make the magic and exclude page from the build. Also don’t forget to keep consistency in environment.ts & environment.prod.ts otherwise you’ll get the unnecessary pages in the prod build.
How to verify page is excluded from the build?
I have verified it by generating a debug android app & decompiling it. Or you could simply observe the app/bundle size.
