Less Plugin

Use Less as the CSS preprocessor, implemented based on less-loader.

Quick Start

Install Plugin

You can install the plugin using the following command:

npm
yarn
pnpm
bun
npm add @rsbuild/plugin-less -D
TIP
  • The Less plugin only supports @rsbuild/core versions >= 0.7.0.
  • If the @rsbuild/core version is lower than 0.7.0, it has builtin support for the Less plugin, you do not need to install it.

Register Plugin

You can register the plugin in the rsbuild.config.ts file:

rsbuild.config.ts
import { pluginLess } from '@rsbuild/plugin-less';

export default {
  plugins: [pluginLess()],
};

After registering the plugin, you can import *.less or *.module.less files into the code without adding other configs.

Options

If you need to customize the compilation behavior of Less, you can use the following configs.

lessLoaderOptions

You can modify the config of less-loader via lessLoaderOptions.

  • Type: Object | Function
  • Default:
const defaultOptions = {
  lessOptions: {
    javascriptEnabled: true,
    paths: [path.join(rootPath, 'node_modules')],
  },
  implementation: require.resolve('less'),
  sourceMap: rsbuildConfig.output.sourceMap.css,
};
  • Example:

If lessLoaderOptions is an object, it is merged with the default config through Object.assign in a shallow way. It should be noted that lessOptions is merged through deepMerge in a deep way.

pluginLess({
  lessLoaderOptions: {
    javascriptEnabled: false,
  },
});

If lessLoaderOptions is a function, the default config is passed as the first parameter, which can be directly modified or returned as the final result.

pluginLess({
  lessLoaderOptions(config) {
    config.lessOptions = {
      javascriptEnabled: false,
    };
  },
});

Modifying Less Version

In some scenarios, if you need to use a specific version of Less instead of the built-in Less v4 in Rsbuild, you can install the desired Less version in your project and set it up using the implementation option of the less-loader.

pluginLess({
  lessLoaderOptions: {
    implementation: require('less'),
  },
});