Is it possible to have different assets for development and production with Angular CLI (7.0.5)?
For production i want the assets:
"assets": [
"projects/example/src/favicon.ico"
]
For development i want the assets:
"assets": [
"projects/example/src/favicon.ico",
"projects/example/src/assets/development.css"
]
Thank you in advance.
The Angular application has a default assets folder. By default, whatever you put in the assets folder you can access it directly from the browser and queried through an HTTP request. You can create your own folder like assets by adding that folder to the assets section in the angular. json file.
To Fix Error “This command is not available when running the Angular CLI outside a workspace Error”, Do Right-Click on yours project name in VS Code and Click “Open in Integrated Terminal” Option. It would open the project to your terminal and error would be fixed.
To solve the error "Could not find module '@angular-devkit/build-angular'", make sure to install the package by opening your terminal in your project's root directory and running the following command: npm i -D @angular-devkit/build-angular and restart your IDE and development server. Copied!
I have found a way to make it work, it ain't pretty but it works.
In your angular.json file you can copy your current project, in my case my project is named "example". I rename the copied project to "example-dev". See the code below:
{
...
"projects": {
"example": {
"root": "projects/example/",
"sourceRoot": "projects/example/src",
"projectType": "application",
"prefix": "app",
"schematics": {},
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/example",
"index": "projects/example/src/index.html",
"main": "projects/example/src/main.ts",
"polyfills": "projects/example/src/polyfills.ts",
"tsConfig": "projects/example/tsconfig.app.json",
"assets": [
"projects/example/src/favicon.ico"
],
"styles": [
"projects/example/src/styles.scss"
],
"scripts": []
}
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "example:build"
}
}
}
},
"example-dev": {
"root": "projects/example/",
"sourceRoot": "projects/example/src",
"projectType": "application",
"prefix": "app",
"schematics": {},
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/example",
"index": "projects/example/src/index.html",
"main": "projects/example/src/main.ts",
"polyfills": "projects/example/src/polyfills.ts",
"tsConfig": "projects/example/tsconfig.app.json",
"assets": [
"projects/example/src/favicon.ico",
"projects/example/src/assets/development.css"
],
"styles": [
"projects/example/src/styles.scss"
],
"scripts": []
}
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "example-dev:build"
}
}
}
}
}
In the project "example" (production) i removed the line "projects/example/src/assets/development.css" in the assets. In the project "example-dev" i changed the browserTarget in serve to "example-dev:build" instead of "example:build"
If i want to serve the development project:
ng serve example-dev -o
If i want to serve the production project:
ng serve example -o
If someone has a cleaner solution then I would love to hear that :)
EDITED
Thanks to Karthick Venkat i have found a other way to make it work, which is alot cleaner.
In your angular.json file you need to add a new configuration for development. See the code below:
{
"projects": {
"example": {
"root": "projects/example/",
"sourceRoot": "projects/example/src",
"projectType": "application",
"prefix": "app",
"schematics": {},
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/example",
"index": "projects/example/src/index.html",
"main": "projects/example/src/main.ts",
"polyfills": "projects/example/src/polyfills.ts",
"tsConfig": "projects/example/tsconfig.app.json",
"styles": [
"projects/example/src/styles.scss"
],
"scripts": []
},
"configurations": {
"production": {
"fileReplacements": [{
"replace": "projects/example/src/environments/environment.ts",
"with": "projects/example/src/environments/environment.prod.ts"
}],
"optimization": true,
"outputHashing": "none",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"assets": [
"projects/example/src/favicon.ico"
]
},
"development": {
"assets": [
"projects/example/src/favicon.ico",
"projects/example/src/assets"
]
}
}
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "example:build"
},
"configurations": {
"production": {
"browserTarget": "example:build:production"
},
"development": {
"browserTarget": "example:build:development"
}
}
}
}
}
}
}
If i want to serve the development project:
ng serve example --configuration=development -o
If i want to serve the production project:
ng serve example --configuration=production -o
or
ng serve example -o
We have an option called fileReplacements under production configurations in angular.json. There you can replace the files.
Note : I have not tested.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With