• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

READMEH A D29-Mar-20134.1 KiB13187

jsmin.cH A D29-Mar-20137.9 KiB307231

README

1JSMin, The JavaScript Minifier
2
3
4Douglas Crockford
5douglas@crockford.com
6
72013-02-25
8
9JSMin is a filter that removes comments and unnecessary whitespace from
10JavaScript files. It typically reduces filesize by half, resulting in faster
11downloads. It also encourages a more expressive programming style because it
12eliminates the download cost of clean, literate self-documentation.
13
14What JSMin Does
15
16JSMin is a filter that omits or modifies some characters. This does not change
17the behavior of the program that it is minifying. The result may be harder to
18debug. It will definitely be harder to read.
19
20JSMin first replaces carriage returns ('\r') with linefeeds ('\n'). It replaces
21all other control characters (including tab) with spaces. It replaces comments
22in the // form with linefeeds. It replaces comments in the /* */ form with
23spaces. All runs of spaces are replaced with a single space. All runs of
24linefeeds are replaced with a single linefeed.
25
26It omits spaces except when a space is preceded and followed by a non-ASCII
27character or by an ASCII letter or digit, or by one of these characters:
28
29    \ $ _
30
31It is more conservative in omitting linefeeds, because linefeeds are sometimes
32treated as semicolons. A linefeed is not omitted if it precedes a non-ASCII
33character or an ASCII letter or digit or one of these characters:
34
35    \ $ _ { [ ( + -
36
37and if it follows a non-ASCII character or an ASCII letter or digit or one of
38these characters:
39
40    \ $ _ } ] ) + - " '
41
42No other characters are omitted or modified.
43
44JSMin knows to not modify quoted strings and regular expression literals.
45
46JSMin does not obfuscate, but it does uglify.
47
48Before:
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 or any other thing like it.
65// Seriously.
66
67var 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
77is.mac = is.ua.indexOf('mac') >= 0;
78
79if (is.ua.indexOf('opera') >= 0) {
80    is.ie = is.ns = false;
81    is.opera = true;
82}
83
84if (is.ua.indexOf('gecko') >= 0) {
85    is.ie = is.ns = false;
86    is.gecko = true;
87}
88
89After:
90
91var 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'}
92is.mac=is.ua.indexOf('mac')>=0;if(is.ua.indexOf('opera')>=0){is.ie=is.ns=false;is.opera=true;}
93if(is.ua.indexOf('gecko')>=0){is.ie=is.ns=false;is.gecko=true;}
94
95Character Set
96
97JSMin requires, but does not verify, that the character set encoding of the
98input program is either ASCII or UTF-8. It might not work correctly with other
99encodings.
100
101Caution
102
103Be sure to retain your original source file. JSMin is a one-way trip: Once done,
104it cannot be undone.
105
106Do not put raw control characters inside a quoted string. That is an extremely
107bad practice. Use \x<i>hh</i> notation instead. JSMin will replace control
108characters with spaces or linefeeds.
109
110Command Line Options
111
112Optional parameters will be listed at the beginning of the output as comments.
113This is a convenient way of replacing copyright messages and other documentation.
114
115Example:
116
117  jsmin <jslint.js >jslint.min.js "(c)2001 Douglas Crockford"
118
119Errors
120
121JSMin can produce three error messages to stderr:
122
123Unterminated comment.
124Unterminated string constant.
125Unterminated regular expression.
126
127It ignores all other errors that may be present in your source program.
128Use of JSLint is strongly recommended.
129
130Copyright 2001 Douglas Crockford. All Rights Reserved Wrrrldwide.
131