To exclude some files from ng build you can declare them in the “assets” attributes of the “architect” section of the project’s angular.json file. You can use ignore field to exclude specific files.
We will use the options properties section inside build tag of angular.json. The angular.json file is the configuration file for the Angular CLI. It contains various architect options that dictate how the CLI builds, serves, and tests your Angular application. The build option specifies how the application should be built. The options property of build allows you to set various build options, such as assets to include or exclude in the build output. The assets array lists the files or directories to be included as assets in the build.
You need to include the following in the “architect” section, for instance, to exclude all .json and .spec files located in the “src/assets” directory:
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/...../browser",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.app.json",
"inlineStyleLanguage": "scss",
"assets": [
"src/favicon.ico",
"src/robots.txt",
{
"glob": "**/*",
"input": "src/assets/",
"ignore": ["**/*.json", "**/*.spec"],
"output": "/assets/"
}
......
]
Each item in the assets array is an object that specifies which files from the input directory should be included as assets. The input property specifies the directory that contains the assets to be included in the build. The glob property is a file pattern that defines which files from the input directory should be included as assets. The ignore property is an array that lists file patterns to exclude from the build.
In the above example, the following line is responsible for excluding .json and .spec files from the assets folder on ng build:
"ignore": ["**/*.json", "**/*.spec"],
The code tells the Angular CLI to ignore certain files when building the project. The ignore property is an array that lists the file patterns to be ignored. It includes two patterns:
- “**/*.json”: Ignore all JSON files in the subdirectories.
- “**/*.spec”: Ignore all “.spec” files in all subdirectories.
The Angular CLI will not include JSON files and files with a “.spec” extension in the final output of your project.
To exclude some files from ng build and depending upon the files you want to exclude from the build, you can add multiple or a single value in that field.