Angular 4使用--prod构建时如何排除文件夹、assets目录及JSON数据
--prod) Hey there, let's walk through exactly how to exclude specific content when running ng build --prod in your Angular 4 project. Angular 4 relies on the angular-cli.json file (not the newer angular.json from later Angular versions) for build configuration tweaks, so we'll focus on editing that file here.
1. Excluding Custom Standalone Folders
If you have a standalone folder (like a temp directory or unused utility folder) that you don't want included in your production build, add its path to the exclude array in your angular-cli.json:
"apps": [ { // ... other existing config settings "exclude": [ "temp-files/**/*", "unused-utils/**/*" ] } ]
- The
**/*wildcard ensures we exclude all files and subfolders inside the target directory, not just the folder itself.
2. Excluding Specific Content from the Assets Folder
By default, Angular copies everything in your assets folder to the dist directory during a build. To exclude specific files or subfolders within assets, you'll need to replace the simple string path in the assets array with a detailed object configuration:
Default Assets Setup (Before Exclusions):
"assets": [ "assets", "favicon.ico" ]
Modified Assets Setup (With Exclusions):
"assets": [ "favicon.ico", { "glob": "**/*", "input": "./assets/", "output": "./assets/", "ignore": [ "old-images/**/*", "unused-config.json" ] } ]
glob: Matches all files and subfolders in the specifiedinputdirectoryinput: The source assets directory (relative to your project root)output: Where the included assets will be placed in the finaldistfolderignore: The list of paths (relative toinput) you want to exclude from the build
3. Excluding JSON Files
How you exclude JSON files depends on their location:
- JSON files outside the assets folder: Add their direct path (or wildcard paths) to the
excludearray inangular-cli.json:"exclude": [ "src/legacy-data/*.json", "src/temp/*.json" ] - JSON files inside the assets folder: Use the
ignorearray in the assets configuration we covered in section 2.
Quick Pro Tip
After modifying your angular-cli.json, clear the build cache before running your production build to ensure the exclusions take effect properly:
ng clean ng build --prod
Hope this helps you tailor your production build exactly how you need it!
内容的提问来源于stack exchange,提问作者Gaurav

