0

I have this code in envrionment.ts

export const environment = {
    production: false,
    host_name: 'http://local.website.com',
    api_name: 'http://local.website.com/api',
    ajax_name: 'http://local.website.com/ajax',
};

Once the app is built, we need to change the environment settings without undergoing another build. Essentially because the web-app is coupled to another website, and the back-end people want to change these settings without running a build. Using the .env root file has been ruled-out as insecure leaving a .json file in the assets folder as the only option. There is another ts file on the app that can read the json with no problem when served but envrionment.ts can not seem to, supposedly because its static. Any help would be appreciated.

  • are you asking to DYNAMICALLY change a STATIC file ? ;/ – noririco May 18 '21 at 09:35
  • You can't use `environment.ts` file after the build. That file is compiling and bundling into the production file so you need to do the rebuild after changing any value in it. You can refer https://www.jvandemo.com/how-to-use-environment-variables-to-configure-your-angular-application-without-a-rebuild/ – Julian W. May 18 '21 at 09:41
  • So even if the data is loaded from a json file, the build will compile this within the app and not a references external file? I was told if the assets folder is completely ignored by the build and this can be used. – t-owens-elas May 18 '21 at 10:31
  • @noririco yes, essentially, is this possible? – t-owens-elas May 18 '21 at 10:32
  • @t-owens-elas you can try https://stackoverflow.com/questions/43651991/how-to-update-json-file-without-rebuilding-in-angular-cli – noririco May 18 '21 at 11:32
  • Thank you, loading in a JSON file requires a script that must run outside of the environment.ts, I created a service for it but I cant load in this script to the environment. – t-owens-elas May 18 '21 at 14:41

1 Answers1

1

At the company I work for now, they adjust the settings via ftp. For this reason, it needs to read the json file and rewrite it every time the client is refreshed.

Follow the instructions in the url I shared. it worked for us.

https://christianlydemann.com/implementing-dynamic-environments-in-angular-for-avoiding-one-build-per-environment/

Edit : maybe you can use specifically created regex records. You can try changing it in main.bundle.js using the fs library belonging to nodejs. like this

export const environment = {
    production: '<<<<PRODUCTION>>>>',
    host_name: '<<<<HOST_NAME>>>>',
    api_name: '<<<<API_NAME>>>>',
    ajax_name: '<<<<AJAX_NAME>>>>',
};
Onur Özkır
  • 559
  • 3
  • 12
  • 1
    Thank you, I did try that, its a legacy build and complicated solutions seem to cause bigger problem. All I really need to do is import the JSON info read by the app using ngInit() (which is does) only I need to import this to envrionment.ts. I think that should do it. – t-owens-elas May 18 '21 at 10:39
  • Thank you for the edit. Looks like an interesting solution. I am not sure how to alter things like that in Node directly. I will look into it. Any guidance you can give me is also appreciated. – t-owens-elas May 18 '21 at 14:42