I am working on an application that was set up using jhipster, Spring Boot and Angular
I need to set up different public keys for something depending on whether the app is running on dev or prod.
I've been sitting in my angular.json for quite some time now, and googled a bunch of solutions, trying all the angular.json based ones.
Here's what I tried in angular.json:
All three of my index files are in the webapp directory.
angular.json(relevant bit):
"root": "",
"sourceRoot": "src/main/webapp",
"projectType": "application",
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"index": "src/main/webapp/index.html",
"configurations": {
"production": {
"index": {
"input": "src/main/webapp/index-prod.html",
"output": "src/main/webapp/index.html"
}
}
},
"scripts": [
],
"styles": [
"./src/main/webapp/teststyle.css"
]
}
}
}
The project does not have environment.ts files, but enableProdMode() is being called (through webpack magic) and behaves as expected.
What can I do? Ideally we would like to not have to rebuild between environments, but at this point I'll take it as long as the index.html file changes automatically.
Customize Webpack Configuration in Your Angular Application Now that we've added it to our project, we can use the index transform option to modify the HTML file output, based on the environment.
You don't want to use separate index. html pages, because that is where all the angular scripts are held, and transitioning to a new index. html means loading all your scripts again. Instead, you can use abstract parent states which represent the template with their own ui-view for the child states to populate.
html file. Angular is a framework which allows us to create "Single Page Applications", and here the index. html is the single page which was provided by the server.
The index option supports a longhand form as follows:
"index": {
"input": "src/index.html", // used index file from your project folder
"output": "index.html", // created index file in your outputPath
},
angular.json
example:
"architect": {
"build": {
"options": {
"outputPath": "dist/myproject",
"index": "src/index.html", // default index file (shorthand form)
},
"configurations": {
"production": {
"index": { // production index replacement (longhand form)
// use the file at 'src/index.prod.html' in production builds
"input": "src/index.prod.html",
// create the index file under 'index.html' in your outputPath
"output": "index.html"
}
}
}
}
}
This feature was added in Angular Cli v8.2.0-next.0.
Related GitHub issue: https://github.com/angular/angular-cli/issues/14599
In addition to @fridoo's answer. Net Basal have created a wonderful article about the options that we have in this case.
netbasal | environment-based-index-html-with-angular-cli
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