Hi, Im trying to use the css modules with scss files. I have instaled the razzle-plugin-scss and trying:
import styles from "./App.module.css"; // works
import styles2 from "./App.module.scss"; // doesn't work
const App = () => (
<div className={styles.foo}>
But just the CSS works, not the SCSS variant.
I have also tied the configuration:
module.exports = {
plugins: [
{
name: "scss",
options: {
css: {
modules: true
}
}
},
But it doesn't work. Can you please help?
Thank You
@dizzyn The razzle plugin above I think is just for straightforward SCSS support.
I lifted this from elsewhere, but works for me. Essentially you copy the existing CSS loader configuration and apply it for .module.scss
extensions like so:
const makeLoaderFinder = require('razzle-dev-utils/makeLoaderFinder');
const cssLoaderFinder = makeLoaderFinder('css-loader');
const paths = require('razzle/config/paths');
module.exports = {
modify: (baseConfig, {dev}, webpack) => {
const config = Object.assign({}, baseConfig);
config.module.rules.filter(cssLoaderFinder).forEach(rule => {
const isCssModuleRule = !rule.test.test('module.css');
const scssExclude = [paths.appBuild];
let scssTest = /\.s[ac]ss$/;
if (isCssModuleRule) {
scssTest = /\.module\.s[ac]ss$/;
} else {
scssExclude.push(/\.module\.s[ac]ss$/);
}
// Use default configs
config.module.rules.push({
test: scssTest,
exclude: scssExclude,
use: [
...rule.use,
scssLoader,
]
});
});
}
};
In the razzle.config.js
I expanded the configuration webpack.
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const getCSSModuleLocalIdent = require('react-dev-utils/getCSSModuleLocalIdent');
const postcssNormalize = require('postcss-normalize');
const sassRegex = /\.(scss|sass)$/;
const sassModuleRegex = /\.module\.(scss|sass)$/;
const getStyleLoaders = (cssOptions, preProcessor) => {
const loaders = [
{
loader: MiniCssExtractPlugin.loader
},
{
loader: require.resolve('css-loader'),
options: cssOptions,
},
{
loader: require.resolve('postcss-loader'),
options: {
ident: 'postcss',
plugins: () => [
require('postcss-flexbugs-fixes'),
require('postcss-preset-env')({
autoprefixer: {
flexbox: 'no-2009',
},
stage: 3,
}),
postcssNormalize(),
],
sourceMap: false,
},
},
].filter(Boolean);
if (preProcessor) {
loaders.push({
loader: require.resolve(preProcessor),
options: {
sourceMap: false,
},
});
}
return loaders;
};
module.exports = {
modify: (config, { target, dev }, webpack) => {
config.plugins.push(new MiniCssExtractPlugin({
filename: 'static/css/bundle.[contenthash:8].css',
chunkFilename: 'static/css/[name].[contenthash:8].chunk.css',
allChunks: true,
}))
config.module.rules.push(
{
test: sassRegex,
exclude: sassModuleRegex,
use: getStyleLoaders(
{
importLoaders: 2,
sourceMap: false,
},
'sass-loader'
),
sideEffects: true,
},
{
test: sassModuleRegex,
use: getStyleLoaders(
{
importLoaders: 2,
sourceMap: false,
modules: true,
getLocalIdent: getCSSModuleLocalIdent,
},
'sass-loader'
),
}
)
return config;
}
};
it's work
import './Home.css';
import s from './Home.module.css';
import './a.scss';
import a from './a.module.scss';
I created pull request https://github.com/jaredpalmer/razzle/pull/1047 in wich css scss, *.module.scss, *.module.css supports in default configuration
Most helpful comment
I created pull request https://github.com/jaredpalmer/razzle/pull/1047 in wich css scss, *.module.scss, *.module.css supports in default configuration