Use a minimal function declaration as the token string to replace preserved comment block #17
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When a preserved comment block is followed by an IIFE, the minified js doesn't pass syntax parsing, like closure compiler, because of missing brackets around the IIFE in minified js file.
For example:
The snippet above is a minimal version of a yui 2 module, when wrapped as a yui2in3 module, including YUI2 variable and version information at the bottom, minified version could be:
However, the minified result doesn't compile in closure compiler , neither does it in any browser.
Errors in closure compiler:
JSC_PARSE_ERROR: Parse error. syntax error at line 4 character 32
function(){console.log("test")}()},"@Version@");
^
JSC_PARSE_ERROR: Parse error. missing } after function body at line 4 character 47
function(){console.log("test")}()},"@Version@");
^
The missing brackets around the IIFE is the cause.
The deeper reason behind this is that yuglify uses a simple string as the token to replace the preserved comment, i.e.:
If preserved comment block is followed by an IIFE, after uglify minifies the js file, in which preserved comments has been replaced with tokens, it adds a comma between the token string and the IIFE and removes the surrounding brackets of the IIFE, because uglify joins consecutive simple statements into sequences using the “comma operator”. (if I understand correctly after a deep debugging.)
Therefore instead of using a simple string as the token and handles the inconsistency that sometimes it adds comma sometimes it doesn't, a minimal function could be a better alternative:
and the uglify result is consistent for all the cases. And of course it fixes the IIFE after preserved comment block:
It requires a minor fix of shifter's tests, which requires another PR yui/shifter#97.