I understand that minifying tries to reduce file size by removing whitespace characters. Sometimes things end up on one line, but more often than not, there are multiple lines. I noticed that line breaks happen wherever spaces occur. Wouldn't it be better just to have it all on one line?
-
possible duplicate of [Why do we have newlines in minified JavaScript?](http://stackoverflow.com/questions/10193851/why-do-we-have-newlines-in-minified-javascript) – Matt Feb 27 '13 at 21:37
3 Answers
Well with JavaScript, a line break can imply the end of a statement (in place of the ;
). So putting it all on one line could cause issues with the code functioning correctly.
I would guess that it depends on the minimizer you are using, but that could be one reason. I would think the minimizer would try and account for this and put in semicolons where it can, but this ability could vary wildly between them.

- 113,795
- 27
- 197
- 251
-
if the line break is the replacement of semicolon ";", so a file with a thousand line breaks and a file with a thousand semicolons have the same size, right? – T H Apr 06 '22 at 22:23
If you are using yuicompressor-maven-plugin, you may face this issue. The YUI compressor plugin has an option linebreakpos for which the default value is zero. If you read the description about how this option is used by the original YUI compressor library at this page, you'll notice that a value of zero for this value means that a newline will be emitted after CSS rule or a semi-colon in javascript.
Fortunately, having a look at the implementation suggests an easy work-around. Just specify any value less than 0 and you should be good to go.

- 3,669
- 3
- 20
- 37
Given the lack of sample code and information about how the minification is being performed, my best guess would be: Whatever you are using to view the code is performing word wrapping and there aren't any real new lines in there.

- 914,110
- 126
- 1,211
- 1,335
-
One error i've had with dreamweaver a few years back was it would truncate lines that were too long (not even wrap) and if you tried to save it your document would get damaged. – Dan Heberden Jun 25 '10 at 14:30