L
L
lexstile2021-05-13 22:16:39
webpack
lexstile, 2021-05-13 22:16:39

How to enable css modules for sass/scss only?

How to enable css modules only for certain type(s) of files?
In my case, I need to disable modules for .css, leave the rest.

webpack:

spoiler
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const autoprefixer = require('autoprefixer');

const path = require('path');

const htmlWebpackPluginConfig = new HtmlWebpackPlugin({
  template: __dirname + '/src/public/index.html',
  inject: 'body',
  scriptLoading: 'defer',
});

const miniCssExtractConfig = new MiniCssExtractPlugin({
  filename: '[name].[hash].css',
  chunkFilename: '[id].[hash].css',
});

module.exports = {
  entry: __dirname + '/src/index.js',
  output: {
    // path: __dirname + '/dist',
    path: __dirname,
    filename: '[name].[hash].js',
    publicPath: '/',
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        use: ['babel-loader', 'eslint-loader'],
        exclude: [/node_modules/],
      },
      {
        test: /\.(sass|scss|css)$/,
        use: [
          MiniCssExtractPlugin.loader,
          {
            loader: 'css-loader',
            options: {
              modules: {
                exportGlobals: true,
                localIdentName: '[local]--[hash:base64:5]',
              },
            },
          },
          {
            loader: 'sass-loader',
          },
          {
            loader: 'postcss-loader',
            options: {
              postcssOptions: {
                plugins: [
                  [
                    autoprefixer({
                      browsers: ['> 0%'],
                    }),
                  ],
                ],
              },
              sourceMap: true,
            },
          },
        ],
      },
    ],
  },
  resolve: {
    alias: {
      components: path.join(__dirname, 'src/components'),
      constants: path.join(__dirname, 'src/constants'),
      helpers: path.join(__dirname, 'src/helpers'),
      store: path.join(__dirname, 'src/store'),
      pages: path.join(__dirname, 'src/pages'),
      utils: path.join(__dirname, 'src/utils'),
      styles: path.join(__dirname, 'src/styles'),
      api: path.join(__dirname, 'src/api'),
    },
    extensions: ['.js', '.jsx'],
  },
  plugins: [htmlWebpackPluginConfig, miniCssExtractConfig],
  devServer: {
    contentBase: './src/public',
    port: 10888,
  },
};

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ravil, 2021-05-13
@lexstile

For myself, I solve this problem by adding a line to the config: auto: /\.module\.\w+$/i,.
This means that css modules will only work for those css files that have ".module" in their name.
For example: app.module.scss

{
            loader: 'css-loader',
            options: {
              modules: {
                exportGlobals: true,
                localIdentName: '[local]--[hash:base64:5]',
                auto: /\.module\.\w+$/i,
              },
            },
          },

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question