简体   繁体   中英

Webpack; how to import css dynamically

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM