Jump to content

Minification (programming)

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Cretin66 (talk | contribs) at 04:29, 4 October 2022 (riefly). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Minification (also minimisation or minimization) is the process of removing all unnecessary characters from the source code of interpreted programming languages or markup languages without changing its functionality. These unnecessary characters usually include white space characters, new line characters, comments, and sometimes block delimiters, which are used to add readability to the code but are not required for it to execute. Minification reduces the size of the source code, making its transmission over a network (e.g. the Internet) more efficient. In programmer culture, aiming at extremely minified source code is the purpose of recreational code golf competitions.

Minification can be distinguished from the more general concept of data compression in that the minified source can be interpreted immediately without the need for an uncompression step: the same interpreter can work with both the original as well as with the minified source.

The goals of minification are not the same as the goals of obfuscation; the former is often intended to be reversed using a pretty-printer[citation needed] or unminifier. However, to achieve its goals, minification sometimes uses techniques also used by obfuscation; for example, shortening variable names and refactoring the source code. When minification uses such techniques, the pretty-printer or unminifier can only fully reverse the minification process if it is supplied details of the transformations done by such techniques. If not supplied those details, the reversed source code will contain different variable names and control flow, even though it will have the same functionality as the original source code.

Example

For example, the JavaScript code

// This is a comment that will be removed by the minifier
var array = [];
for (var i = 0; i < 20; i++) {
  array[i] = i;
}

is equivalent to but longer than

for(var a=[i=0];i<20;a[i]=i++);

History

In 2001 Douglas Crockford introduced JSMin,[1] In 2010, Mihai Bazon introduced UglifyJS, which was superseded by UglifyJS2 in 2012; the rewrite was to allow for source map support.[2] From 2017, Alex Lam took over maintenance and development of UglifyJS2, replacing it with UglifyJS3 which unified the CLI with the API.[3]

Source mapping

A Source Map is a file format that allows software tools for JavaScript to display different code to a user than the code actually executed by the computer. For example, to aid in debugging of minified code, by "mapping" this code to the original unminified source code instead.

The original format was created by Joseph Schorr as part of the Closure Inspector minification project.[4] Version 2 and 3 of the format reduced the size of the map files considerably.[4]

Types

Tools

Visual Studio Code comes with minification support for several languages. It can readily browse the Visual Studio Marketplace to download and install additional minifiers.

JavaScript optimizers which can minify and generate source maps include UglifyJS and Google Closure Compiler. In addition, certain online tools, such as Microsoft Ajax Minifier,[5] the Yahoo! YUI Compressor or Pretty Diff,[6] can compress CSS files.{{citation needed|datee multiple script files into a single file for client download. JavaScript source maps can make code readable and debuggable even after it has been combined and minified.[7]

References

  1. ^ Paul, Ryan (6 November 2009). "Google opens up its JavaScript development toolbox to all". Ars Technica. Self-published. {{cite web}}: |archive-url= requires |archive-date= (help); Check |archive-url= value (help)
  2. ^ Bazon, Mihai (8 November 2012). "Should you switch to UglifyJS2?". lisperator.net. Self-published.
  3. ^ "uglify-js NPM". npmjs.com.
  4. ^ a b "Source Map Revision 3 Proposal". 11 January 2011. Archived from the original on 26 November 2014. Retrieved 16 April 2016.
  5. ^ Microsoft Ajax Minifier. Ajaxmin.codeplex.com (13 September 2012).
  6. ^ Pretty Diff. Pretty Diff.
  7. ^ "Introduction to JavaScript Source Maps - HTML5 Rocks".