Consider using lightningcss as a CSS minifier #46536
Replies: 12 comments 3 replies
-
@devongovett FYI, React Beta Docs (https://beta.reactjs.org/) has only 1 CSS file and the after-minified size is at 75.2 KiB. Is it possible for Besides, Also, IMHO a normal Next.js project would usually output less than 10 CSS files, thus the speed of And the npm install size is also an important thing to be considered. Currently, Next.js' built-in cssnano preset has an install size of |
Beta Was this translation helpful? Give feedback.
-
No,
Browser targets have a big effect on this, since they allow
Again, I wouldn't expect the difference in size to be massive – cssnano is a very good minifier, and there's only so much that can be done safely, especially in this case where the input CSS is generated by Tailwind rather than authored by hand. The ability to take advantage of browser improvements over time is a powerful feature though.
Merging adjacent
Depends how big those files are. The time can add up quickly, and can easily represent a significant percentage of the overall build time. Making this 100x faster could be a nice speedup.
This depends on the platform since |
Beta Was this translation helpful? Give feedback.
-
Sorry I didn't make my idea clear. What I mean is that While as I said. Next.js includes some legacy browsers in its default browserlists. Thus some optimization provided by
In my personal case, I use
|
Beta Was this translation helpful? Give feedback.
-
True, but (a) it should be at least as good as cssnano, and (b) users can customize their browserslist so may be able to get additional benefit over the defaults.
I just implemented it in parcel-bundler/lightningcss@ae87b83. Should go out with the next release.
Where does this concern come from? I just tested and on my machine:
|
Beta Was this translation helpful? Give feedback.
-
Hey @devongovett, it's definitely something we are considering. SWC also has a CSS parser, and although it's less far along than Lightning, the team sees some advantages to using the same tool for JS/CSS compilation. We'll be evaluating whether we want to invest more in the SWC CSS compiler or use something else soon, but we've got some higher priority items, like polishing up the minifier, that we need to focus on first. |
Beta Was this translation helpful? Give feedback.
-
Apart from minification there are some features that Lightning CSS does better than any PostCSS plugins or other preprocessors I've seen, like downleveling of the |
Beta Was this translation helpful? Give feedback.
-
With the latest release, can lightningcss now replace the postCSS as well? @devongovett ? With the plan for Nextjs to allow usage of bun with Next, is it possible to give first-party support for lightingcss as well? As to create documentation on how to use NextJS with lightningCSS |
Beta Was this translation helpful? Give feedback.
-
Nothing has changed since the last comment above – the "usage of bun" here I'm assuming is referring to my draft PR for using bun as a packager manager with |
Beta Was this translation helpful? Give feedback.
-
I'd love it if I could at least write a Next.js plugin for Lightning CSS. I tried at one point and it was possible to override the webpack CSS plugins, but it seemed kinda brittle to be splicing plugins in the array at hard coded indices. So many developers use Next.js, and it's unfortunate that they are locked into the tools that it ships with. This makes it really hard for new tools in a specific area to gain adoption. It would be huge for the ecosystem if the defaults could be swapped out for alternatives, allowing devs to continue using next.js while trying out new tools. |
Beta Was this translation helpful? Give feedback.
-
It is always difficult to modify the Next.js' built-in webpack config without breaking anything, but this should work: import NextBuiltinMiniCssExtractPlugin from 'next/dist/compiled/build/webpack/...';
// ...
webpackConfig.plugins = webpackConfig.plugins.map(plugin => {
if (plugin instanceof NextBuiltinMiniCssExtractPlugin) {
return new MiniCssExtractPlugin({ /* now with lightningcss enabled */ });
}
return plugin;
}); |
Beta Was this translation helpful? Give feedback.
-
Does #58471 change things now? Any updates on replacing postcss? Thanks |
Beta Was this translation helpful? Give feedback.
-
Something related #67417 |
Beta Was this translation helpful? Give feedback.
-
Describe the feature you'd like to request
Lightning CSS is a new CSS transformer and minifier in Rust, built by the team behind the Parcel bundler. It is significantly faster than any other CSS minifier I am aware of, while producing smaller output in many cases. It's over 100x faster than cssnano, the CSS minifier used by Next.js today.
I think Next.js could adopt
lightningcss
as a minifier easily, and users would see reduced production build times, with potential for reduced bundle sizes as well. In the future, additional features such as replacing postcss-preset-env and autoprefixer could also be adopted, but minification is the easiest place to start.The GitHub readme has information about the features. For more about the internal architecture, see the announcement blog post. As if March, it has been the default CSS minifier in Parcel.
Describe the solution you'd like
css-minimizer-webpack-plugin already supports using
lightningcss
with a one line change.This could easily be opt-in at first (similar to the
swcMinify
option for JavaScript) if you are concerned about bugs or regressions.Describe alternatives you've considered
I am biased as the main author of
lightningcss
, but I think changing the default would be a good choice. However, if you don't want to do this, I think it would be nice to be able to customize the minifier more easily. I tried to write a Next.js plugin to do this, but it involves some brittle hackery to the webpack config. There's no way to detect the default minimizer plugin, so you have to assume the index in the list of minimizers in the webpack config doesn't change in order to delete the default and add the replacement. Perhaps a config option similar to the one supported by css-minimizer-webpack-plugin could be supported to allow users choice over the minifier they use.I am aware that SWC is also working on a CSS minifier. However,
lightningcss
is ready today. Again, I'm biased, but I think the architecture of Lightning CSS will enable it to continue to be faster and produce smaller output. At least offering users a choice over what minifier they use would be better than locking them in.Beta Was this translation helpful? Give feedback.
All reactions