Less.js plugin that integrates clean-css for CSS minification and optimization
npx @tessl/cli install tessl/npm-less-plugin-clean-css@1.6.0Less Plugin Clean CSS is a Less.js plugin that integrates the clean-css library for CSS minification and optimization. It enables developers to compress CSS output from Less compilation by applying advanced optimizations including whitespace removal, property merging, selector optimization, and dead code elimination.
npm install less-plugin-clean-cssconst LessPluginCleanCSS = require('less-plugin-clean-css');const less = require('less');
const LessPluginCleanCSS = require('less-plugin-clean-css');
// Create plugin instance with options
const cleanCSSPlugin = new LessPluginCleanCSS({
advanced: true,
keepSpecialComments: 1,
compatibility: 'ie8'
});
// Use with Less.js
less.render(lessString, { plugins: [cleanCSSPlugin] })
.then(result => {
console.log(result.css); // Minified CSS output
});# Install Less.js globally
npm install -g less
# Install the plugin globally
npm install -g less-plugin-clean-css
# Use with lessc
lessc input.less --clean-css="--s1 --advanced --compatibility=ie8"The plugin follows the Less.js plugin architecture with these key components:
LessPluginCleanCSS - Entry point and configuration managementCreates a new Less.js plugin instance for CSS minification.
/**
* Creates a Less.js plugin instance for CSS minification
* @param {Object|String} options - Configuration options for clean-css
*/
function LessPluginCleanCSS(options);Registers the plugin with Less.js to enable CSS minification during compilation.
/**
* Registers the plugin with Less.js plugin manager
* @param {Object} less - Less.js instance
* @param {Object} pluginManager - Less.js plugin manager
*/
LessPluginCleanCSS.prototype.install = function(less, pluginManager);Prints detailed usage information and available options to the console.
/**
* Prints usage information for the plugin to console
*/
LessPluginCleanCSS.prototype.printUsage = function();Updates plugin configuration after instantiation.
/**
* Updates plugin options after instantiation
* @param {Object|String} options - New configuration options
*/
LessPluginCleanCSS.prototype.setOptions = function(options);Specifies the minimum required Less.js version for compatibility.
/**
* Minimum required Less.js version [2, 1, 0]
* @type {Array<number>}
*/
LessPluginCleanCSS.prototype.minVersion = [2, 1, 0];The plugin supports all clean-css options with some defaults optimized for Less.js output:
When using string format (command-line), options are space-separated:
keep-line-breaks or b - Preserve line breaks in outputs0 - Remove all special commentss1 - Keep first special comment onlykeepSpecialComments=* - Keep all special comments (default)advanced - Enable advanced optimizations (default: false)rebase - Enable URL rebasing (default: false)compatibility=ie8 - Set CSS compatibility moderounding-precision=2 - Set decimal precision for numbersskip-aggressive-merging - Disable aggressive property mergingskip-restructuring - Disable CSS restructuringskip-shorthand-compacting - Disable shorthand property compactingWhen using object format (programmatic), use clean-css option names:
interface CleanCSSOptions {
/** Keep special comments: "*" (all), 1 (first), 0 (none) */
keepSpecialComments?: string | number;
/** Enable advanced optimizations */
advanced?: boolean;
/** Enable URL rebasing */
rebase?: boolean;
/** Preserve line breaks */
keepBreaks?: boolean;
/** CSS compatibility mode */
compatibility?: string;
/** Decimal precision */
roundingPrecision?: number;
/** Enable aggressive property merging */
aggressiveMerging?: boolean;
/** Enable CSS restructuring */
restructuring?: boolean;
/** Enable shorthand property compacting */
shorthandCompacting?: boolean;
}Usage Examples:
// Object format
const plugin = new LessPluginCleanCSS({
advanced: true,
keepSpecialComments: 1,
compatibility: 'ie8',
roundingPrecision: 2
});
// String format (parsed internally)
const plugin2 = new LessPluginCleanCSS('s1 advanced compatibility=ie8 rounding-precision=2');The plugin applies these defaults different from clean-css:
keepSpecialComments: "*" - Keeps all special comments by defaultprocessImport: false - Disabled to avoid conflicts with Less importsrebase: false - Disabled by default for safetyadvanced: false - Disabled by default for safetyThe plugin throws errors for:
// Webpack with less-loader
module.exports = {
module: {
rules: [{
test: /\.less$/,
use: [{
loader: 'style-loader'
}, {
loader: 'css-loader'
}, {
loader: 'less-loader',
options: {
lessOptions: {
plugins: [
new (require('less-plugin-clean-css'))({ advanced: true })
]
}
}
}]
}]
}
};const express = require('express');
const less = require('less');
const LessPluginCleanCSS = require('less-plugin-clean-css');
app.get('/styles.css', async (req, res) => {
const lessContent = await fs.readFile('styles.less', 'utf8');
const result = await less.render(lessContent, {
plugins: [new LessPluginCleanCSS({ advanced: true })]
});
res.type('text/css').send(result.css);
});