pas2js minifier: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | # Overview | |
| 2 | 2 | ||
| 3 | 3 | This page explains ways to reduce and/or compress the pas2js generated JavaScript. | |
| 4 | 4 | ||
| 5 | 5 | For a complete list of optimizations, including ideas not yet implemented, see [[Pas2js optimizations]] | |
| 6 | 6 | ||
| 7 | 7 | # Omit unneeded code | |
| 8 | 8 | ||
| 9 | 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 | 10 | - Use -O1 to omit unused identifiers. If a record/class is used, every unused member/method is omitted. | |
| 11 | 11 | - By default published properties/methods are kept and will generate type information (RTTI). If you don't need that you can ... | |
| 12 | 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 | 13 | - use -OoRemoveNotUsedPublished to omit all published properties not referenced at compile time. | |
| 14 | 14 | ||
| 15 | 15 | # Third party minifier | |
| 16 | 16 | ||
| 17 | 17 | There are several free minifiers for JavaScript. A minifier removes unneeded characters and replaces some code without changing its functionality. | |
| 18 | 18 | ||
| 19 | 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 | 20 | ||
| 21 | 21 | As an example here is how to use uglifyjs with pas2js: | |
| 22 | 22 | ||
| 23 | 23 | ## Windows | |
| 24 | 24 | ||
| 25 | 25 | - Download and install "node" form <https://nodejs.org/en/download/> | |
| 26 | 26 | - Start the "Node.js command prompt". This starts a console, which is setup with an environment for *Node.js* and *npm*. | |
| 27 | 27 | - In the prompt type | |
| 28 | 28 | ||
| 29 | - | ||
| 29 | + | ```text | |
| 30 | + | npm install uglify-js -g | |
| 31 | + | ``` | |
| 30 | 32 | ||
| 31 | 33 | and press \<enter\> This will download and install uglifyjs as a command line app. Test it by typing | |
| 32 | 34 | ||
| 33 | - | ||
| 35 | + | ```text | |
| 36 | + | uglifyjs -h | |
| 37 | + | ``` | |
| 34 | 38 | ||
| 35 | 39 | This should give the list of uglifyjs options. | |
| 36 | 40 | ||
| 37 | 41 | - After install restart applications like Lazarus so they get the new PATH, which is needed by uglifyjs to work. | |
| 38 | 42 | - Find out the path of uglifyjs.cmd, for example by typing *where uglifyjs* | |
| 39 | 43 | - In Lazarus: | |
| 40 | 44 | - With pas2js 1.1: Add to the custom compiler options **"-JpcmdC:\path\of\uglifyjs.cmd"**. Note that *-Jpcmd* is case sensitive. | |
| 41 | 45 | - 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 | 46 | ||
| 43 | 47 | ## Linux/MacOS | |
| 44 | 48 | ||
| 45 | 49 | - Install npm | |
| 46 | 50 | - Open a terminal and do: | |
| 47 | 51 | ||
| 48 | - | ||
| 52 | + | ```text | |
| 53 | + | sudo npm install uglify-js -g | |
| 54 | + | ``` | |
| 49 | 55 | ||
| 50 | 56 | This will download and install uglifyjs as a command line app. Test it by typing | |
| 51 | 57 | ||
| 52 | - | ||
| 58 | + | ```text | |
| 59 | + | uglifyjs -h | |
| 60 | + | ``` | |
| 53 | 61 | ||
| 54 | 62 | This should give the list of uglifyjs options. | |
| 55 | 63 | ||
| 56 | 64 | - In Lazarus: | |
| 57 | 65 | - With pas2js 1.1: Add to the custom compiler options **-Jpcmduglifyjs**. This is the option *-Jpcmd* plus the path to *uglifyjs*. | |
| 58 | 66 | - 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 | 67 | ||
| 60 | 68 | --- | |
| 61 | 69 | *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 | 70 | Category: Pas2js |
