简体   繁体   English

具有多个 Webpack 入口点的多个 Tailwind CSS 类

[英]Multiple Tailwind CSS classes having multiple Webpack entry points

Problem statement问题陈述

So I have a React project setup with webpack and tailwind CSS.所以我有一个带有 webpack 和 tailwind CSS 的 React 项目设置。
In my webpack config I have multiple entry point in order to generate different CSS and JS for each entry point.在我的 webpack 配置中,我有多个入口点,以便为每个入口点生成不同的 CSS 和 JS。
The problem arises when I use the tailwind classes in my React components.当我在我的 React 组件中使用顺风类时,就会出现问题。
Let's suppose if I use a tailwind class bg-red-600 only in Component1(or entry point 1).假设我仅在 Component1(或入口点 1)中使用顺风类bg-red-600
So after building the files through webpack the bg-red-600 will be present in all the entry point's generated CSS files(keep in mind I have just used this class in first entry point component only).因此,通过 webpack 构建文件后, bg-red-600将出现在所有入口点生成的 CSS 文件中(请记住,我只是在第一个入口点组件中使用了这个类)。
What it should be doing is only have bg-red-600 class in first component CSS file instead it is preset in all the CSS files even though I have not used it in any other place other than first component.它应该做的只是在第一个组件 CSS 文件中有bg-red-600类,而不是在所有 CSS 文件中预设它,即使我没有在第一个组件以外的任何其他地方使用它。

Hope I was able to made my point.希望我能够表达我的观点。

Thanks.谢谢。

Webpack's entry points: Webpack 的入口点:

entry: {
    app1: path.resolve(
        __dirname,
        'src/Component1'
    ),
    app2: path.resolve(
        __dirname,
        'src/Component2'
    ),

},

Here is my solution:这是我的解决方案:

  1. /config folder with custom tailwind-xxx.config file for each entry js /config文件夹,其中包含每个条目 js 的自定义 tailwind-xxx.config 文件

eg.例如。 /config/tailwind-ConfirmButton.config.js : /config/tailwind-ConfirmButton.config.js

module.exports = {
  content: [
    './src/common/ConfirmButton/ConfirmButton.jsx',
  ],
  // plugins: [require('@tailwindcss/forms')],
}


  1. webpack.config.js webpack.config.js
const postcssOpts = { postcssOptions: env => {
    // here is the point
    const component = env._module.context.slice(env._module.context.lastIndexOf('/') + 1)

    return { 
      plugins: [
        ['tailwindcss', {
          config: `./config/tailwind-${component}.config.js`,
        }],
        autoprefixer
      ] 
    }
  } 
}

...

entry: {
  confirm: path.resolve(__dirname, './src/widgets/confirmButton.js'),
},
target: ['web', 'es5'], // <=== can be omitted as default is 'web'
output: {
  filename: '[name]/tag.js',
  path: path.resolve(__dirname, 'dist/exp'),
  publicPath: './',
},

...

{
  test: /\.css$/,
  use: [
    { loader: 'style-loader' },
    { loader: 'css-loader' },
    {
      loader: 'postcss-loader',
      options: postcssOpts,
    },
  ],
},
  1. entry widget js eg.入口小部件 js 例如。 /src/widgets/customButton.js : /src/widgets/customButton.js
...

render(
  <ConfirmButton
    expId={expId}
    content={content}
    confirmBtn={confirmBtn}
    cancelBtn={cancelBtn}
    field={field}
  />,
  container
)
  1. finally run weppack --mode=production最后运行weppack --mode=production

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM