5

After searching the web I cannot find a straight answer so my question is: Is it possible in webpack to import css files dynamically like so :

if(foo){
  import 'mobile.css'
} else {
  import 'desktop.css'
}

I tried this and it does not work; is there a workaround or special webpack module or else ?

2

1 Answer 1

6

This works for me:

const foo = false;
if (foo) {
  require("./mobile.css");
} else {
  require("./desktop.css");
}

Also you can use the dynamic import:

const foo = false;
if (foo) {
  import("./mobile.css");
} else {
  import("./desktop.css");
}

See: https://stackblitz.com/edit/js-hrvzy9?file=index.js

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.