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)

rgb_life
  • 353
  • 3
  • 10

4 Answers4

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.

Allen Bargi
  • 14,674
  • 9
  • 59
  • 58
10

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

texelate
  • 2,460
  • 3
  • 24
  • 32
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

anemone928
  • 139
  • 1
  • 5
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
Walf
  • 8,535
  • 2
  • 44
  • 59
soquel
  • 89
  • 3
  • 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'` – kubi Apr 11 '19 at 13:26