[英]Multiple Tailwind CSS classes having multiple Webpack entry points
問題陳述
所以我有一個帶有 webpack 和 tailwind CSS 的 React 項目設置。
在我的 webpack 配置中,我有多個入口點,以便為每個入口點生成不同的 CSS 和 JS。
當我在我的 React 組件中使用順風類時,就會出現問題。
假設我僅在 Component1(或入口點 1)中使用順風類bg-red-600 。
因此,通過 webpack 構建文件后, bg-red-600將出現在所有入口點生成的 CSS 文件中(請記住,我只是在第一個入口點組件中使用了這個類)。
它應該做的只是在第一個組件 CSS 文件中有bg-red-600類,而不是在所有 CSS 文件中預設它,即使我沒有在第一個組件以外的任何其他地方使用它。
希望我能夠表達我的觀點。
謝謝。
Webpack 的入口點:
entry: {
app1: path.resolve(
__dirname,
'src/Component1'
),
app2: path.resolve(
__dirname,
'src/Component2'
),
},
這是我的解決方案:
/config
文件夾,其中包含每個條目 js 的自定義 tailwind-xxx.config 文件例如。 /config/tailwind-ConfirmButton.config.js
:
module.exports = {
content: [
'./src/common/ConfirmButton/ConfirmButton.jsx',
],
// plugins: [require('@tailwindcss/forms')],
}
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,
},
],
},
/src/widgets/customButton.js
:...
render(
<ConfirmButton
expId={expId}
content={content}
confirmBtn={confirmBtn}
cancelBtn={cancelBtn}
field={field}
/>,
container
)
weppack --mode=production
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.