5

I am trying to use an environment variable in my angular.json file. I suspect this isn't possible, but thought I would see if anyone knew how. I am using it to point to a signalr file generated by the server (issue being when in dev vs. staging it's a different baseUrl). So... angular.json

"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "ab-inbev-web2": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist",
            "index": "src/index.html",
            "main": "src/main.ts",
            "tsConfig": "src/tsconfig.json",
            "assets": [
              "src/assets",
              "src/favicon.ico"
            ],

            "scripts": [
              "node_modules/bootstrap/dist/js/bootstrap.min.js",
              "http://site-dev.com/signalr/hubs"
and when on staging
              "http://site-stage.com/signalr/hubs"
            ]

environment.ts

export const environment = {
  baseUrlWithSlash: 'http://site-prod.com/',
  logoffWarningTimeoutSeconds: 600,
  production: true,
  searchDelimiters: /[,;\n]+/
};

environment-local.ts
  export const environment = {
      baseUrlWithSlash: 'http://site-dev.com/',
      logoffWarningTimeoutSeconds: 600,
      production: true,
      searchDelimiters: /[,;\n]+/
    };

ect...

I am trying to avoid hard coding it and trying to have it work while running local. I thought maybe there is a way to sniff the location in the angular.json file and do an if statement.

RooksStrife
  • 1,647
  • 3
  • 22
  • 54
  • @user184994 is a way to sniff the location in the angular.json file and do an if statement to append to the script location? – RooksStrife Nov 06 '18 at 18:46
  • When you do a build, you pass the environment, such as `ng build --prod` for example. You can use environments I guess? https://github.com/angular/angular-cli/wiki/stories-application-environments – user184994 Nov 06 '18 at 18:48
  • Since when JS has access to env variables??:> – Antoniossss Nov 06 '18 at 18:50
  • @Antoniossss It doesn't, but Angular has an `environments.ts` file, which may provide the functionality that OP is after. – user184994 Nov 06 '18 at 18:51
  • idk, he asked about ENV . Obviously environments is what he is looking for. – Antoniossss Nov 06 '18 at 18:51
  • Right I have environments files setup as in the wiki all that works. I am trying to augment the actual script location in the angular.json - based on the environment or location that angular.json exist (local, site-dev, etc.) – RooksStrife Nov 06 '18 at 18:53
  • are you looking for something like `ng serve --configuration=`? – Mike Tung Nov 06 '18 at 18:54
  • I am thinking more of this https://stackoverflow.com/questions/34489916/how-to-load-external-scripts-dynamically-in-angular... just found. – RooksStrife Nov 06 '18 at 18:55
  • Found this too -https://stackoverflow.com/questions/41741457/angular2-load-script-conditionally-using-angular-cli Closing question. – RooksStrife Nov 06 '18 at 18:58
  • https://stackoverflow.com/questions/41741457/angular2-load-script-conditionally-using-angular-cli – RooksStrife Nov 06 '18 at 18:58
  • @RooksStrife might be worth reading this https://stackoverflow.com/a/51490031/1990451 ; not answering this question but could give some hints still – smnbbrv Nov 06 '18 at 19:07
  • https://stackoverflow.com/questions/41741457/angular2-load-script-conditionally-using-angular-cli I not different then using environment file..... – Antoniossss Nov 06 '18 at 19:52
  • Use dynamic script loading instead of add it into your CLI config. Example of service to allow dynamic script loading : https://github.com/gilsdav/angular-lazyload-script-service – Gilsdav Nov 06 '18 at 23:53

0 Answers0