Bug report, ES5, Uglify version 3.0.3
This is working properly in version 2.x.
JavaScript input
test.js:
!function(){
var a;
eval( 'a=1' );
console.log( a );
}()
The uglifyjs CLI command executed or minify() options used.
uglifyjs test.js -c
JavaScript output
!function(){var a;eval("a=1"),console.log(void 0)}();
It appears that uglify thinks that variable a is not used, leading to an optimization that replaces it with void 0.
Workaround
Using compress option unused set to false:
uglifyjs test.js -c unused=false
Generates correct code:
!function(){var a;eval("a=1"),console.log(a)}();
I did not find which option to use with the API to workaround the problem. I have tried:
var output = ugly.minify( sources, { compress: { unused: false } } );
Suggested fix
Always set unused option within the scope of eval.
This is working properly in version 2.x.
2.8.23 has the same bug as 3.0.3.
I did test with version 2.8.22, and it was working properly.
$ node_modules/uglify-js/bin/uglifyjs -V
uglify-js 2.8.22
$ node_modules/uglify-js/bin/uglifyjs test.js -c
!function(){var a;eval("a=1"),console.log(void 0)}();
$ node_modules/uglify-js/bin/uglifyjs test.js -c | node
undefined
$ node test.js
1
To be more specific, I did not have the problem in version 2.8.22, using the low-level API. I noticed the problem while migrating to version 3.0.3 where I am now using minify().
@uiteoi @kzc thanks for the report and detailed investigation - fix is on the way.