7

Is there a way to call the UglifyJS2 API in a node script (ie by calling require('uglify-js').minify) on a string of code such that it removes dead/unreachable code but does not apply any compression

For example:

var foo = 'bar';
if (false) {
    foo = 'yo';
}
alert('Foo value found');
alert(foo);

Would become

var foo = 'bar';
alert('Foo value found');
alert(foo);
JoeR
  • 1,861
  • 3
  • 24
  • 38
  • That doesn't looks safe...not even close. I don't think there is a tool for that. What if, for instance, the same piece of code is used by some other module/file outside the same file? – x80486 Dec 18 '15 at 18:09
  • 1
    @ɐuıɥɔɐɯ It's a pretty safe and standard thing to do - the content of the `if (false)` block will never be evaluated so it can be removed. Closure compiler etc does this and it's a feature of UglifyJS2. The use for this is pretty niche but to elaborate it's a build script which takes a source which is out of my control and does some modifications which renders some parts of the script 'dead'. These parts need to be removed because they interfere with another part of the build process. As I said - niche use case where I know it to be a safe/necessary operation. – JoeR Dec 18 '15 at 18:22
  • Well, I take that back. I tested and it's doing that...anyway, form my point of view it doesn't looks quite safe, although there are some instances that indeed are pretty much easy to figure it out. I just set `compress: { dead_code: true, unused: true }` in `options` – x80486 Dec 18 '15 at 18:51
  • @ɐuıɥɔɐɯ yep. Now I just want to disable it doing anything else to my source. Ideally without having to enumerate all the compress options and set them to false – JoeR Dec 18 '15 at 18:54

1 Answers1

1

Very late answer, but compress: {defaults: false, dead_code: true, unused: true} will work in Terser. See docs.

dmnd
  • 2,280
  • 2
  • 21
  • 28