dead-code=true not removing unused function
Maybe I don't understand what -c dead-code=true is supposed to do so can someone please clarify? I am assuming uglifyjs2 is supposed to remove this code under the condition dead-code=true:
var DEBUG = false;
if(DEBUG) {
var create_some_function = function () { ... };
}
In my case it doesn't remove this code. Am I doing something wrong?
My shell script looks like this:
uglifyjs2 $parentdir/jquip/src/jquip.js \
$parentdir/jquip/src/jquip.events.js \
$parentdir/vars.js \
$parentdir/embed.js \
--define DEBUG=false \
--comments \
-c dead-code=true \
-m \
-o $parentdir/embed.min.js \
--source-map $parentdir/embed.map.json \
--v
Any help would be much appreciated.
Well, it's not entirely without side effects. Your code is equivalent to
var DEBUG, create_some_function;
DEBUG = false;
if(DEBUG) {
create_some_function = function(){ ... };
}
So, as you can see, only the initialisation value could have been removed.
Thanks Michael.
I use Google closure compiler & I am looking into UglifyJS2 because some of the libraries we need to compile can't run through Google closure compiler in ADVANCED mode. One thing closure compiler does which I think is amazing is it has the ability to remove code that doesn't do anything. For example closure compiler would remove all of this code if run in advanced mode:
var DEBUG = false;
var log = function (str) {
if (DEBUG) {
// log is a cross browser console logging module
}
};
// deep inside code all log() functions are removed because they don't do anything
log ('here is a log');
Does UglifyJS2 give you a way to remove all debug type code, not just console.log, during compilation when using -c? Currently when I compile all of my log() functions remain.
Thanks for reading.
I don't think you're supposed to define the --define'd variables in the source code itself.
@curiousdannii is correct — the reason why it doesn't drop code inside the if in the original ticket is because there is var DEBUG in the code. Take that out, and it should remove the function itself (though it should keep var create_some_function (as @michaelficarra points out).
At this time UglifyJS2 does not detect whether a function is a no-op to discard calls to it. I can imagine how that would help coupled with --define.
This seems to be related, so I wanted to ask about expected behaviour in certain situations. These are all done on aebafad41eab48f43ed649ce8c77e8f1528b50da.
:+1: Empty IIFE, works as intended:
$ echo '(function(){}())' | bin/uglifyjs - -c
WARN: Dropping side-effect-free statement [-:1,0]
:+1: Empty IIFE with unused argument, works as intended:
$ echo '(function(a){}())' | bin/uglifyjs - -c
WARN: Dropping unused function argument a [-:1,10]
WARN: Dropping side-effect-free statement [-:1,0]
:question: Empty IIFE taking an argument, not as I expected. I would want this to also compress down to nothing:
$ echo '(function(){}(a))' | bin/uglifyjs - -c
(function(){})(a);
:+1: Defined empty IIFE, works as intended:
$ echo '(function(){ assert(); }())' | bin/uglifyjs - -c --define assert='(function(){})'
WARN: Dropping side-effect-free statement [-:1,13]
WARN: Dropping side-effect-free statement [-:1,0]
:question: Similar to above, defined empty IIFE taking an argument, not as I expected:
$ echo '(function(){ assert(a); }())' | bin/uglifyjs - -c --define assert='(function(){})'
(function(){(function(){})(a)})();
:exclamation: Maybe this is too much to handle, but an interesting case:
$ echo '(function(){ assert(a); }())' | bin/uglifyjs - -c --define assert='(function(a){})'
/path/UglifyJS2/lib/scope.js:323
return this.definition().references.length == 0
^
TypeError: Cannot read property 'references' of undefined
at AST_Node.unreferenced (/path/UglifyJS2/lib/scope.js:323:29)
at Object.before (/path/UglifyJS2/lib/compress.js:942:37)
at AST_Node.transform (/path/UglifyJS2/lib/transform.js:62:35)
at AST_Node.drop_unused (/path/UglifyJS2/lib/compress.js:1046:18)
at Object.merge.before (/path/UglifyJS2/lib/compress.js:85:18)
at AST_Node.transform (/path/UglifyJS2/lib/transform.js:62:35)
at make_node_from_constant (/path/UglifyJS2/lib/compress.js:148:49)
at /path/UglifyJS2/lib/compress.js:1826:24
at AST_Node.optimize (/path/UglifyJS2/lib/compress.js:112:23)
at Object.merge.before (/path/UglifyJS2/lib/compress.js:89:21)
:exclamation: And finally, what I hope to be able to do. Though the error is different, I suspect the root cause is probably related to the previous example:
$ echo '(function(){ assert(a); }());' | bin/uglifyjs - -c --define assert='(function(assertion){if(!assertion)throw new Error})'
/path/UglifyJS2/lib/compress.js:923
in_use[i].orig.forEach(function(decl){
^
TypeError: Cannot read property 'orig' of undefined
at AST_Node.drop_unused (/path/UglifyJS2/lib/compress.js:923:26)
at Object.merge.before (/path/UglifyJS2/lib/compress.js:85:18)
at AST_Node.transform (/path/UglifyJS2/lib/transform.js:62:35)
at make_node_from_constant (/path/UglifyJS2/lib/compress.js:148:49)
at /path/UglifyJS2/lib/compress.js:1826:24
at AST_Node.optimize (/path/UglifyJS2/lib/compress.js:112:23)
at Object.merge.before (/path/UglifyJS2/lib/compress.js:89:21)
at AST_Node.transform (/path/UglifyJS2/lib/transform.js:62:35)
at /path/UglifyJS2/lib/transform.js:173:43
at Object.merge.before (/path/UglifyJS2/lib/compress.js:88:9)
I just realized that you'd need to verify that what's passed into an empty IIFE doesn't have any side effects, (function(){})(a) versus (function(){})(a.mutatorProperty). Well, that's a bummer.
@kzc I think we might have fixed this with #1469 (see https://github.com/mishoo/UglifyJS2/pull/1469#issuecomment-278231528)
I think you're right.