I've been using css modules with sass in a react library that I want to be consumed by other libraries.
I have the following code for a component:
import React, { Component } from 'react';
import styles from './FormInput.scss';
import cs from 'classnames';
import { Input, Label } from '../..';
export const FormInput = ({ invalid, required }) =>
<div
className={styles['form-input']}
>
<Label htmlFor={this.id} invalid={invalid} required={required}>
{label}
</Label>
</div>
In development the css module classes are there in the rendered markup:
<div class="form-group FormInput__form-input___2PK4N">
<label for="ctrl1" class="">Form Input</label>
</div>
But when I import the library, they are not there:
<div class="form-group">
<label for="ctrl1" class="">label</label>
</div>
If I log the styles variable from import styles from './FormInput.scss';
It is /static/media/Banner.ee4182d1.scss
which seems wrong.
I am using webpack and in my webpack.dev.config.js
my loader looks like this:
{
test: /\.css$/,
exclude: /node_modules/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
// Could also be write as follow:
// use: 'css-loader?modules&localIdentName=[name]__[local]___[hash:base64:5]!postcss-loader'
use: [
{
loader: 'css-loader',
query: {
modules: true,
localIdentName: '[name]__[local]___[hash:base64:5]'
}
},
'postcss-loader'
]
})
},
{
test: /\.scss$/,
exclude: /node_modules/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
// Could also be write as follow:
// use: 'css-loader?modules&importLoader=2&sourceMap&localIdentName=[name]__[local]___[hash:base64:5]!sass-loader'
use: [
{
loader: 'css-loader',
query: {
modules: true,
sourceMap: true,
importLoaders: 2,
localIdentName: '[name]__[local]___[hash:base64:5]'
}
},
'sass-loader'
]
})
}
And in webpack.prod.config.js
looks like this:
{
test: /\.css$/,
loader: ExtractTextPlugin.extract(
Object.assign(
{
fallback: require.resolve('style-loader'),
use: [
{
loader: require.resolve('css-loader'),
options: {
importLoaders: 1,
minimize: true,
sourceMap: true
}
},
{
loader: require.resolve('postcss-loader'),
options: {
ident: 'postcss', // https://webpack.js.org/guides/migrating/#complex-options
plugins: () => [
require('postcss-flexbugs-fixes'),
autoprefixer({
browsers: [
'>1%',
'last 4 versions',
'Firefox ESR',
'not ie < 9' // React doesn't support IE8 anyway
],
flexbox: 'no-2009'
})
]
}
}
]
},
extractTextPluginOptions
)
)
// Note: this won't work without `new ExtractTextPlugin()` in `plugins`.
},
{
test:/\.scss$/,
use:ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [
{
loader:'css-loader',
options:{
modules: true,
importLoaders: 1,
minimize: true,
sourceMap: true
}
},
{
loader: 'sass-loader',
},
{
loader: 'postcss-loader',
options:{
plugins:function(){
return [autoprefixer]
}
}
}
]
})
}
I can see that there are scsss files generated as part of the output but why have the classes been stripped?