Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace index.html in Angular 8.3+ depending on environment?

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:

  • The old usage of "index"
  • fileReplacements
  • The new index "input" "output" options
  • With the latter, I've cycled through every way of writing the path to the index files that I could think of (including just the filename).

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.

like image 865
Marvin Falkner Avatar asked Dec 13 '19 14:12

Marvin Falkner


People also ask

Can we change index html file in angular?

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.

Can Angular have multiple index html?

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.

What is the use of index html in angular?

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.


2 Answers

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

like image 165
frido Avatar answered Oct 08 '22 00:10

frido


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

like image 3
StPaulis Avatar answered Oct 08 '22 00:10

StPaulis