【问题标题】:tailwind css with css modules in next.js在 next.js 中带有 css 模块的 tailwind css
【发布时间】:2020-02-22 16:45:16
【问题描述】:

如何配置 next.js 以支持带有 css 模块的同时顺风 css? 我想要 tailwindcss 包装整个项目:

// /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';
...

在一个示例组件中:

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

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


【问题讨论】:

  • 好问题,+1 +1 !!

标签: reactjs sass next.js css-modules tailwind-css


【解决方案1】:

一种解决方案是拆分 webpack 样式加载器。一个用于全局 css 的加载器,另一个用于 css 模块加载器,因此 webpack 加载器如下所示:

{
  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',
  ],
},

【讨论】:

  • 你为什么不把你的答案标记为正确的?
【解决方案2】:

你需要这样写类名

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

【讨论】:

  • 感谢您的回答。你能描述一下你的配置吗?
猜你喜欢
  • 2020-08-29
  • 2021-04-05
  • 2021-08-07
  • 2021-08-20
  • 2021-10-16
  • 2019-03-09
  • 2018-04-07
  • 1970-01-01
  • 2017-03-26
相关资源
最近更新 更多