wikimedia/minify
Minification of JavaScript code and CSS stylesheets
Loading...
Searching...
No Matches
Wikimedia\Minify\JavaScriptMinifier Class Reference

JavaScript Minifier. More...

Static Public Member Functions

static minify ( $s, $onError=null)
 Returns minified JavaScript code.
 
static createMinifier ()
 Create a minifier state object without source map capabilities.
 
static createSourceMapState ()
 Create a minifier state object with source map capabilities.
 
static createIdentityMinifier ()
 Create a MinifierState that doesn't actually minify.
 
static minifyInternal ( $s, $mapGenerator=null, $onError=null)
 Minify with optional source map.
 

Detailed Description

JavaScript Minifier.

This class is meant to safely minify JavaScript code, while leaving syntactically correct programs intact. Other libraries, such as JSMin require a certain coding style to work correctly. OTOH, libraries like jsminplus, that do parse the code correctly are rather slow, because they construct a complete parse tree before outputting the code minified. So this class is meant to allow arbitrary (but syntactically correct) input, while being fast enough to be used for on-the-fly minifying.

This class was written with ECMA-262 7th Edition in mind ("ECMAScript 2016"). Parsing features new to later editions of ECMAScript might not be supported. It's assumed that the input is syntactically correct; if it's not, this class may not detect that, and may produce incorrect output.

This class has limited support for 8.0 spec ("ECMAScript 2017"), specifically, the await keyword, and most kinds of async functions are implemented. Other new parsing features of ES2017 are not yet supported.

See also:

Member Function Documentation

◆ createIdentityMinifier()

static Wikimedia\Minify\JavaScriptMinifier::createIdentityMinifier ( )
static

Create a MinifierState that doesn't actually minify.

Returns
IdentityMinifierState

◆ createMinifier()

static Wikimedia\Minify\JavaScriptMinifier::createMinifier ( )
static

Create a minifier state object without source map capabilities.

Example:

JavaScriptMinifier::createMinifier() ->addSourceFile( 'file.js', $source ) ->getMinifiedOutput();

Returns
JavaScriptMinifierState

◆ createSourceMapState()

static Wikimedia\Minify\JavaScriptMinifier::createSourceMapState ( )
static

Create a minifier state object with source map capabilities.

Example:

$mapper = JavaScriptMinifier::createSourceMapState() ->addSourceFile( 'file1.js', $source1 ) ->addOutput( "\n\n" ) ->addSourceFile( 'file2.js', $source2 ); $out = $mapper->getMinifiedOutput(); $map = $mapper->getSourceMap()

Returns
JavaScriptMapperState

◆ minify()

static Wikimedia\Minify\JavaScriptMinifier::minify ( $s,
$onError = null )
static

Returns minified JavaScript code.

See also
MinifierState::setErrorHandler
Parameters
string$sJavaScript code to minify
callable | null$onErrorCalled with a ParseError object
Returns
string Minified code

The documentation for this class was generated from the following file: