Keywords: Angular CLI | Build Output Path | angular.json Configuration
Abstract: This article provides an in-depth exploration of configuring build output paths in Angular CLI, detailing methods to modify the dist folder path through the outDir property in angular.json and the --output-path parameter of ng build command. It analyzes configuration differences across Angular versions, demonstrates implementation with practical code examples, and discusses baseHref settings for subdirectory deployments.
Overview of Build Output Path Configuration in Angular CLI
In Angular project development, configuring the output path for build files is a common and essential requirement. By default, Angular CLI uses the dist folder as the build output directory, but in real-world projects, developers often need to adjust the output path based on deployment environments or project structures.
Detailed Configuration Methods
Angular CLI provides two main approaches to configure build output paths: permanent settings through project configuration files, or temporary overrides through command-line parameters.
Via angular.json Configuration File
In Angular 6 and later versions, project configuration is unified in the angular.json file. To modify the output path, locate the outputPath property in the project configuration:
{
"projects": {
"my-app": {
"architect": {
"build": {
"options": {
"outputPath": "dist/my-app"
}
}
}
}
}
}Change the value of outputPath to the desired path. For example, to output to a production folder in the project root, set it as:
"outputPath": "production"Via Command-Line Parameters
In addition to configuration files, you can temporarily specify the output path during build using command-line parameters:
ng build --output-path=production
# Or use the shorthand form
ng build -op productionThis approach is particularly useful for continuous integration environments or scenarios requiring different build outputs for various environments.
Version Compatibility Notes
It's important to note that in earlier versions of Angular CLI (Angular 5 and before), the configuration file was named .angular-cli.json, and the corresponding configuration property was outDir:
{
"apps": [
{
"outDir": "./location/toYour/dist"
}
]
}Starting from Angular 6, the configuration file format was restructured, with outDir being replaced by outputPath, and the configuration structure becoming more organized.
Subdirectory Deployment Configuration
When an Angular application needs to be deployed in a subdirectory of a web server, merely modifying the output path is insufficient; the baseHref property must also be configured:
{
"projects": {
"my-app": {
"architect": {
"build": {
"options": {
"outputPath": "dist/my-app",
"baseHref": "/my-folder/"
}
}
}
}
}
}Or via command-line parameter:
ng build --base-href=/my-folder/Practical Application Scenarios
In actual development, output path configuration typically relates to specific deployment requirements. For instance, when integrating an Angular application into a .NET project, you might need to direct build output directly to the .NET project's wwwroot directory:
"outputPath": "../MyNetApplication/wwwroot"However, with certain build tools (like es-build), additional subdirectories (such as browser) might be automatically created under the specified output path, requiring special attention during deployment.
Best Practices Recommendations
To ensure configuration reliability and maintainability, it is recommended to:
- Use configuration files uniformly for path settings in team development
- Create corresponding npm scripts for different build environments (development, testing, production)
- Use command-line parameters to override default configurations in continuous integration workflows
- Regularly check for configuration changes brought by Angular CLI version updates
By properly configuring build output paths, you can significantly enhance the deployment efficiency and flexibility of Angular projects, providing robust support for application publishing across different environments.