telling uglify to keep certain comments (using CodeKit)
Asked Answered
C

4

11

I'm using CodeKit to develop a WordPress theme. Of course I'd like to compress the LESS when it's compiled into CSS, but uglify.js removes all comments.

Does anyone know how to mark specific comments for preservation?

Edit: just wanted to add that after trying this in 2019 with CodeKit 3, the exclamation point trick works perfectly! (Allen Bargi's answer)

Contango answered 28/4, 2012 at 4:58 Comment(0)
I
13

There's convention to put an exclamation mark right after the comment, to preserve it after minifying. you should use something like this:

/*!
  this comment will not be removed by minifiers
 */

The above answer is not valid anymore! things evolve!

Now you should add either @preserve or @license to the comment as mentioned by @texelate below.

Isaac answered 28/4, 2012 at 17:30 Comment(5)
uglify doesn't respect that, not by default at leastHebdomadary
@allen, this is not the default!Gracious
This is wrong and shouldn't be the accepted answer. See my answer.Pelisse
Just to help any newbies, this means /*@preserve comment here */Clearway
Coming back to this in 2019, and in CodeKit 3, this convention works beautifully! I can compress EVERYTHING now. Adding a CSS map helps me work out issues in the inspector too.Contango
P
10

You need to add either @preserve or @license to the comments you want to keep. It doesn't honour /*!

Pelisse answered 20/3, 2015 at 11:17 Comment(0)
A
5

You can use this way: --comments '/foo|bar/' : will keep only comments that contain "foo" or "bar". See more : https://github.com/mishoo/UglifyJS2#keeping-copyright-notices-or-other-comments

Alsworth answered 23/10, 2013 at 8:34 Comment(0)
T
4

Half a year later, I hit the same issue and the exclamation mark trick did not "do the trick" for me. Neither any of the @preserve or @license options listed in uglify documentation. What did work is providing a regex on the commandline, e.g.:

uglifyjs file.js -c -m --comments '/^!|@(?:license|preserve)/' > file.min.js
Terminate answered 16/2, 2013 at 13:13 Comment(1)
to respect more styles, including /*!, I used --comments '/(^!|@license|@preserve)/'. Test: uglifyjs -c -m --comments '/(^!|@license|@preserve)/' <<< 'test(); /* regular */ /*! important */ /*@license foo */ noop(); /*bar @preserve*/ // regular'Dissimilitude

© 2022 - 2024 — McMap. All rights reserved.