I am building a web app (React) in which I need to support multiple themes. The file structure is as follows. Each component has it’s own directory containing all files that belong to the component.
Example:
ui-Avatar
|– index.jsx
|– theme-a.css
|– theme-b.css
|– ….
When using webpack-dev-server I can quite easily use a conditional require and pull in either theme-a or theme-b.
onComponentWillMount() {
if (this.context.theme === ‘theme-a’) {
require(‘./theme-a.css’);
else (this.context.theme === ‘theme-b’) {
require(‘./theme-b.css’);
}
}
BUT when I use web pack to package for the production server both theme-a.css and theme-b.css get pulled in and packaged into the same resulting css file. With the result that the style rules in the themes overwrite each other.
What I would like is to be able to produce separate files for the two themes with the collected theme snippets from all the components in the application.
Does anyone know how to set up webpack to solve my problem?
You can use Local scoped CSS or “CSS Modules” (experimental) for that.
Just give your component a
theme
property and use the styles from this.Example using CSS Modules:
Useage:
CSS Module mode: https://github.com/webpack/css-loader#module-mode
I realize that this thread is fairly old, but it has the most information about this problem so I’m hoping someone here might be able to help.
I’m working with a whitelabeled website that has multiple customer domains pointing to the same set of servers. The backend returns a different CSS bundle based on the domain with custom branding. I’d like to be able to do the following:
brand_1_theme.css
,brand_2_theme.css
, files with custom colors, etc.ExtractTextPlugin
output a different set of CSS files for each theme, and haveManifestPlugin
output a manifest file with mappings for all themes.Has anyone done anything similar to this? What has been there approach?
import() works:
OR
I’m dealing with the same problem currently, I try to figure it out without any invasive of codes. Then I write a webpack plugin themes-switch to do that. Maybe it can help you.
Now I just use variables to define different colors and dimensions, then webpack would generate themes for me, and the style formats can be less, postcss and sass. Then I can make a realtime-switch in my app.
theme-a.less
theme-b.less
default.less
main.less
In use