tags:

views:

51

answers:

2

I'm using facelets, and I have a number of CSS files in webapp/styles/blueprint/*.css. They contain comments which I don't want to become visible to end-users. How can I remove them on-fly?

A: 

As Nick Craver said in the comments, you should, if at all, do that work as a part of your building process since css is a static resource. No runtime modifications needed.

Now depending on your build process, you could write a small script that simply strips out any comments, my first approach would be regular expressions:

cssFileContents = cssFileContents.replaceAll("/\*.*?\*/", ""); //with lazy quantifier on the "."!

In this case you need to make sure that the regex "."-metacharacter includes linebreaks.

Since css only allows block comments (/* ... */) and no line comments (// ... \n), this is the only thing you need to replace.

f1sh
Sounds good, thanks! But I thought that maybe there are some libraries exist, which automate the process and do some extra work. For example, optimizing of CSS and compressing it..
Vincenzo
+1  A: 

Use YUI compressor. It will not only remove comments, but also minify the CSS (and JS) files.

Reader reader = null;
Writer writer = null;

try {
    reader = new InputStreamReader(new FileInputStream(cssFile), "UTF-8");
    writer = new OutputStreamWriter(new FileOutputStream(minFile), "UTF-8");
    new CssCompressor(reader).compress(writer, -1); // That's it.
} finally {
    close(writer);
    close(reader);
}

See also

BalusC