tailwind css with css modules in next.js
Asked Answered
S

3

27

How to config next.js to support Simultaneous tailwind css with css modules? I want tailwindcss wrap whole project:

// /tailwind.scss
:global {
  @import "tailwindcss/base";
  @import "tailwindcss/components";
  @import "tailwindcss/utilities";
}

// /test-module.css
.example {
  font-size: 36px;
}

// /pages/_app.jsx
import '../talwind.scss';
...

And in a sample component:

// /components/my-component.jsx
import css from '../test-module.css';

const Test = () => (
  <div className={`bg-red-500` ${css.example}}>Test Tailwind with CSS</div>
);


Shoffner answered 26/10, 2019 at 18:38 Comment(0)
S
5

A solution is split webpack style loader. A loader for global css another for css modules loader so webpack loader is looks like below:

{
  test: /\.s?[ac]ss$/,
  exclude: /\.global.css$/,
  use: [
    {
      loader: MiniCssExtractPlugin.loader,
      options: {
        hmr: !isProduction,
        reloadAll: true,
      },
    },
    // 'css-loader',
    {
      loader: 'css-loader',
      options: {
        importLoaders: 1,
        modules: {
          localIdentName: '[name]__[local]___[hash:base64:5]',
        },
      },
    },
    'postcss-loader',
    { loader: 'sass-loader', options: { sourceMap: true } },
  ],
},
{
  test: /\.global.css$/,
  use: [
    {
      loader: MiniCssExtractPlugin.loader,
      options: {
        hmr: !isProduction,
        reloadAll: true,
      },
    },
    'css-loader',
    'postcss-loader',
  ],
},
Shoffner answered 10/11, 2019 at 10:5 Comment(2)
Why you do not mark your answer as the correct one?Sorrow
Amazing, I spent two days on this! You should accept this answer.Cherey
T
3

I had the same problem, I did the following and it worked:

const Test = () => (
    <div className={["bg-red-500", css.example].join(" ")}>
        Test Tailwind with CSS
    </div>
);
Tillietillinger answered 20/1, 2022 at 13:51 Comment(0)
I
1

You need to write classname like this

const App = () => {
<h1 className={[style[`bg-teal-600`], style.holi].join(' ')}>Holi</h1>;
};
Isodiametric answered 30/11, 2019 at 19:13 Comment(1)
Thanks for your answer. Could you describe what your config?Shoffner

© 2022 - 2024 — McMap. All rights reserved.