rsbuild icon indicating copy to clipboard operation
rsbuild copied to clipboard

[Bug]: PostCSS plugins gets reinitialized on compile

Open sibbng opened this issue 1 year ago • 4 comments

Version

System:
    OS: Windows 10 10.0.19045
  npmPackages:
    @rsbuild/core: ^1.0.1 => 1.0.7
    @rsbuild/plugin-vue: ^1.0.1 => 1.0.1

Details

Rsbuild re-initializes PostCSS plugins on each compile step. Both TailwindCSS and UnoCSS plugins expect it to be initialized only once.

https://github.com/tailwindlabs/tailwindcss/blob/6a50e6e1605cd8c4e071970211ae6254cc1c6313/packages/%40tailwindcss-postcss/src/index.ts#L40-L52

https://github.com/unocss/unocss/blob/55b505917d49865d1720d9819bf6ae5a0beb014f/packages/postcss/src/index.ts#L9-L10

When plugins gets reinitialized, they lose global state and this slows down compile step significantly. As far as I know, Vite, initialize PostCSS plugins body only once.

In the following reproduction, it gets even worse and plugins gets initialized twice on each re-compile.

Reproduce link

https://github.com/sibbng/rsbuild-vue3

Reproduce Steps

  1. pnpm i
  2. pnpm dev

sibbng avatar Sep 30 '24 20:09 sibbng

The reproduce link is 404:

image

I tried to reproduce this issue with tailwindcss v3, but I can not reproduce it:

image

chenjiahan avatar Oct 01 '24 03:10 chenjiahan

Hello @sibbng. Please provide a reproduction repository or online demo. For background, see Why reproductions are required. Thanks ❤️

github-actions[bot] avatar Oct 01 '24 03:10 github-actions[bot]

Sorry, it should be accessible now.

My reproductions uses Tailwind V4, but I observe same behaviour on UnoCSS too. image

sibbng avatar Oct 01 '24 04:10 sibbng

It appears to behave consistently with Vite, invoking the plugin for each module.

inottn avatar Oct 01 '24 13:10 inottn

As the issue was labelled with need reproduction, but no response in 5 days. This issue will be closed. Feel free to comment and reopen it if you have any further questions. For background, see Why reproductions are required.

由于该 issue 被标记为 "需要重现",但在 5 天内没有回应,因此该 issue 将被关闭。如果你有任何进一步的问题,请随时发表评论并重新打开该 issue。背景请参考 为什么需要最小重现

github-actions[bot] avatar Oct 07 '24 00:10 github-actions[bot]

@chenjiahan

I added reproduction to the issue.

sibbng avatar Oct 07 '24 09:10 sibbng

This issue is caused by postcss-load-config, see https://github.com/postcss/postcss-load-config/issues/225

A simple workaround is invoking the tailwindcss plugin in the postcss config file:

module.exports = {
  plugins: [require('@tailwindcss/postcss')()],
};

I will further investigate other solutions.

chenjiahan avatar Oct 11 '24 08:10 chenjiahan