mrclay / minify

Combines. minifies, and serves CSS or Javascript files
BSD 3-Clause "New" or "Revised" License
3.01k stars 472 forks source link

How to remove comments from css #640

Open ProgrammerNomad opened 6 years ago

ProgrammerNomad commented 6 years ago

Hello

All is working fine,

only i am want to remove/hide all css comments from minify output so please help me.

Thanks

AgentSmith0 commented 2 years ago

I would also like to know how to do this.

sedimentation-fault commented 5 months ago

So you want to only strip comments off your CSS...read on!

How to strip comments only from CSS with minify

This is not something that is supported out-of-the-box, but if you are willing to do some really tiny hacking, you will get it working! This is what you must do:

I suppose you have the source code somewhere, let's say it is in a directory called minify. Open minify/lib/Minify/CSS/Compressor.php and replace the whole function

protected function _process($css)
{
...
}

with this one

protected function _process($css)
{
    // apply callback to all valid comments (and strip out surrounding ws)
    $pattern = '@\\s*/\\*([\\s\\S]*?)\\*/\\s*@';
    $css = preg_replace_callback($pattern, array(get_called_class(), '_commentCB'), $css);

    return trim($css);
}

That's it! Have fun stripping comments only off your CSS. :smiley: