Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add README

  • Loading branch information...
commit 8f62fe05856935ddcd49e364502ed98c4cf555b8 1 parent 8879b8c
Douglas Crockford authored

Showing 1 changed file with 152 additions and 0 deletions. Show diff stats Hide diff stats

  1. +152 0 README
152 README
... ... @@ -0,0 +1,152 @@
  1 +JSMIN, The JavaScript Minifier
  2 +
  3 +
  4 +Douglas Crockford
  5 +douglas@crockford.com
  6 +
  7 +2003-12-04
  8 +
  9 +JSMin is a filter which removes comments and unnecessary whitespace from
  10 +JavaScript files. It typically reduces filesize by half, resulting in faster
  11 +downloads. It also encourages a more expressive programming style because it
  12 +eliminates the download cost of clean, literate self-documentation.
  13 +
  14 +What JSMin Does
  15 +
  16 +JSMin is a filter that omits or modifies some characters. This does not change
  17 +the behavior of the program that it is minifying. The result may be harder to
  18 +debug. It will definitely be harder to read.
  19 +
  20 +JSMin first replaces carriage returns ('\r') with linefeeds ('\n'). It replaces
  21 +all other control characters (including tab) with spaces. It replaces comments
  22 +in the // form with linefeeds. It replaces comments in the /* */ form with
  23 +spaces. All runs of spaces are replaced with a single space. All runs of
  24 +linefeeds are replaced with a single linefeed.
  25 +
  26 +It omits spaces except when a space is preceded and followed by a non-ASCII
  27 +character or by an ASCII letter or digit, or by one of these characters:
  28 +
  29 + \ $ _
  30 +
  31 +It is more conservative in omitting linefeeds, because linefeeds are sometimes
  32 +treated as semicolons. A linefeed is not omitted if it precedes a non-ASCII
  33 +character or an ASCII letter or digit or one of these characters:
  34 +
  35 + \ $ _ { [ ( + -
  36 +
  37 +and if it follows a non-ASCII character or an ASCII letter or digit or one of
  38 +these characters:
  39 +
  40 + \ $ _ } ] ) + - " '
  41 +
  42 +No other characters are omitted or modified.
  43 +
  44 +JSMin knows to not modify quoted strings and regular expression literals.
  45 +
  46 +JSMin does not obfuscate, but it does uglify.
  47 +
  48 +Before:
  49 +
  50 +// is.js
  51 +
  52 +// (c) 2001 Douglas Crockford
  53 +// 2001 June 3
  54 +
  55 +
  56 +// is
  57 +
  58 +// The -is- object is used to identify the browser. Every browser edition
  59 +// identifies itself, but there is no standard way of doing it, and some of
  60 +// the identification is deceptive. This is because the authors of web
  61 +// browsers are liars. For example, Microsoft's IE browsers claim to be
  62 +// Mozilla 4. Netscape 6 claims to be version 5.
  63 +
  64 +// Warning: Do not use this awful, awful code.
  65 +
  66 +var is = {
  67 + ie: navigator.appName == 'Microsoft Internet Explorer',
  68 + java: navigator.javaEnabled(),
  69 + ns: navigator.appName == 'Netscape',
  70 + ua: navigator.userAgent.toLowerCase(),
  71 + version: parseFloat(navigator.appVersion.substr(21)) ||
  72 + parseFloat(navigator.appVersion),
  73 + win: navigator.platform == 'Win32'
  74 +}
  75 +
  76 +is.mac = is.ua.indexOf('mac') >= 0;
  77 +
  78 +if (is.ua.indexOf('opera') >= 0) {
  79 + is.ie = is.ns = false;
  80 + is.opera = true;
  81 +}
  82 +
  83 +if (is.ua.indexOf('gecko') >= 0) {
  84 + is.ie = is.ns = false;
  85 + is.gecko = true;
  86 +}
  87 +
  88 +After:
  89 +
  90 +var is={ie:navigator.appName=='Microsoft Internet Explorer',java:navigator.javaEnabled(),ns:navigator.appName=='Netscape',ua:navigator.userAgent.toLowerCase(),version:parseFloat(navigator.appVersion.substr(21))||parseFloat(navigator.appVersion),win:navigator.platform=='Win32'}
  91 +is.mac=is.ua.indexOf('mac')>=0;if(is.ua.indexOf('opera')>=0){is.ie=is.ns=false;is.opera=true;}
  92 +if(is.ua.indexOf('gecko')>=0){is.ie=is.ns=false;is.gecko=true;}
  93 +
  94 +Character Set
  95 +
  96 +JSMin requires, but does not verify, that the character set encoding of the
  97 +input program is either ASCII or UTF-8. It might not work correctly with other
  98 +encodings.
  99 +
  100 +Caution
  101 +
  102 +Be sure to retain your original source file. JSMin is a one-way trip: Once done,
  103 +it cannot be undone.
  104 +
  105 +Do not put raw control characters inside a quoted string. That is an extremely
  106 +bad practice. Use \x<i>hh</i> notation instead. JSMin will replace control
  107 +characters with spaces or linefeeds.
  108 +
  109 +Use parens with confusing sequences of + or -. For example, minification changes
  110 +
  111 + a + ++b
  112 +
  113 +into
  114 +
  115 + a+++b
  116 +
  117 +which is interpreted as
  118 +
  119 + a++ + b
  120 +
  121 +which is wrong. You can avoid this by using parens:
  122 +
  123 + a + (++b)
  124 +
  125 +JSLint checks for all of these problems. It is suggested that JSLint be used
  126 +before using JSMin.
  127 +
  128 +Command Line Options
  129 +
  130 +Optional parameters will be listed at the beginning of the output as comments.
  131 +This is a convenient way of replacing copyright messages and other documentation.
  132 +
  133 +Example:
  134 +
  135 + jsmin <fulljslint.js >jslint.js "(c)2002 Douglas Crockford"
  136 +
  137 +Errors
  138 +
  139 +JSMin can produce three error messages to stderr:
  140 +
  141 +Unterminated comment.
  142 +Unterminated string constant.
  143 +Unterminated regular expression.
  144 +
  145 +It ignores all other errors that may be present in your source program.
  146 +
  147 +Get Minified
  148 +
  149 +You can get a zip file containing an MS-DOS.exe file, or you can get the
  150 +C source code and build it yourself.
  151 +
  152 +Copyright 2001 Douglas Crockford. All Rights Reserved Wrrrldwide.

0 comments on commit 8f62fe0

Please sign in to comment.
Something went wrong with that request. Please try again.