Gatsby plugin to add support for using Less CSS preprocessor with webpack configuration
npx @tessl/cli install tessl/npm-gatsby-plugin-less@7.15.0gatsby-plugin-less provides seamless integration of Less CSS preprocessor into Gatsby projects. It automatically configures webpack to handle .less files through a processing pipeline that includes less-loader for Less compilation, css-loader for CSS modules support, postcss-loader for autoprefixing and optimization, and mini-css-extract-plugin for CSS extraction.
npm install gatsby-plugin-less lessThis plugin is configured through Gatsby's plugin system, not imported directly:
// in gatsby-config.js
module.exports = {
plugins: [`gatsby-plugin-less`]
}With configuration options:
// in gatsby-config.js
module.exports = {
plugins: [
{
resolve: `gatsby-plugin-less`,
options: {
// Plugin options go here
}
}
]
}Add the plugin to your gatsby-config.js and start using .less files:
// gatsby-config.js
module.exports = {
plugins: [`gatsby-plugin-less`]
}// src/styles/main.less
@primary-color: #1890ff;
.header {
color: @primary-color;
font-size: 24px;
&:hover {
color: darken(@primary-color, 10%);
}
}// src/components/Header.js
import '../styles/main.less';
export default function Header() {
return <h1 className="header">Welcome</h1>;
}CSS modules work automatically with .module.less files:
// src/components/Button.module.less
@button-height: 40px;
.button {
height: @button-height;
padding: 0 16px;
border: none;
border-radius: 4px;
&.primary {
background: #1890ff;
color: white;
}
}// src/components/Button.js
import { button, primary } from './Button.module.less';
export default function Button({ variant, children }) {
return (
<button className={variant === 'primary' ? `${button} ${primary}` : button}>
{children}
</button>
);
}Configures webpack to process Less files in Gatsby applications.
// Plugin configuration in gatsby-config.js
{
resolve: `gatsby-plugin-less`,
options: {
lessOptions?: LessOptions;
loaderOptions?: LessLoaderOptions;
cssLoaderOptions?: CssLoaderOptions;
postCssPlugins?: PostCSSPlugin[];
}
}Configure the Less compiler behavior.
/**
* Less compiler configuration options
* @typedef {Object} LessOptions
* @property {boolean} [strictMath] - Enable strict math mode for calculations
* @property {LessPlugin[]} [plugins] - Array of Less plugins
* @property {Object.<string, string>} [modifyVars] - Modify Less variables at compile time
* @property {string[]} [paths] - Paths to search for @import directives
* @property {boolean} [relativeUrls] - Enable relative URLs
* @property {boolean} [compress] - Compress output CSS
* @property {boolean|SourceMapOptions} [sourceMap] - Generate source maps
*/Usage:
// gatsby-config.js
module.exports = {
plugins: [
{
resolve: `gatsby-plugin-less`,
options: {
lessOptions: {
strictMath: true,
modifyVars: {
'primary-color': '#ff6b35',
'border-radius-base': '2px'
},
plugins: [
new (require('less-plugin-clean-css'))({ advanced: true })
]
}
}
}
]
}
// Alternative: Load variables from an external file
module.exports = {
plugins: [
{
resolve: `gatsby-plugin-less`,
options: {
lessOptions: {
modifyVars: require('./src/theme.js')
}
}
}
]
}Where theme.js exports a variable object:
// src/theme.js
module.exports = {
'primary-color': '#1890ff',
'text-color': '#333'
}Configure less-loader specific behavior.
/**
* Less loader configuration options
* @typedef {Object} LessLoaderOptions
* @property {string} [appendData] - Append data to every Less file
* @property {string} [prependData] - Prepend data to every Less file
* @property {string|Function} [additionalData] - Additional data to pass to Less compiler
* @property {Object} [implementation] - Custom Less implementation to use instead of default
* @property {boolean} [sourceMap] - Source map configuration
*/Usage:
// gatsby-config.js
module.exports = {
plugins: [
{
resolve: `gatsby-plugin-less`,
options: {
loaderOptions: {
appendData: `@env: ${process.env.NODE_ENV};`
}
}
}
]
}Configure css-loader behavior for CSS processing.
/**
* CSS loader configuration options
* @typedef {Object} CssLoaderOptions
* @property {boolean|CssModulesOptions} [modules] - Enable/disable CSS modules
* @property {boolean} [camelCase] - Enable/disable camelCase transformation
* @property {boolean} [esModule] - Enable/disable ES modules syntax for exported names
* @property {number} [importLoaders] - Number of loaders applied before css-loader
* @property {boolean} [sourceMap] - Enable/disable source maps
* @property {boolean} [import] - Enable/disable CSS imports resolution
* @property {boolean} [url] - Enable/disable CSS url() resolution
*/
/**
* CSS modules configuration options
* @typedef {Object} CssModulesOptions
* @property {boolean} [namedExport] - Enable named exports for CSS modules
* @property {string} [localIdentName] - CSS class naming pattern
* @property {boolean} [exportGlobals] - Export globals alongside CSS modules
*/Usage:
// gatsby-config.js
module.exports = {
plugins: [
{
resolve: `gatsby-plugin-less`,
options: {
cssLoaderOptions: {
camelCase: false,
esModule: false,
modules: {
namedExport: false,
localIdentName: '[name]__[local]--[hash:base64:5]'
}
}
}
}
]
}Add PostCSS plugins for additional CSS processing.
/**
* PostCSS plugin configuration
* @typedef {Object} PostCSSPlugin
* @property {string|Function} plugin - Plugin name or instance
* @property {*} [options] - Plugin options
*/Usage:
// gatsby-config.js
module.exports = {
plugins: [
{
resolve: `gatsby-plugin-less`,
options: {
postCssPlugins: [
require('autoprefixer')({ grid: true }),
require('cssnano')({ preset: 'default' })
]
}
}
]
}Files matching *.less pattern are processed with CSS modules disabled by default.
/\.less$/Files matching *.module.less pattern are processed with CSS modules enabled.
/\.module\.less$/During SSR stages (develop-html, build-html), CSS processing is bypassed using null loaders to prevent issues during HTML generation.
// SSR behavior (automatic)
const isSSR = [`develop-html`, `build-html`].includes(stage);
// Uses null loader instead of CSS processing chain during SSR/**
* Gatsby plugin options for gatsby-plugin-less
* @typedef {Object} GatsbyPluginLessOptions
* @property {Object} [lessOptions] - Options passed to the Less compiler
* @property {Object} [loaderOptions] - Options passed to less-loader
* @property {Object} [cssLoaderOptions] - Options passed to css-loader
* @property {Array} [postCssPlugins] - Array of PostCSS plugins to apply
*/
/**
* Less plugin interface
* @typedef {Object} LessPlugin
* @property {Function} install - Plugin installation method (less: any, pluginManager: any) => void
* @property {number[]} [minVersion] - Plugin minimum Less version requirement
*/
/**
* Source map configuration options
* @typedef {Object} SourceMapOptions
* @property {boolean} [sourceMapContents] - Include source content in source map
* @property {string} [sourceMapFilename] - Include filename in source map
* @property {string} [sourceMapBasepath] - Base path for source map URLs
* @property {string} [sourceMapRootpath] - Root path for source map
* @property {string} [sourceMapURL] - URL for source map
*/