Minify "strings" as if they were variables?

Jul 12, 2013 at 9:27 PM

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

var func = function(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?
Jul 13, 2013 at 6: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:
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) {
        return unwrap(this).get(key);
}(unwrap, validate);
That would minify just fine.
Jul 14, 2013 at 1: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!