Yii2: asset/compress needs semicolon-less JavaScripts support

Created on 5 Oct 2017  ·  11Comments  ·  Source: yiisoft/yii2

New released yii-pjax 2.0.7 breaks my compressed JavaScript which generated by asset/compress command. JS files are joined with no separators after jquery.pjax.js.

What steps will reproduce the problem?

Create an Yii project, then simply use yii asset/compress.

What is the expected result?

At least a semicolon should be inserted. I prefer wrapping each scripts by function(){ ... }();.

What do you get instead?

no-semicolon

Additional info

| Q | A
| ---------------- | ---
| Yii version | 2.0.12
| PHP version | 7.0.15, 7.1.8
| Operating system |Ubuntu, MacOS

ready for adoption enhancement

All 11 comments

I like semicolon but certain semicolon haters who can lead trends really exist -- GitHub, Twitter Bootstrap, Vue.js ...

Wrapping with (function(){ ... })(); will change code behavior - some scripts are intended to work in global scope.

@rob006 right! I will change my PR.

@samdark It will fix it for pjax, but any other library can have similar issues. Compressing assets should not depend on trailing semicolon.

Right. Makes sense to fix that. Would checking for trailing semicolon when concatenating files and adding it if it's not there do?

Sorry for being late to the party...

@samdark The semicolon does not hurt, but shouldn't a simple new-line be OK for this?
It's JavaScript 😏 http://www.bradoncode.com/blog/2015/08/26/javascript-semi-colon-insertion/

[edit] Hmm, not quite sure after reading this https://stackoverflow.com/a/1169596/291573

[edit2] A problematic use-case might be, that if you are passing the generated, concatenated code to another tool, it might complain about the additional semicolons.

@tanakahisateru I wonder why the comments are not visible in the screenshot above. Is the source from concatenating the files with Yii or is there another tool involved?

@schmunk42 I can't think of a case where it won't work as expected...

@samdark Can you reopen this issue? I found the case for trailing line comment, e.g:

(function($) {
})(jQuery) // trailing line comment 👻

... will be combined and compressed with the next file as:

/*** BEGIN FILE: 1st.js ***/
(function($) {
})(jQuery) // Trailing line comment 👻;
/*** END FILE: 1st.js  ***/
/*** BEGIN FILE: 2nd.js ***/
(function($) {
})(jQuery)
/*** END FILE: 2nd.js  ***/

$ node_modules/.bin/uglifyjs conbined.js 
(function($){})(jQuery)(function($){})(jQuery);

It needs a new line before inserted semicolon.

})(jQuery) // Trailing line comment 👻
;
/*** END FILE: 1st.js  ***/

Would you please create a pull request? Thanks.

Was this page helpful?
0 / 5 - 0 ratings