0

All our webservices are passed to our XHR via an associative array. IE:

new XHR({"year": 2015, "week": 6}, ...);

All our scripts are added to a single file, and this is then Uglified via (Essentially mangle and compress everything except for our entry point):

uglifyjs --mangle toplevel --screw-ie8 --compress --mangle-regex="/(?s)^((?!$$).)*$/" --reserve-domprops --reserved-file reserved -- input

The problem is, the year and week field keep getting obfuscated. So I tried putting a $$ infront, so we now have $$year and $$week. To get this to work, I added the regex argument to the UglifyJS command above. I am trying to negate the lookup, so it will mangle all properties except those with a $$ at the start (Can be any set of characters, just using $$ as a starting point).

My questions:

  1. Is this the best way to do it? Is there a way in UglifyJS to ignore certain associative arrays?
  2. Is my regex right? I've tried escaping the two dollar signs, I've tried with __ instead of $$ as well as several other attempts (Note: My regex is not good)
  3. If the regex is wrong, is there somewhere that would detail a method for creating a valid regex?

EDIT: Resolved. Needed to use this:

uglifyjs --mangle toplevel --screw-ie8 --mangle-props --mangle-regex="/^((?!\$\$).)*$/" --reserve-domprops --reserved-file reserved -- input > output
JosephGarrone
  • 4,081
  • 3
  • 38
  • 61
  • It could be that `--reserve-domprops` is implicitly turning on `--mangle-props`. Try taking that out. Also, what is the `(?s)` at the beginning of your regexp? –  Sep 30 '15 at 03:54
  • the entire regex I used was from http://stackoverflow.com/questions/406230/regular-expression-to-match-line-that-doesnt-contain-a-word – JosephGarrone Sep 30 '15 at 04:18
  • With regard to your edit, glad it worked, but it is still a mystery why property names were getting mangled in the first place. That should not happen in the absence of `--mangle-props`. I cannot reproduce this on my machine. Are you sure `uglifyjs` is referring to the node executable itself and not some script which is adding options? –  Sep 30 '15 at 04:55
  • I'm on a windows box with npm installed. And in my path I've got the default uglify js path that was added. The --mangle-regex I think was not working at all maybe. – JosephGarrone Sep 30 '15 at 05:23

1 Answers1

0

I ran UglifyJs v2.4.10 with these arguments:

uglifyjs src.js --mangle toplevel --screw-ie8 --compress --reserve-domprops --mangle-regex="/(?s)^((?!$$).)*$/"

And it converted this:

(function(lib){
    var xhr = new XHR({"year": 2015, "week": 6});
    xhr.run();
})(library);

to this:

!function(r){var e=new XHR({year:2015,week:6});e.run()}(library);

I am unable to get the other options to work properly.

Which version are you using?

Cameron
  • 1,524
  • 11
  • 21