I am developing a react app, that is now extended by several themes.
These themes are defined in .scss files in my themes folder:
- themes
- - theme-base.scss
- - theme.dark.scss
- - theme.light.scss
they contain basic color schemes with some variables, eg:
[theme-dark.scss]
$color-primary: #100534;
$color-primary-accent: #231454;
All components just use these color variables to theme them.
Now I want webpack to compile and concat my scss code to separate static css files for each theme.
I got this to work by using extractWebpackTextPlugin and some file loaders like this:
module.exports = env => {
const config = require('./webpack.common')(env);
const project = env.project;
const postcss = {
loader: 'postcss-loader',
options: postcssConfig(settings.browsers),
};
config.watch = true;
config.devtool = 'eval';
const themes = ['theme-base'];
themes.forEach(themeKey => {
const themeInstance = new ExtractTextPlugin('css/' + themeKey + '.css');
const themePath = path.resolve(__dirname, '../../react/app/css/_' + themeKey + '.scss');
config.module.rules.push({
test: /\.s?css$/,
exclude: /node_modules/,
use: themeInstance.extract({
fallback: 'style-loader',
use: [
{
loader: 'css-loader',
options: {
localIdentName: '[hash:base64:5]',
modules: true,
importLoaders: 1,
}
},
postcss,
{
loader: 'sass-loader',
options: {
data: `@import "${themePath}";`,
}
}
]
}),
});
config.plugins.push(themeInstance);
});
}
But:
I can not add more than one theme to my array themes! As soon as it contains more than one item, the css and sass loaders throw errors while reading the scss files.
Invalid CSS after "...load the styles": expected 1 selector or at-rule, was "var content = requi"
How would I setup webpack to compile one static css file for each theme?