1

I'm trying to find a way to change how my scss is compiled based on a "profile" variable environment. My app will be installed on different locations, witch with different theme, but all the css is the same. I just need to change what color palette will be used, during compilation. My app already have on the environments.ts a profile variable, that will be used for brand image.

I was thinking in something like:

export const environment = {
  production: true,
  profile: 'appX'
  apiUrl: 'http://mydoainX.com/api/',
};

and, on the scss:

$profile: environment.profile;

It's possible to do something like this?

Any thoughts?

Brosig
  • 724
  • 1
  • 10
  • 19
  • How about overwriting that css value with locally stored copy in each environment from another css file: import url("department_store.css"); Not sure about sass but I think they use include for that. – boateng Oct 20 '17 at 16:07

1 Answers1

7

Eventually a found this question, witch lead me to the solution.

It says to create multiple apps with different .scss file in .angular-cli.json.

Step 1: Create several folders with .scss file for each environment, all the .scss files has the same filename:

|- src
  ...
  |- environments
    |- app1
      |- scss
        |- globalVars.scss
      |- environment.dev.ts
      |- environment.prod.ts
    |- app2
      |- scss
        |- globalVars.scss
      |- environment.dev.ts
      |- environment.prod.ts
  ...

in src/environments/app1/scss/globalVars.scss: $profile: 'app1'.

in src/environments/app2/scss/globalVars.scss: $profile: 'app2'.

Step 2:

Add multiple apps in .angular-cli.json ( Angular-cli Wiki Here ), and add stylePreprocessorOptions entry in each app object for each environment ( Angular-cli Wiki Here ).

"apps": [
  {
    ...
    "name": "app1",
    "environments": {
      "dev": "environments/app1/environment.dev.ts",
      "prod": "environments/app1/environment.prod.ts"
    },
    "stylePreprocessorOptions": {
      "includePaths": [
        "environments/app1/scss"
      ]
    }
    ...
  },
  {
    ...
    "name": "app2",
    "environments": {
      "dev": "environments/app2/environment.dev.ts",
      "prod": "environments/app2/environment.prod.ts"
    },
    "stylePreprocessorOptions": {
      "includePaths": [
        "environments/app2/scss"
      ]
    }
    ...
  },
  ...
],

Step 3:

Import the globalVars.scss where the env-specific variables needed. Do not use the relative path!

@import "globalVars";

When using ng build --app=app1, the $profile will be 'app1'and so on.

Step 4: On my theme.scss i used the variable like this:

.my-style {
  @if $profile == 'app1' {@include angular-material-theme($theme-app1);}
  @if $profile == 'app2' {@include angular-material-theme($theme-app2);}

  &.contrast {
    @include angular-material-theme($theme-contrast);
  }
}
Kelvin Santiago
  • 305
  • 1
  • 10
Brosig
  • 724
  • 1
  • 10
  • 19
  • 3
    This doesn't and won't get the environment variable saved in `environment.prod.ts`, and also not the right answer to the question. – k.vincent Jun 14 '18 at 09:27