How to use cssnano as the final CSS minifier with injectStyles: true while keeping Lightning CSS prefixing/transforms?
#7225
-
|
I am using Rsbuild and I have a case where I must keep Trying to understand the best/supported way to do this:
With I tried using In that setup, Lightning CSS still runs after PostCSS (loader order), and it reserializes the CSS. In practice, this reintroduces formatting/newlines/whitespace and cancels out cssnano's minified formatting. I tested this on my codebase and saw:
So Is there a supported way in Rsbuild to keep Lightning CSS prefixing/transforms but still make I am investigating this because I want predictable final minification behavior, and I am cautious about optimizer rewrites in some CSS cases. In particular, Lightning CSS can rewrite One reason I am cautious about Lightning CSS minification right now is this issue/PR context: I do not like that (at least from what I can see in Lightning CSS right now), I cannot easily configure Lightning CSS minifier behavior in a granular way to disable specific optimizations/rewrites that I consider risky for my project. Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
🧠 The "Internal Pipeline" Fix: Move Lightning CSS into PostCSSThe reason your CSS is being "un-minified" is that Rsbuild's default Lightning CSS loader runs after PostCSS. Even if To fix this and ensure 1. Install the PostCSS bridgenpm install postcss-lightningcss cssnano -D |
Beta Was this translation helpful? Give feedback.
-
|
the issue isn't just cssnano-it's the Rspack loader execution order |
Beta Was this translation helpful? Give feedback.
-
|
@x11kitty Thank you for your response! My goal is to avoid bloating the rsbuild configuration by moving or duplicating loaders. After further examining the Lightning CSS source code, I’m hesitant to use it in production due to concerns about the optimizations and normalizations it applies, its limited configurability, and questions about maintainability. For now, I’m willing to trade build speed for stability by using autoprefixer and cssnano. Although much slower than Lightning CSS, these tools produce even smaller output in some cases, and they do so without unsafe optimizations like losing 2D/3D context in transforms. |
Beta Was this translation helpful? Give feedback.
🧠 The "Internal Pipeline" Fix: Move Lightning CSS into PostCSS
The reason your CSS is being "un-minified" is that Rsbuild's default Lightning CSS loader runs after PostCSS. Even if
cssnanominifies the string, the subsequent Lightning CSS loader receives that string, parses it, applies transforms, and then re-serializes (pretty-prints) it into the JS bundle.To fix this and ensure
cssnanois the final serializer, you should move Lightning CSS into the PostCSS chain.1. Install the PostCSS bridge