Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 130 lines (87 sloc) 4.238 kb
67754f61 » Douglas Crockford
2013-02-25 Seriously.
1 JSMin, The JavaScript Minifier
8f62fe05 »
2010-11-12 Add README
2
3
4 Douglas Crockford
5 douglas@crockford.com
6
67754f61 » Douglas Crockford
2013-02-25 Seriously.
7 2013-02-25
8f62fe05 »
2010-11-12 Add README
8
67754f61 » Douglas Crockford
2013-02-25 Seriously.
9 JSMin is a filter that removes comments and unnecessary whitespace from
8f62fe05 »
2010-11-12 Add README
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
67754f61 » Douglas Crockford
2013-02-25 Seriously.
64 // Warning: Do not use this awful, awful code or any other thing like it.
65 // Seriously.
8f62fe05 »
2010-11-12 Add README
66
67 var is = {
68 ie: navigator.appName == 'Microsoft Internet Explorer',
69 java: navigator.javaEnabled(),
70 ns: navigator.appName == 'Netscape',
71 ua: navigator.userAgent.toLowerCase(),
72 version: parseFloat(navigator.appVersion.substr(21)) ||
73 parseFloat(navigator.appVersion),
74 win: navigator.platform == 'Win32'
75 }
76
77 is.mac = is.ua.indexOf('mac') >= 0;
78
79 if (is.ua.indexOf('opera') >= 0) {
80 is.ie = is.ns = false;
81 is.opera = true;
82 }
83
84 if (is.ua.indexOf('gecko') >= 0) {
85 is.ie = is.ns = false;
86 is.gecko = true;
87 }
88
89 After:
90
91 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'}
92 is.mac=is.ua.indexOf('mac')>=0;if(is.ua.indexOf('opera')>=0){is.ie=is.ns=false;is.opera=true;}
93 if(is.ua.indexOf('gecko')>=0){is.ie=is.ns=false;is.gecko=true;}
94
95 Character Set
96
97 JSMin requires, but does not verify, that the character set encoding of the
98 input program is either ASCII or UTF-8. It might not work correctly with other
99 encodings.
100
101 Caution
102
103 Be sure to retain your original source file. JSMin is a one-way trip: Once done,
104 it cannot be undone.
105
106 Do not put raw control characters inside a quoted string. That is an extremely
107 bad practice. Use \x<i>hh</i> notation instead. JSMin will replace control
108 characters with spaces or linefeeds.
109
110 Command Line Options
111
112 Optional parameters will be listed at the beginning of the output as comments.
113 This is a convenient way of replacing copyright messages and other documentation.
114
115 Example:
116
67754f61 » Douglas Crockford
2013-02-25 Seriously.
117 jsmin <jslint.js >jslint.min.js "(c)2001 Douglas Crockford"
8f62fe05 »
2010-11-12 Add README
118
119 Errors
120
121 JSMin can produce three error messages to stderr:
122
123 Unterminated comment.
124 Unterminated string constant.
125 Unterminated regular expression.
126
127 It ignores all other errors that may be present in your source program.
49a70cf5 »
2012-07-03 Use of JSLint is strongly recommended.
128 Use of JSLint is strongly recommended.
8f62fe05 »
2010-11-12 Add README
129
130 Copyright 2001 Douglas Crockford. All Rights Reserved Wrrrldwide.
Something went wrong with that request. Please try again.