question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dynamically import a stylesheet depending on the environment

See original GitHub issue

Please provide us with the following information:

I’m having a question about how to dynamically import ‘scss’ files based on the environment which is serving or building.

Example.

I have a /styles/app/app-1.scss. I have a /styles/app/app-2.scss.

Now i have also 2 environments called app-1 and app-2 I want to be able to say if it’s served/builded environment is app-2 for example that it’s loading the /styles/app/app-2.scss stylesheet.

This so I can develop 1 website and depending on how I build it’s including the stylesheet for overwriting some parts of the application, style the application.

OS?

Windows 7, 8 or 10. Linux (which distribution). Mac OSX (Yosemite? El Capitan?) Mac OSX Sierra

Versions.

Please run ng --version. If there’s nothing outputted, please run in a Terminal: node --version and paste the result here: cli c30 and node v 7.5.0

Repro steps.

Was this an app that wasn’t created using the CLI? What change did you do on your code? etc.

The log given by the failure.

Normally this include a stack trace and some more information.

Mention any other details that might be useful.


Thanks! We’ll be in touch soon.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:19
  • Comments:19 (6 by maintainers)

github_iconTop GitHub Comments

12reactions
RicardoVarandacommented, Feb 14, 2017

@agatsoh @masaanli I have found a solution for this, keep in mind this isn’t exactly supported by the cli but rather a feature of Webpack.

Example of app.component.ts:

import {Component, OnInit, isDevMode} from '@angular/core';

@Component({
  selector: 'app-root',
  template: '<h1>Testing :)</h1>',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  ngOnInit(){
    if(isDevMode()) {
      require("style-loader!./../styles2.css");
    } else {
      require("style-loader!./../styles.css");
    }
  }
}

Where styles2:

h1 {
  color: red;
}

Styles:

h1 {
  color: green;
}

While this may work it was brought to my attention by @filipesilva that there might be some drawbacks to using this solution such as:

  • Your code not being as portable
  • Styles may not be processed correctly
  • Might break if cli changes its loaders
9reactions
mousavidevcommented, Jan 26, 2018

I am looking for such a solution.

theme.ts

interface Theme {
  url: string;
  rel: string;
  media: string;
}

app.component.ts

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  theme: AppComponentThemeProvider
})
export class AppComponent {
  constructor() {
  }
}

app.component.theme.ts

export class AppComponentThemeProvider {
  constructor(@Inject(LOCALE_ID) private localeId) {
  }

  getThemes() {
    let themes = [<Theme>{url:'./app.component.css'}];

    if(this.localeId == 'fa-IR') {
      themes.push(<Theme>{url: './app.component.rtl.css'})
    }

    return themes;
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Sass dynamic import based on environment - Stack Overflow
After some time I found a solution. I ended up generating a file _env.scss as part of webpack. This file contains the $env...
Read more >
Make Your CSS Dynamic with CSS Custom Properties - Toptal
Explore CSS custom properties and find out how they can be used to make better, more dynamic stylesheets.
Read more >
Dynamically Import CSS - Medium
In brief, we inject CSS through JavaScript on the fly. Dynamic Import. Dynamic import() , which allow asynchronous load and execution of script ......
Read more >
Dynamically Load CSS with the Angular CLI | juri.dev
This is great and worked for me Thanks! I have a new problem now, how do i import SCSS variables from each component's...
Read more >
How to Dynamically Import JavaScript with Import Maps
Import maps won't completely replace build tools that perform many other valuable actions like building style sheets and handling images, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found