[aklug] Re: Removing newlines from minified javascript

From: Michael Fowler <michael@shoebox.net>
Date: Wed Aug 08 2012 - 09:49:08 AKDT

On Tue, Aug 07, 2012 at 10:32:43AM -0800, Tim Johnson wrote:
> It follows therefore, that if one first determines that the javascript source
> file is given the okay from jslint, then it should be safe to remove newlines or replace them with spaces.
> 1)Am I correct?
> 2)Will removing newlines really speed up performance all that much?

Assuming you're correct in that semicolons have been added between all
statements, then blind removing all newlines will not generate syntax
errors.

However, it may generate logical errors. Quoted strings in the source
may have embedded newlines, and removing them will change the meaning of
the code. You would need a parser that understands the actual
JavaScript to remove the newlines (or, build a syntax tree, then
essentially compile to a newline-free chunk of code). Thus, this is
best done in the minifier itself, because it (presumably) understands
JavaScript.

Given weird enough quoting rules, a lack of syntax errors is no
guarantee, either. I'm not sure what JavaScript's quoting rules are; if
it allows backslash to escape a newline, then removing the newline will
leave the backslash to escape something else. That may generate a
syntax error.

You may be able to fake it with something that understands JavaScript's
quoting rules (e.g. escape with \, quote with ", etc.), but that will
most likely lead to a false negative or a false positive at some point.

The best approach is to patch jsmin itself.

--
Michael Fowler
www.shoebox.net
---------
To unsubscribe, send email to <aklug-request@aklug.org>
with 'unsubscribe' in the message body.
Received on Wed Aug 8 09:49:32 2012

This archive was generated by hypermail 2.1.8 : Wed Aug 08 2012 - 09:49:32 AKDT