LESS Silent Multiline Comment

Is there a way to create silent multi-line comments in LESS? I want the same behavior as // comment, but for multi-line strings.


source to share

1 answer

As already explained by @harry, options -x

and clean-css

also remove comments. Since version 2, the clean-css option has been moved to plugin ( npm install -g less-plugin-clean-css


Since Less 2 you can use plugins, see also http://lesscss.org/usage/#plugins for you to write and use a plugin that removes your multiline comments.


Download and unzip clean-css to your working directory. You can find the clean-css at https://github.com/jakubpawlowicz/clean-css (this will create a minor senior called clean-css-master)

To create your plugin, call this file less-plugin-remove-comments.js


var getCommentsProcessor = require("./comments-processor");

module.exports = {
    install: function(less, pluginManager) {
        var CommentsProcessor = getCommentsProcessor(less);
        pluginManager.addPostProcessor(new CommentsProcessor());


Yours comment-processor.js

, which may contain the following:

var cleaner = require('./clean-css-master/lib/text/comments-processor');

module.exports = function(less) {
    function CommentProcessor(options) {
        this.options = options || {};

    CommentProcessor.prototype = {
        process: function (css) {
            var commentsProcessor = new cleaner('*', false);
            css = commentsProcessor.escape(css);
            return css;

    return CommentProcessor;


Finally, you should be able to run the following command:

lessc --plugin=./less-plugin-remove-comments.js index.less


The previous command should give you the compiled CSS without comments.



All Articles