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>
);


0

3 Answers 3

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',
  ],
},
Sign up to request clarification or add additional context in comments.

2 Comments

Why you do not mark your answer as the correct one?
Amazing, I spent two days on this! You should accept this answer.
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>
);

Comments

1

You need to write classname like this

const App = () => {
<h1 className={[style[`bg-teal-600`], style.holi].join(' ')}>Holi</h1>;
};

1 Comment

Thanks for your answer. Could you describe what your config?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.