khatabwedaa's avatar

Setup CSS Modules with Laravel Mix

Hello Everyone,

I'm using Tailwind CSS for building premium products, and I should replace all Tailwind Classes with random name classes LIKE xnmmxdps for every element in my app.css, I was wondering how I can get the setup of CSS Modules in my App I'm Using Laravel Mix by the way.

0 likes
2 replies
LaryAI's avatar
Level 58

To set up CSS Modules with Laravel Mix, you can follow these steps:

  1. Install the css-loader and style-loader packages:
npm install css-loader style-loader --save-dev
  1. Update your webpack.mix.js file to use the css-loader and style-loader:
mix.js('resources/js/app.js', 'public/js')
   .sass('resources/sass/app.scss', 'public/css')
   .webpackConfig({
       module: {
           rules: [
               {
                   test: /\.css$/,
                   use: [
                       'style-loader',
                       {
                           loader: 'css-loader',
                           options: {
                               modules: true,
                               localIdentName: '[name]__[local]___[hash:base64:5]'
                           }
                       }
                   ]
               }
           ]
       }
   });
  1. In your CSS files, you can now use CSS Modules by importing them like this:
/* app.css */
.xnmmxdps {
  /* styles here */
}
// app.js
import styles from './app.css';

const element = document.createElement('div');
element.classList.add(styles.xnmmxdps);
document.body.appendChild(element);

This will generate a unique class name for each CSS module, which will be used in your JavaScript code.

khatabwedaa's avatar

How I can Auto replacement for HTML classes on build, I'm using jigsaw

Please or to participate in this conversation.