| 1 | # Squish - One language to write them all, one squisher to squish them |
| 2 | |
| 3 | Squish is a simple script to build a single file out of multiple scripts, modules, and other files. |
| 4 | |
| 5 | For example if you have a script called A, and it requires modules X, Y and Z, all of them could be squished |
| 6 | into a single file, B. |
| 7 | |
| 8 | When run, Squish reads a file called 'squishy' in the current (or specified) directory, which contains |
| 9 | instructions on how to squish a project. |
| 10 | |
| 11 | For an example you can see Squish's own squishy file, included in this package. For reference, see below. |
| 12 | |
| 13 | ## Building and installing |
| 14 | |
| 15 | Squish uses itself to squish itself and its components into a single 'squish' utility that can be run anywhere. |
| 16 | To build squish, just run "make" - there are no dependencies other than Lua. |
| 17 | |
| 18 | You can run "make install" to copy squish to /usr/local/bin/ if you have permission. |
| 19 | |
| 20 | ## Squishing |
| 21 | |
| 22 | Running squish will search for a 'squishy' file in the current directory. Alternatively you can pass to squish |
| 23 | a directory to look in. |
| 24 | |
| 25 | Command-line options vary depending on what features squish has been built with. Below are the standard ones. |
| 26 | |
| 27 | ### Minify |
| 28 | 'Minification' is the act of condensing source code by stripping out spaces, line breaks, comments and anything |
| 29 | that isn't required to be there. Although the source code is re-organised and changed, the program is still the |
| 30 | same and runs without any changes. |
| 31 | |
| 32 | #### --no-minify |
| 33 | Disable minification of the output file after squishing. Default is to minify. |
| 34 | |
| 35 | #### --minify-level=level |
| 36 | The level may be one of: none, basic, default, full |
| 37 | |
| 38 | They vary in effectiveness, and the time taken to process large files. Experiment! |
| 39 | |
| 40 | ### Uglify |
| 41 | 'Uglification' is the name Squish gives to a certain basic form of compression. With large files it can reduce the |
| 42 | size by some kilobytes, even after full minification. It works by replacing Lua keywords with a single byte and |
| 43 | inserting some short code at the start of the script to expand the keywords when it is run. |
| 44 | |
| 45 | #### --uglify |
| 46 | Enable the uglification filter. Default is to not uglify. |
| 47 | |
| 48 | #### --uglify-level=LEVEL |
| 49 | If the level specified is "full" then Squish will extend its replacement to identifiers and string literals, as |
| 50 | well as Lua keywords. It first assigns each identifier and string a score based on its length and how many times |
| 51 | it appears in the file. The top scorers are assigned single-byte identifiers and replaced the same as the keywords. |
| 52 | |
| 53 | ### Gzip |
| 54 | Gzip, or rather the DEFLATE algorithm, is extremely good at compressing text-based data, including scripts. Using |
| 55 | this extension compresses the squished code, and adds some runtime decompression code. This decompression code adds |
| 56 | a little bit of time to the loading of the script, and adds 4K to the size of the generated code, but the overall |
| 57 | savings are usually well worth it. |
| 58 | |
| 59 | #### --gzip |
| 60 | Compress the generated code with gzip. Requires the gzip command-line utility (for compression only). |
| 61 | |
| 62 | ### Compile |
| 63 | Squish can compile the resulting file to Lua bytecode. This is experimental at this stage (you may get better results |
| 64 | with luac right now), however it's a work in progress. Compiling to bytecode can actually increase the size of |
| 65 | minified output, but it can speed up loading (not that you would notice it anyway, since the Lua compiler is so fast). |
| 66 | |
| 67 | #### --compile |
| 68 | Enables compilation of the output file. |
| 69 | |
| 70 | ### Debug |
| 71 | Due to the way Squish combines multiple scripts into one, sometimes when a squished script raises an error the traceback |
| 72 | will be fairly unhelpful, and point to a line in the unreadable squished script. This is where the debug extension comes in! |
| 73 | |
| 74 | #### --debug |
| 75 | This option includes some code into the squished file which will restore the filenames and line numbers in error messages and |
| 76 | tracebacks. This option will increase the size of the output by no more than about 6KB, so may be very much worth it when |
| 77 | squishing large tricky-to-debug applications and libraries. |
| 78 | |
| 79 | **Note:** Minification may interfere with the line number calculation, use --minify-level=debug to enable all features of minify |
| 80 | that don't change line numbers, and everything will be fine. |
| 81 | |
| 82 | ### Virtual IO |
| 83 | Squish allows you to pack resources (any file) into the squished output. Sometimes it would be convenient to access these through |
| 84 | the standard Lua io interface. Well now you can! :) |
| 85 | |
| 86 | #### --virtual-io |
| 87 | Inserts code into the squished output which replaces io.open, io.lines, dofile and loadfile. The new functions will first check |
| 88 | whether the specified filename matches a packed resource's name. If it does then it will operate on that resource instead of an |
| 89 | actual file. If the filename does _not_ match a resource then the function passes on to the real Lua functions. |
| 90 | |
| 91 | ## Squishy reference |
| 92 | |
| 93 | A squishy file is actually a Lua script which calls some Squish functions. These functions are listed here. |
| 94 | |
| 95 | ### Module "name" "path" |
| 96 | Adds the specified module to the list of those to be squished into the output file. The optional path specifies |
| 97 | where to find the file (relative to the squishy file), otherwise Squish will attempt to find the module itself. |
| 98 | |
| 99 | ### Main "script.lua" |
| 100 | Adds a script into the squished output. Scripts are executed in the order specified in the squishy file, but only |
| 101 | after all modules have been loaded. |
| 102 | |
| 103 | ### Output "filename.lua" |
| 104 | Names the output file. If none is specified, the default is 'squished.out.lua'. |
| 105 | |
| 106 | ### Option "name" "value" |
| 107 | Sets the specified option, to 'true', or to the optional given value. This allows a squishy file to set default |
| 108 | command-line options. |
| 109 | |
| 110 | ### GetOption "name" |
| 111 | Returns the current value of the given option. |
| 112 | |
| 113 | ### Resource "name" "path" |
| 114 | Adds a 'resource' to the squished file. A resource may be any file, text, binary, large or small. Scripts can |
| 115 | retrieve the resource at runtime by calling require_resource("name"). If no path is given then the name is used |
| 116 | as the path. |
| 117 | |
| 118 | ### AutoFetchURL "url" |
| 119 | **Experimental** feature which is subject to change. When specified, all the following Module statements will be |
| 120 | fetched via HTTP if not found on the filesystem. A ? (question mark) in the URL is replaced by the relative path |
| 121 | of the module file that was given in the Module statement. |
| 122 | |
| 123 | ## make_squishy |
| 124 | |
| 125 | Squish includes a small utility which aims to help with converting a project to use Squish. Pass it a list of files |
| 126 | and it will scan those files looking for calls to require(). It will then attempt to resolve the module names to |
| 127 | files relative to the directory of the first filename passed to make_squishy. |
| 128 | |
| 129 | It generates a 'squishy.new' file in the current directory. Modify accordingly and rename to just 'squishy'. |