Minify "strings" as if they were variables?

Jul 12, 2013 at 8:27 PM
Hi,

I have some code similar to the following
var validate = function(k){...}
var unwrap = function(t){...}

var func = function(key){
  validate(key);
  return unwrap(this).get(key);
}

var del = new Function("unwrap","validate", "return "+func)(unwrap, validate);
When the above gets minimized, both unwrap and validate get renamed, but not the strings that will represents those values on the wrapped function (rendering the del method unusable).

Is there a way I can markup those strings so they get renamed with the same name as their "variable name" counterparts?
Coordinator
Jul 13, 2013 at 5:28 AM
Unfortunately not -- the Function constructor is essentially an eval statement, and the content of those strings are determined at runtime (the body of your function being a prime example). The best you could do is mark those names that break as no rename so AjaxMin leaves them alone and keeps everything working:
-norename:unwrap,validate
That said, why do you feel you need to use the Function constructor in the first place? Why not ditch the eval altogether and do something like:
var del = function(unwrap, validate) {
    return function(key) {
        validate(key);
        return unwrap(this).get(key);
    }
}(unwrap, validate);
That would minify just fine.
Jul 14, 2013 at 12:08 AM
That's part of a Weakmap implementation. It's not my code but I believe it has to be related to not retaining a "hard" link between the function and the code so it can be GC'd.

I will try what you suggest, but I'm using ajaxmin as part of the minification bundle and I don't know if I can't pass parameters to the minification engine...

Anyways, thank you!