New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature]: Lightning CSS Support #1702
Comments
plugin-lightningcssDesignMany bundlers and frameworks experimentally support lightningcss. There have been some good cases of lightningcss integration in the community such as lightningcss-loader and css-minimizer-webpack-plugin. In order to avoid future changes in api, our plugin design is as transparent as possible. InterfaceThere are two main parts of lightningcss, css-transformer and css-minifier. We already have The configuration of the plugin is as follows: export type PluginLightningcssOptions = {
/**
* @see https://github.com/fz6m/lightningcss-loader
* @default
* {
* targets: browserslistToTargets(browserslist)
* }
*/
lightningcssLoaderOptions?: ChainedConfig<LightningCssLoaderOptions>;
/**
* The 'insert-before'/'insert-after' define the insertion order of the lightningcss-loader based on the postcss-loader.
* @default false
*/
disableRemovePostCSS?: false | 'insert-before' | 'insert-after';
};
Here are some examples of users Example 1. only use lightningcss-loader as transformerimport { defineConfig } from '@rsbuild/core';
import { pluginReact } from '@rsbuild/plugin-react';
import { pluginLightningcss } from '@rsbuild/plugin-lightningcss';
import {
pluginCssMinimizer,
CssMinimizerWebpackPlugin,
} from '@rsbuild/plugin-css-minimizer';
export default defineConfig({
plugins: [
pluginReact(),
pluginLightningcss({
lightningcssLoaderOptions: {
// ... lightningcss options
},
// disableRemovePostCSS: false | 'insert-before' | 'insert-after'
}),
],
}); Example 2. use lightningcss as both transformer and minifierimport { defineConfig } from '@rsbuild/core';
import { pluginReact } from '@rsbuild/plugin-react';
import { pluginLightningcss } from '@rsbuild/plugin-lightningcss';
import {
pluginCssMinimizer,
CssMinimizerWebpackPlugin,
} from '@rsbuild/plugin-css-minimizer';
import lightningcss from 'lightningcss';
export default defineConfig({
plugins: [
pluginReact(),
pluginLightningcss({
lightningcssLoaderOptions: {
implementation: lightningcss,
// ... lightningcss options
},
// disableRemovePostCSS: false | 'insert-before' | 'insert-after'
}),
pluginCssMinimizer({
pluginOptions: {
minify: CssMinimizerWebpackPlugin.lightningCssMinify,
minimizerOptions: {
// ...
}
},
}),
],
}); Example 3. migrate from plugin-px2rem to plugin-lightningcss
parcel-bundler/lightningcss#483 Related links and information
feel free to give some feedback if something wrong |
@chenjiahan I decided to also builtin the feature of minimizer instead of using cssMinimizerWebpackPlugin.So I add a "false" type to turn it off and re-adjusted the plugin interface export type PluginLightningcssOptions = {
/**
* @see https://github.com/parcel-bundler/lightningcss/blob/master/node/index.d.ts
* @default
* {
* targets: browserslistToTargets(browserslist)
* }
*/
loaderOptions?: false | LightningCssLoaderOptions;
/**
* The 'insert-before'/'insert-after' define the insertion order of the lightningcss-loader based on the postcss-loader.
* @default false
*/
disableRemovePostCSS?: false | 'insert-before' | 'insert-after';
/**
* @see https://github.com/parcel-bundler/lightningcss/blob/master/node/index.d.ts
* @default
* {
* targets: browserslistToTargets(browserslist)
* }
*/
minimizerOptions?: false | LightningCssMinifyPluginOptions;
}; As for |
LGTM 😄
How about rename the options to: type PluginLightningcssOptions = {
/**
* @see https://github.com/parcel-bundler/lightningcss/blob/master/node/index.d.ts
* @default
* {
* targets: browserslistToTargets(browserslist)
* }
*/
transform?: false | LightningCssLoaderOptions;
/**
* @see https://github.com/parcel-bundler/lightningcss/blob/master/node/index.d.ts
* @default
* {
* targets: browserslistToTargets(browserslist)
* }
*/
minify?: false | LightningCssMinifyPluginOptions;
}; I removed the |
For |
Ok, I will do this
https://github.com/onigoetz/postcss-lightningcss by the way, postcss-lightningcss which is 3000 downloads weekly maybe a better choice |
This is not an ideal choice because:
|
I just considered the design of With PostCSS (default)The default order without lightning css:
With Lightning CSSWhen the lightning css plugin is used, the builtin PostCSS / autoprefixer is disabled by default, and the order will become:
With two transformersWhen the lightning css plugin is used, and if user needs to use
Other caseRsbuild will not provide an option for `Sass/Less -> Lightning CSS -> PostCSS', I haven't thought about when to use it yet. |
How can we detect the user's intention? Is this way? if (
config.tools.postcss !== undefined ||
config.tools.autoprefixer !== undefined
) {
use.before(CHAIN_ID.USE.POSTCSS);
} |
Yeah, we can check the Rsbuild config or the postcss config file. But this behavior is somewhat implicit, perhaps we still need a plugin option to control whether postcss-loader is enabled. type PluginLightningcssOptions = {
/**
* Whether to enable postcss-loader.
* @default false
*/
postcss?: boolean;
/**
* We can add this option if needed. It does not need to be implemented in the first release.
* @default 'postcssFirst'
*/
executeOrder?: 'postcssFirst' | 'lightningcssFirst'
}; |
What problem does this feature solve?
Lightning CSS is a popular CSS transformer based on Rust. It can solve the performance overhead caused by PostCSS + autoprefixer in the current Rsbuild, and it can also replace SWC CSS for CSS compression.
At present, we cannot determine whether Rspack will integrate Lightning CSS in the future, or replace SWC CSS. However, Rsbuild can first support the integration of Lightning CSS as a plugin, to verify the benefits of Lightning CSS.
What does the proposed API look like?
The text was updated successfully, but these errors were encountered: