1

I am using Angular 5.2 and I need to bundle like it does with ng build --prod but with the different environment

I tried:

ng build --env=qa --aot --vendorChunk --common-chunk --output-hashing=bundles

but it does not give me the same bundling as i see with --prod

it generates both a .js and .js.map file

main.66dc6fba707fe2f3314c.bundle.js
main.66dc6fba707fe2f3314c.bundle.js.map

What options should I use to get me the same result at --prod but with a different environment?

2 Answers 2

4

In angular 6 you can create multiple environments in angular.json

Find configuration and inside that you can create multiple environment with various settings which you can find from here https://github.com/angular/angular-cli/wiki/angular-cli

example

"configurations": {
  "production": {
    "fileReplacements": [
      {
        "replace": "src/environments/environment.ts",
        "with": "src/environments/environment.prod.ts"
      }
    ],
    "optimization": true,
    "outputHashing": "all",
    "sourceMap": false,
    "extractCss": true,
    "namedChunks": false,
    "aot": true,
    "extractLicenses": true,
    "vendorChunk": false,
    "buildOptimizer": true
  },
  "staging": {
    "fileReplacements": [
      {
        "replace": "src/environments/environment.ts",
        "with": "src/environments/environment.staging.ts"
      }
    ],
    "optimization": true,
    "outputHashing": "all",
    "sourceMap": false,
    "extractCss": true,
    "namedChunks": false,
    "aot": true,
    "extractLicenses": true,
    "vendorChunk": false,
    "buildOptimizer": true
  }
}

As you can see i have created an another environment name staging

The Dummy angular.json file is https://api.myjson.com/bins/12k70w

To run the application with specific environment just use

ng build --configuration=staging

I have also created a file in environment called environment.staging.ts

export const environment = {
    production: true,
    APIEndpoint: "http://0.0.0.0:8080/api"
};
Sign up to request clarification or add additional context in comments.

3 Comments

as I am using 5.2 there is no angular.json
I tried your suggestion but i think the same you have given me is not the complete json it is throwing error. can you share me a template I can work off?
@kumar have you updated your angular cli to version 6. Do you have angular.json file? I will also update the answer and share my angular.json file
2

When you are creating environment file, we need to set production: true which will enable Production Build with AOT by default in main.ts.

environment.stage.ts

export const environment = {
  production: true
};

main.ts

if (environment.production) {
  enableProdMode();
}


Cmd: ng build --prod --env=stage

1 Comment

I tried this but it still bundles as 2 different files bundle.js and bundle.js.map

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.