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

The base class for stateful minifying without source map support. More...

+ Inheritance diagram for Wikimedia\Minify\MinifierState:

Public Member Functions

 outputFile (string $file)
 Set the name of the output file, to be given as the "file" key.
 
 sourceRoot (string $url)
 Set the source root.
 
 addSourceFile (string $url, string $source, bool $bundle=false)
 Minify a source file and collect the output and mappings data.
 
 setErrorHandler ( $onError)
 Register an error callback.
 
 addOutput (string $output)
 Add a string to the output without any minification or source mapping.
 
 ensureNewline ()
 Add a line break to the output if the output is non-empty and does not end in a line break.
 
 getMinifiedOutput ()
 Get the minified output.
 

Protected Member Functions

 minify (string $source)
 Minify a string.
 

Protected Attributes

 $sources = []
 
 $sourcesContent = []
 
 $minifiedOutput = ''
 
 $outputFile
 
 $sourceRoot
 
 $onError
 

Detailed Description

The base class for stateful minifying without source map support.

Some stub mutator methods for source map support are provided for the convenience of callers switching between source map and plain mode.

Member Function Documentation

◆ addOutput()

Wikimedia\Minify\MinifierState::addOutput ( string $output)

Add a string to the output without any minification or source mapping.

Parameters
string$output
Returns
$this

Reimplemented in Wikimedia\Minify\MapperState.

◆ addSourceFile()

Wikimedia\Minify\MinifierState::addSourceFile ( string $url,
string $source,
bool $bundle = false )

Minify a source file and collect the output and mappings data.

Parameters
string$urlThe name of the input file. Possibly a URL relative to the source root.
string$sourceThe input source text.
bool$bundleWhether to add the source text to sourcesContent
Returns
$this

Reimplemented in Wikimedia\Minify\MapperState.

◆ ensureNewline()

Wikimedia\Minify\MinifierState::ensureNewline ( )

Add a line break to the output if the output is non-empty and does not end in a line break.

Returns
$this

◆ getMinifiedOutput()

Wikimedia\Minify\MinifierState::getMinifiedOutput ( )

Get the minified output.

Returns
string

◆ minify()

Wikimedia\Minify\MinifierState::minify ( string $source)
abstractprotected

Minify a string.

Parameters
string$source
Returns
string

Reimplemented in Wikimedia\Minify\IdentityMinifierState, Wikimedia\Minify\JavaScriptMapperState, and Wikimedia\Minify\JavaScriptMinifierState.

◆ outputFile()

Wikimedia\Minify\MinifierState::outputFile ( string $file)

Set the name of the output file, to be given as the "file" key.

Parameters
string$file
Returns
$this

◆ setErrorHandler()

Wikimedia\Minify\MinifierState::setErrorHandler ( $onError)

Register an error callback.

JavaScriptMinifier assumes all input has previously been validated by a linter (such as Peast or ESLint) and not attempt to validate the syntax. Any new or unknown syntax is generally silently preserved in the output.

Certain kinds of syntax error are nonetheless detected and can be logged via this error callback to aid in debugging.

The error callback may be invoked during MinifierState::addSourceFile (via JavaScriptMinifier::minify), and is passed a ParseError object as its only parameter.

Example:

JavaScriptMinifier::createMinifier() ->setErrorHandler( static function ( $error ) { trigger_error( $error->getMessage() ); } ) ->addSourceFile( 'file.js', $source ) ->getMinifiedOutput();

Parameters
callable$onError
Returns
$this

◆ sourceRoot()

Wikimedia\Minify\MinifierState::sourceRoot ( string $url)

Set the source root.

The spec says this will be merely "prepended" to the source names, not resolved as a relative URL, so it should probably have a trailing slash.

Parameters
string$url
Returns
$this

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