diff options
-rw-r--r-- | plugins/minimize.sh | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/plugins/minimize.sh b/plugins/minimize.sh new file mode 100644 index 0000000..f7eec1b --- /dev/null +++ b/plugins/minimize.sh | |||
@@ -0,0 +1,45 @@ | |||
1 | # minimize.sh --- Minimize CSS/JS assets using sed | ||
2 | |||
3 | # from https://www.tero.co.uk/scripts/minify.php | ||
4 | |||
5 | ### Commentary: | ||
6 | |||
7 | ## How it works | ||
8 | # It removes comments from CSS and Javascript files using sed. First it replaces | ||
9 | # /**/ and /*\*/ with /~~/ and /~\~/ as these comments are used as CSS hacks and | ||
10 | # should stay in the file. Then it removes /*...*/ style comments on single | ||
11 | # lines (from Javascript and CSS files - this was adapted from a common PHP | ||
12 | # regular expression for removing CSS comments). Then it removes // style | ||
13 | # comments from Javascript files (unless they are preceded by a : which means | ||
14 | # they might be web links). (It's done in this order so that comments like | ||
15 | # /*...//...*/ get removed properly). Then it replaces all newlines with spaces | ||
16 | # and removes /*...*/ style comments again (this time over multiple lines). Then | ||
17 | # it puts the /**/ and /*\*/ CSS hacks back in. Then it replaces multiple spaces | ||
18 | # with single spaces. Then it removes spaces before [{;:,] and then spaces after | ||
19 | # [{:;,]. | ||
20 | |||
21 | ## Problems - quotes and Ajaxy functions | ||
22 | # This script does not ignore things in quotes. So if you have a comment | ||
23 | # inside a quote in a Javascript file, such as document.writeln ('//This | ||
24 | # is a comment');, the script will remove everything from the // and | ||
25 | # cause a Javascript error. Also, all lines of Javascript must end with | ||
26 | # a semicolon. This can be an issue with libraries which declare | ||
27 | # functions on a line but don't end the line with a semicolon (as is | ||
28 | # common in Ajax and JQuery scripts). For example, in: load : function | ||
29 | # (url,callback,format) {...} http.send(nul);, there should be a | ||
30 | # semicolon after the function definition. This is legal Javascript if | ||
31 | # there's a line break, but causes an error once minimised without the | ||
32 | # line break. | ||
33 | |||
34 | ## The Function | ||
35 | minimize() { # minimize < INPUT | ||
36 | sed -e "s|/\*\(\\\\\)\?\*/|/~\1~/|g" \ | ||
37 | -e "s|/\*[^*]*\*\+\([^/][^*]*\*\+\)*/||g" \ | ||
38 | -e "s|\([^:/]\)//.*$|\1|" -e "s|^//.*$||" | | ||
39 | tr '\n' ' ' | | ||
40 | sed -e "s|/\*[^*]*\*\+\([^/][^*]*\*\+\)*/||g" \ | ||
41 | -e "s|/\~\(\\\\\)\?\~/|/*\1*/|g" \ | ||
42 | -e "s|\s\+| |g" \ | ||
43 | -e "s| \([{;:,]\)|\1|g" \ | ||
44 | -e "s|\([{;:,]\) |\1|g" | ||
45 | } | ||