Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A simple regex for minifying/compressing javascript is unlikely to exist anywhere. There are probably several good reasons for this, but here are a couple of these reasons:</p> <p><strong>Line breaks and semicolons</strong> Good javascript minifiers remove all extra line breaks, but because javascript engines will work without semicolons at the end of each statement, a minifier could easily break this code unless it is sophisticated enough to watch for and handle different coding styles.</p> <p><strong>Dynamic Language Constructs</strong> Many of the good javascript minifiers available will also change the names of your variables and functions to minify the code. For instance, a function named 'strip_white_space' that is called 12 times in your file might be renamed simple 'a', for a savings of 192 characters in your minified code. Unless your file has <em>a lot</em> of comments and/or whitespace, optimizations like these are where the majority of your filesize savings will come from.</p> <p>Unfortunately, this is much more complicated than a simple regex <em>should</em> try to handle. Say you do something as simple as:</p> <pre><code>var length = 12, height = 15; // other code that uses these length and height values var arr = [1, 2, 3, 4]; for (i = (arr.length - 1); i &gt;= 0; --i) { //loop code } </code></pre> <p>This is all valid code. BUT, how does the minifier know what to replace? The first "length" has "var" before it (but it doesn't have to), but "height" just has a comma before it. And if the minifier is smart enough to replace the first "length" properly, how smart does it have to be know NOT to change the word "length" when used as a property of the array? It would get even more complicated if you defined a javascript object where you specifically defined a "length" property and referred to it with the same dot-notation.</p> <p><strong>Non-regex Options</strong> Several projects exist to solve this problem using more complex solutions than just a simple regex, but many of them don't make any attempt to change variable names, so I still stick with <a href="http://dean.edwards.name/packer/" rel="nofollow noreferrer">Dean Edwards' packer</a> or <a href="http://www.crockford.com/javascript/jsmin.html" rel="nofollow noreferrer">Douglas Crockford's JSMin</a> or something like the <a href="http://yui.github.io/yuicompressor/" rel="nofollow noreferrer">YUI Compressor</a>.</p> <p><a href="https://github.com/rgrove/jsmin-php/blob/master/jsmin.php" rel="nofollow noreferrer">PHP implementation of Douglas Crockford's JSMin</a></p> <p><a href="https://github.com/mrclay/minify" rel="nofollow noreferrer">https://github.com/mrclay/minify</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload