Search code examples
javascriptexceptionminifyline-numbers

JavaScript minifier that outputs linefeeds instead of semicolons - Windows based


We want line-numbers to be useful when an exception occurs in the clientside JavaScript. Ideally a solution that understands the ASI (Automatic Semicolon Insertion) rules i.e. that is syntax aware and not RegExp/text based.

The closest I have found is UglifyJS2 with semicolon option set to false. [edit: UglifyJS2 also needs the maximum line length option set to a low value, and perhaps sequences option unticked, otherwise it would still generate long lines e.g. 1000s of characters long].

PS: We must use Windows for our build process (locked in for various reasons), and our build scripts already have dependencies on Python and Java (closurecompiler for JS, yuicompressor for CSS, cssembed for embedding images into CSS) and I really don't want to add another build process dependency (Node, Ruby, or a web service etc), and I strongly disfavour using a RegExp solution as I think text processing is too fragile for a reliable production solution.

PPS: I don't want to rely on sourcemaps because they are significantly more complex (cross-browser issues, internal and client support issues, difficulties managing and finding correct sourcemap).

PPPS: The reason for the new requirement for line numbers is that we are starting to use https://github.com/getsentry/raven-js and it will be most useful if line numbers for exceptions are useful. Our client side code is a single page Ajax/json app. In theory using a linefeed instead of a semicolon would change the gzip size very little.


Solution

  • UglifyJS2 was the only solution I found that worked OK with the following settings:

    • semicolon option set to false
    • maximum line length option set to a reasonably low value
    • sequences option unticked

    The line length choice involves a compromise: low line length is better for diagnosing where an exception occurred, but the gzip size increases by a small amount as you decrease the line length.