Minification (also minimisation or minimization) is the process of removing all unnecessary characters from the
source code
In computing, source code, or simply code, is any collection of code, with or without comment (computer programming), comments, written using a human-readable programming language, usually as plain text. The source code of a Computer program, p ...
of
interpreted programming languages or
markup language
Markup language refers to a text-encoding system consisting of a set of symbols inserted in a text document to control its structure, formatting, or the relationship between its parts. Markup is often used to control the display of the document ...
s without changing its functionality. These unnecessary characters usually include
white space characters,
new line characters,
comments
Comment may refer to:
* Comment (linguistics) or rheme, that which is said about the topic (theme) of a sentence
* Bernard Comment (born 1960), Swiss writer and publisher
Computing
* Comment (computer programming), explanatory text or informa ...
, 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 information theory, data compression, source coding, or bit-rate reduction is the process of encoding information using fewer bits than the original representation. Any particular compression is either lossy or lossless. Lossless compressi ...
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
Obfuscation is the obscuring of the intended meaning of communication by making the message difficult to understand, usually with confusing and ambiguous language. The obfuscation might be either unintentional or intentional (although intent u ...
; the former is often intended to be reversed using a
pretty-print
Pretty-printing (or prettyprinting) is the application of any of various stylistic text formatting, formatting conventions to text files, such as source code, markup language, markup, and similar kinds of content. These formatting conventions may ...
er or unminifier. However, to achieve its goals, minification sometimes uses techniques also used by
obfuscation
Obfuscation is the obscuring of the intended meaning of communication by making the message difficult to understand, usually with confusing and ambiguous language. The obfuscation might be either unintentional or intentional (although intent u ...
; for example, shortening variable names and refactoring the
source code
In computing, source code, or simply code, is any collection of code, with or without comment (computer programming), comments, written using a human-readable programming language, usually as plain text. The source code of a Computer program, p ...
. When minification uses such techniques, the
pretty-print
Pretty-printing (or prettyprinting) is the application of any of various stylistic text formatting, formatting conventions to text files, such as source code, markup language, markup, and similar kinds of content. These formatting conventions may ...
er 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
In computing, source code, or simply code, is any collection of code, with or without comment (computer programming), comments, written using a human-readable programming language, usually as plain text. The source code of a Computer program, p ...
will contain different variable names and control flow, even though it will have the same functionality as the original
source code
In computing, source code, or simply code, is any collection of code, with or without comment (computer programming), comments, written using a human-readable programming language, usually as plain text. The source code of a Computer program, p ...
.
Example
For example, the
JavaScript
JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of Website, websites use JavaScript on the Client (computing), client side ...
code
// This is a comment that will be removed by the minifier
var array = [];
for (var i = 0; i < 20; i++)
is equivalent to but longer than
for(var a=[i=0];i<20;a[i]=i++);
History
In 2001
Douglas Crockford introduced JSMin, which removed comments and whitespace from JavaScript code.
It was followed by YUI Compressor in 2007.
In 2009, Google opened up its Closure toolkit, including Closure Compiler which contained a source mapping feature together with a Firefox extension called Closure Inspector. In 2010, Mihai Bazon introduced UglifyJS, which was superseded by UglifyJS2 in 2012; the rewrite was to allow for source map support. From 2017, Alex Lam took over maintenance and development of UglifyJS2, replacing it with UglifyJS3 which unified the CLI with the API.
Source mapping
A Source Map is a
file format
A file format is a Computer standard, standard way that information is encoded for storage in a computer file. It specifies how bits are used to encode information in a digital storage medium. File formats may be either proprietary format, pr ...
that allows software tools for
JavaScript
JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of Website, websites use JavaScript on the Client (computing), client side ...
to display different code to a user than the code actually executed by the computer. For example, to aid in
debugging
In computer programming and software development, debugging is the process of finding and resolving ''bugs'' (defects or problems that prevent correct operation) within computer programs, software, or systems.
Debugging tactics can involve in ...
of minified code, by "mapping" this code to the original unminified
source code
In computing, source code, or simply code, is any collection of code, with or without comment (computer programming), comments, written using a human-readable programming language, usually as plain text. The source code of a Computer program, p ...
instead.
The original format was created by Joseph Schorr as part of the Closure Inspector minification project.
Version 2 and 3 of the format reduced the size of the map files considerably.
Types
Tools
Visual Studio Code
Visual Studio Code, also commonly referred to as VS Code, is a source-code editor made by Microsoft with the Electron Framework, for Windows, Linux and macOS. Features include support for debugging, syntax highlighting, intelligent code compl ...
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
Google Closure Tools is a set of tools to help developers build rich web applications with JavaScript. It was developed by Google for use in their web applications such as Gmail, Google Docs and Google Maps.
Closure Compiler
The Closure Comp ...
. In addition, certain online tools, such as Microsoft Ajax Minifier, the
Yahoo! YUI Compressor or
Pretty Diff
Pretty Diff is a language-aware data comparison utility implemented in TypeScript. The online utility is capable of source code prettification, minification, and comparison of two pieces of input text. It operates by removing code comments from ...
, can compress
CSS
Cascading Style Sheets (CSS) is a style sheet language used for describing the presentation of a document written in a markup language such as HTML or XML (including XML dialects such as SVG, MathML or XHTML). CSS is a cornerstone t ...
files. For minification of HTML code there are the following tools: HtmlCompressor, HTMLMinifier and WebMarkupMin. There is a Power-Shell script named "minifyPS" that is able to shrink PowerShell script code as well as JavaScript code.
Web development
Components and libraries for
Web application
A web application (or web app) is application software that is accessed using a web browser. Web applications are delivered on the World Wide Web to users with an active network connection.
History
In earlier computing models like client-serve ...
s and websites have been developed to optimize file requests and reduce page load times by shrinking the size of various files.
JavaScript and
Cascading Style Sheet
Cascading Style Sheets (CSS) is a style sheet language used for describing the presentation of a document written in a markup language such as HTML or XML (including XML dialects such as SVG, MathML or XHTML). CSS is a cornerstone technol ...
(CSS) resources may be minified, preserving their behavior while considerably reducing their file size. Libraries available online are capable of minification and optimization to varying degrees. Some libraries also merge 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.
References
{{Reflist
Source code
JavaScript