5

Is there a way to create silent multiline comments in LESS? I want same behaviour as //comment, but for multiline strings.

2
  • 1
    Did you try compress/minify using the -x option or clean-css?
    – Harry
    Sep 27, 2014 at 10:55
  • I would love to know if there is a silent multi-line comment as well
    – Alex W
    Sep 25, 2016 at 4:04

1 Answer 1

3

As already made clear by @harry the -x and clean-css options remove comments too. Since version 2 the clean-css option has been moved into a plugin (npm install -g less-plugin-clean-css).

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

Example:

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

Than 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());
    }
};

Your comment-processor.js than could 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;
};

And finally you should be able to run the following command:

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.