Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

It is possible to compress LESS files without removing comments using lessc?

Tags:

less

I'm executing the following line to compress a less file:

lessc -x site.less site.css

And all my CSS blocks comment /*foo */ are removed.

It is possible to compress my less files and keep the comments?


Update: Currently SimpLESS do it as of version 1.4, the change log said:

if a LESS file starts with a CSS block comment, SimpLESS will keep that comment after minification (i.e. for Wordpress)

I would like to know if is it possible to do it with lessc command.

like image 765
Rubens Mariuzzo Avatar asked Aug 21 '13 23:08

Rubens Mariuzzo


Video Answer


2 Answers

If you want to keep comments in compiled LESS files, you should be using block comments for non-minified code:

site.css
/* this is a keeper */
.foo {
    ...
}

And for minified code you should use --yui-compress with /*! to start your comments:

site.min.css
/*! this is a keeper */
.foo{...}

This is because the lessc command with the --yui-compress flag pipes the CSS through YUI Compressor, and YUI Compressor allows comments when they begin with /*!.

like image 135
zzzzBov Avatar answered Oct 19 '22 10:10

zzzzBov


If you're using a relatively recent version of lessc then simply start the block comment with the /*! comment syntax. eg

/*! This is preserved */

Previously you needed to use --yui-compress to get this to work, it now works by default. The yui-compress option was removed a while ago (I haven't found the exact commit yet, but it was removed before November 2013).

This comment syntax also works with wordpress themes so you can preserve wordpress comment based meta data this way.

like image 27
benz001 Avatar answered Oct 19 '22 11:10

benz001