1
votes

So my Angular application on prod is running in devmode. The two environments i have are:

environment.ts:

export const environment = new Promise((resolve, reject) => {
  const xhr = new XMLHttpRequest();
  xhr.open('GET', './assets/config/config.json');
  xhr.onload = function () {
    if (xhr.status === 200) {
      const config = JSON.parse(xhr.responseText);
      config['production'] = false;
      resolve(config);
    } else {
      reject('Cannot load configuration...');
    }
  };
  xhr.send();
});

environment.prod.ts:

export const environment = new Promise((resolve, reject) => {
  const xhr = new XMLHttpRequest();
  xhr.open('GET', './assets/config/config.json');
  xhr.onload = function () {
    if (xhr.status === 200) {
      const config = JSON.parse(xhr.responseText);
      config['production'] = true;
      resolve(config);
    } else {
      reject('Cannot load configuration...');
    }
  };
  xhr.send();
});

I saw that enableProdMode() in main.ts is called depending on whether im in prod or not.

import {environment as environmentPromise} from './environments/environment';

environmentPromise.then(environment => {

  if (environment['production']) {
    enableProdMode();
  }

  localStorage.setItem('authServer', environment['authServer']);
  localStorage.setItem('resourceServer', environment['resourceServer']);
  platformBrowserDynamic().bootstrapModule(AppModule);
});

But first of all, environment is imported from environment.ts here. So environment['production'] is always false. The exported environemnt from environment.prod.ts is unused. How do i do check whether im in prod or not correctly then?

1
it is done automatically by angular in prod mode - Mridul

1 Answers

0
votes

Why don't you change the enviroment like this:

export const environment = {
  production: true,
  configPromise: new Promise((resolve, reject) => {
      const xhr = new XMLHttpRequest();
      xhr.open('GET', './assets/config/config.json');
      xhr.onload = function () {
        if (xhr.status === 200) {
          const config = JSON.parse(xhr.responseText);
          config['production'] = true;
          resolve(config);
        } else {
          reject('Cannot load configuration...');
        }
     };
    xhr.send();
  });
}

And call it like this

import {environment as environmentPromise} from './environments/environment';

environmentPromise.configPromise.then(environment => {

  if (environment['production']) {
    enableProdMode();
  }

  localStorage.setItem('authServer', environment['authServer']);
  localStorage.setItem('resourceServer', environment['resourceServer']);
  platformBrowserDynamic().bootstrapModule(AppModule);
});