| 1 |
.. copyright: |
| 2 |
Copyright 2011 - 2025 |
| 3 |
André Malo or his licensors, as applicable |
| 4 |
|
| 5 |
.. license: |
| 6 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
you may not use this file except in compliance with the License. |
| 8 |
You may obtain a copy of the License at |
| 9 |
|
| 10 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
|
| 12 |
Unless required by applicable law or agreed to in writing, software |
| 13 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
See the License for the specific language governing permissions and |
| 16 |
limitations under the License. |
| 17 |
|
| 18 |
|
| 19 |
============================== |
| 20 |
rJSmin - Javascript Minifier |
| 21 |
============================== |
| 22 |
|
| 23 |
|**rJSmin**| is a javascript minifier written in python. |
| 24 |
|
| 25 |
.. xxcontents:: |
| 26 |
:local: |
| 27 |
|
| 28 |
|
| 29 |
The minifier is based on the semantics of `jsmin.c by Douglas Crockford`_\. |
| 30 |
|
| 31 |
The module is a re-implementation aiming for :doc:`speed <benchmark>`, so it |
| 32 |
can be used at runtime (rather than during a preprocessing step). Usually it |
| 33 |
produces the same results as the original ``jsmin.c``. It differs in the |
| 34 |
following ways: |
| 35 |
|
| 36 |
- there is no error detection: unterminated string, regex and comment |
| 37 |
literals are treated as regular javascript code and minified as such. |
| 38 |
- Control characters inside string and regex literals are left untouched; they |
| 39 |
are not converted to spaces (nor to \\n) |
| 40 |
- Newline characters are not allowed inside string and regex literals, except |
| 41 |
for line continuations in string literals (ECMA-5). |
| 42 |
- "``return /regex/``" is recognized correctly. |
| 43 |
- Line terminators after regex literals are handled more sensibly. |
| 44 |
- More characters are allowed before regexes |
| 45 |
- "``+ +``" and "``- -``" sequences are not collapsed to '``++``' or |
| 46 |
'``--``' |
| 47 |
- Newlines before ``!`` operators are removed more sensibly |
| 48 |
- (Unnested) template literals are supported |
| 49 |
- Comments starting with an exclamation mark ('``!``') can be kept optionally |
| 50 |
- |rJSmin| does not handle streams, but only complete strings. (However, the |
| 51 |
module provides a "streamy" interface). |
| 52 |
|
| 53 |
Since most parts of the logic are handled by the regex engine it's way |
| 54 |
faster than the original python port of ``jsmin.c`` by Baruch Even. The speed |
| 55 |
factor varies between about 6 and 55 depending on input and python version |
| 56 |
(it gets faster the more compressed the input already is). Compared to the |
| 57 |
speed-refactored python port by Dave St.Germain the performance gain is less |
| 58 |
dramatic but still between 3 and 50 (for huge inputs). |
| 59 |
|
| 60 |
|rjsmin| comes with an optional C-reimplementation of the python/regex |
| 61 |
implementation, which speeds up the minifying process even more. |
| 62 |
|
| 63 |
Supported python versions are 2.7 and 3.6+. |
| 64 |
|
| 65 |
.. _jsmin.c by Douglas Crockford: http://www.crockford.com/javascript/jsmin.c |
| 66 |
|
| 67 |
|
| 68 |
Development Status |
| 69 |
~~~~~~~~~~~~~~~~~~ |
| 70 |
|
| 71 |
|rjsmin| is stable. |
| 72 |
|
| 73 |
|
| 74 |
Documentation |
| 75 |
~~~~~~~~~~~~~ |
| 76 |
|
| 77 |
The module provides a simple function (generated apidoc below), called |
| 78 |
``jsmin`` which takes the script as a string and returns the minified |
| 79 |
script as a string. |
| 80 |
|
| 81 |
The module additionally provides a "streamy" interface similar to the |
| 82 |
one jsmin.c provides: |
| 83 |
|
| 84 |
.. sourcecode:: console |
| 85 |
|
| 86 |
$ python -mrjsmin <script >minified |
| 87 |
|
| 88 |
This command line interface takes two options: |
| 89 |
|
| 90 |
-b Keep bang-comments (Comments starting with an exclamation mark) |
| 91 |
-p Force using the python implementation (not the C implementation) |
| 92 |
|
| 93 |
|
| 94 |
API Docs |
| 95 |
-------- |
| 96 |
|
| 97 |
.. autofunction:: rjsmin.jsmin |
| 98 |
|
| 99 |
|
| 100 |
License |
| 101 |
~~~~~~~ |
| 102 |
|
| 103 |
|rjsmin| is available under the terms and conditions of the "Apache License, |
| 104 |
Version 2.0." You'll find the detailed licensing terms in the root |
| 105 |
directory of the source distribution package or online at |
| 106 |
`http://www.apache.org/licenses/LICENSE-2.0 |
| 107 |
<http://www.apache.org/licenses/LICENSE-2.0>`_. |
| 108 |
|
| 109 |
|
| 110 |
.. placeholder: Download |
| 111 |
|
| 112 |
|
| 113 |
Bugs |
| 114 |
~~~~ |
| 115 |
|
| 116 |
No bugs, of course. ;-) |
| 117 |
But if you've found one or have an idea how to improve |rjsmin|, feel free |
| 118 |
to send a pull request on `github <https://github.com/ndparker/rjsmin>`_ |
| 119 |
or send a mail to <[email protected]>. |
| 120 |
|
| 121 |
|
| 122 |
Author Information |
| 123 |
~~~~~~~~~~~~~~~~~~ |
| 124 |
|
| 125 |
|rjsmin| was written and is maintained by André Malo. |
| 126 |
|
| 127 |
|
| 128 |
Acknowledgements |
| 129 |
---------------- |
| 130 |
|
| 131 |
- `Douglas Crockford <http://www.crockford.com/>`_ |
| 132 |
- `Jeffrey Friedl <http://regex.info/>`_ |
| 133 |
|
| 134 |
|
| 135 |
Trivia / Fun |
| 136 |
~~~~~~~~~~~~ |
| 137 |
|
| 138 |
|rJSmin| is a single pass substitution using a simple regular expression, |
| 139 |
which looks like this: |
| 140 |
|
| 141 |
.. sourcecode:: text |
| 142 |
|
| 143 |
pattern = ( |
| 144 |
r'([^\047"\140/\000-\040]+)|((?:(?:\047[^\047\\\r\n]*(?:\\(?:[^' |
| 145 |
r'\r\n]|\r?\n|\r)[^\047\\\r\n]*)*\047)|(?:"[^"\\\r\n]*(?:\\(?:[^' |
| 146 |
r'\r\n]|\r?\n|\r)[^"\\\r\n]*)*")|(?:\140[^\140\\]*(?:\\(?:[^\r\n' |
| 147 |
r']|\r?\n|\r)[^\140\\]*)*\140))[^\047"\140/\000-\040]*)|(?<=[)])' |
| 148 |
r'(?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+' |
| 149 |
r')*/))*(?:(?:(?://[^\r\n]*)?[\r\n])(?:[\000-\011\013\014\016-\0' |
| 150 |
r'40]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*)*((?:/(?![\r\n/*])[^/' |
| 151 |
r'\\\[\r\n]*(?:(?:\\[^\r\n]|(?:\[[^\\\]\r\n]*(?:\\[^\r\n][^\\\]' |
| 152 |
r'\r\n]*)*\]))[^/\\\[\r\n]*)*/[a-z]*))(?=(?:[\000-\011\013\014\0' |
| 153 |
r'16-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*(?:(?:(?://[^\r\n' |
| 154 |
r']*)?[\r\n])(?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^' |
| 155 |
r'/*][^*]*\*+)*/))*)*\.(?:[\000-\011\013\014\016-\040]|(?:/\*[^*' |
| 156 |
r']*\*+(?:[^/*][^*]*\*+)*/))*(?:(?:(?://[^\r\n]*)?[\r\n])(?:[\00' |
| 157 |
r'0-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*)' |
| 158 |
r'*[a-z])|(?<=[(,=:\[!&|?{};\r\n+*-])(?:[\000-\011\013\014\016-' |
| 159 |
r'\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*(?:(?:(?://[^\r\n]*)' |
| 160 |
r'?[\r\n])(?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*]' |
| 161 |
r'[^*]*\*+)*/))*)*((?:/(?![\r\n/*])[^/\\\[\r\n]*(?:(?:\\[^\r\n]|' |
| 162 |
r'(?:\[[^\\\]\r\n]*(?:\\[^\r\n][^\\\]\r\n]*)*\]))[^/\\\[\r\n]*)*' |
| 163 |
r'/[a-z]*))((?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/' |
| 164 |
r'*][^*]*\*+)*/))*(?:(?:(?://[^\r\n]*)?[\r\n])(?:[\000-\011\013' |
| 165 |
r'\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*)+(?=[^\000' |
| 166 |
r'-\040&)+,.:;=?\]|}-]))?|(?<=[\000-#%-,./:-@\[-^\140{-~-]return' |
| 167 |
r')(?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*' |
| 168 |
r'+)*/))*(?:((?:(?://[^\r\n]*)?[\r\n]))(?:[\000-\011\013\014\016' |
| 169 |
r'-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*)*((?:/(?![\r\n/*])' |
| 170 |
r'[^/\\\[\r\n]*(?:(?:\\[^\r\n]|(?:\[[^\\\]\r\n]*(?:\\[^\r\n][^' |
| 171 |
r'\\\]\r\n]*)*\]))[^/\\\[\r\n]*)*/[a-z]*))((?:[\000-\011\013\014' |
| 172 |
r'\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*(?:(?:(?://[^\r' |
| 173 |
r'\n]*)?[\r\n])(?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:' |
| 174 |
r'[^/*][^*]*\*+)*/))*)+(?=[^\000-\040&)+,.:;=?\]|}-]))?|(?<=[^\0' |
| 175 |
r'00-!#%&(*,./:-@\[\\^{|~])(?:[\000-\011\013\014\016-\040]|(?:/' |
| 176 |
r'\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*(?:((?:(?://[^\r\n]*)?[\r\n]))' |
| 177 |
r'(?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+' |
| 178 |
r')*/))*)+(?=[^\000-\040"#%-\047)*,./:-@\\-^\140|-~])|(?<=[^\000' |
| 179 |
r'-#%-,./:-@\[-^\140{-~-])((?:[\000-\011\013\014\016-\040]|(?:/' |
| 180 |
r'\*[^*]*\*+(?:[^/*][^*]*\*+)*/)))+(?=[^\000-#%-,./:-@\[-^\140{-' |
| 181 |
r'~-])|(?<=\+)((?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:' |
| 182 |
r'[^/*][^*]*\*+)*/)))+(?=\+)|(?<=-)((?:[\000-\011\013\014\016-\0' |
| 183 |
r'40]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/)))+(?=-)|(?:[\000-\011\0' |
| 184 |
r'13\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))+|(?:(?:(?' |
| 185 |
r'://[^\r\n]*)?[\r\n])(?:[\000-\011\013\014\016-\040]|(?:/\*[^*]' |
| 186 |
r'*\*+(?:[^/*][^*]*\*+)*/))*)+' |
| 187 |
) |
| 188 |
|
| 189 |
|
| 190 |
Related |
| 191 |
~~~~~~~ |
| 192 |
|
| 193 |
- `CSS Minifier rCSSmin <http://opensource.perlig.de/rcssmin/>`_ |
| 194 |
|
| 195 |
|
| 196 |
.. vim: ft=rest tw=72 |