pas2js minifier: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: pas2js minifier
| Old | New | Line | |
|---|---|---|---|
| 1 | + | # Overview | |
| 2 | + | ||
| 3 | + | This page explains ways to reduce and/or compress the pas2js generated JavaScript. | |
| 4 | + | ||
| 5 | + | For a complete list of optimizations, including ideas not yet implemented, see [[Pas2js optimizations]] | |
| 6 | + | ||
| 7 | + | # Omit unneeded code | |
| 8 | + | ||
| 9 | + | - Use pas2js option -Jc to generate one JavaScript program. This allows the compiler to use Whole Program Optimization (wpo) to determine all unused identifiers. | |
| 10 | + | - Use -O1 to omit unused identifiers. If a record/class is used, every unused member/method is omitted. | |
| 11 | + | - By default published properties/methods are kept and will generate type information (RTTI). If you don't need that you can ... | |
| 12 | + | - use {\$modeswitch omitrtti} (since pas2js 1.1), which treats class section 'published' as 'public' and *typeinfo()* does not work on symbols declared with this switch. | |
| 13 | + | - use -OoRemoveNotUsedPublished to omit all published properties not referenced at compile time. | |
| 14 | + | ||
| 15 | + | # Third party minifier | |
| 16 | + | ||
| 17 | + | There are several free minifiers for JavaScript. A minifier removes unneeded characters and replaces some code without changing its functionality. | |
| 18 | + | ||
| 19 | + | **Sourcemaps**: Keep in mind that third party minifiers usually makes the source map of pas2js unusable, so use the third party minifier only for the release version. | |
| 20 | + | ||
| 21 | + | As an example here is how to use uglifyjs with pas2js: | |
| 22 | + | ||
| 23 | + | ## Windows | |
| 24 | + | ||
| 25 | + | - Download and install "node" form <https://nodejs.org/en/download/> | |
| 26 | + | - Start the "Node.js command prompt". This starts a console, which is setup with an environment for *Node.js* and *npm*. | |
| 27 | + | - In the prompt type | |
| 28 | + | ||
| 29 | + | `npm install uglify-js -g` | |
| 30 | + | ||
| 31 | + | and press \<enter\> This will download and install uglifyjs as a command line app. Test it by typing | |
| 32 | + | ||
| 33 | + | `uglifyjs -h` | |
| 34 | + | ||
| 35 | + | This should give the list of uglifyjs options. | |
| 36 | + | ||
| 37 | + | - After install restart applications like Lazarus so they get the new PATH, which is needed by uglifyjs to work. | |
| 38 | + | - Find out the path of uglifyjs.cmd, for example by typing *where uglifyjs* | |
| 39 | + | - In Lazarus: | |
| 40 | + | - With pas2js 1.1: Add to the custom compiler options **"-JpcmdC:\path\of\uglifyjs.cmd"**. Note that *-Jpcmd* is case sensitive. | |
| 41 | + | - With pas2js 1.0: Set the *Execute After* command: *C:\path\of\uglifyjs.cmd -oproject1.js project1.js*. Replace *project1.js* with the name of your js file. | |
| 42 | + | ||
| 43 | + | ## Linux/MacOS | |
| 44 | + | ||
| 45 | + | - Install npm | |
| 46 | + | - Open a terminal and do: | |
| 47 | + | ||
| 48 | + | `sudo npm install uglify-js -g` | |
| 49 | + | ||
| 50 | + | This will download and install uglifyjs as a command line app. Test it by typing | |
| 51 | + | ||
| 52 | + | `uglifyjs -h` | |
| 53 | + | ||
| 54 | + | This should give the list of uglifyjs options. | |
| 55 | + | ||
| 56 | + | - In Lazarus: | |
| 57 | + | - With pas2js 1.1: Add to the custom compiler options **-Jpcmduglifyjs**. This is the option *-Jpcmd* plus the path to *uglifyjs*. | |
| 58 | + | - With pas2js 1.0: Set the *Execute After* command: *uglifyjs -oproject1.js project1.js*. Replace *project1.js* with the name of your js file. | |
| 59 | + | ||
| 60 | + | --- | |
| 61 | + | *From the Free Pascal wiki, page "pas2js minifier", as it stood on 2021-03-28 after a change by Mattias2, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* | |
| 62 | + | Category: Pas2js |
