wikimedia/minify
Minification of JavaScript code and CSS stylesheets
|
The base class for stateful minifying without source map support. More...
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 | |
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.
Wikimedia\Minify\MinifierState::addOutput | ( | string | $output | ) |
Add a string to the output without any minification or source mapping.
string | $output |
Reimplemented in Wikimedia\Minify\MapperState.
Wikimedia\Minify\MinifierState::addSourceFile | ( | string | $url, |
string | $source, | ||
bool | $bundle = false ) |
Minify a source file and collect the output and mappings data.
string | $url | The name of the input file. Possibly a URL relative to the source root. |
string | $source | The input source text. |
bool | $bundle | Whether to add the source text to sourcesContent |
Reimplemented in Wikimedia\Minify\MapperState.
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.
Wikimedia\Minify\MinifierState::getMinifiedOutput | ( | ) |
Get the minified output.
|
abstractprotected |
Minify a string.
string | $source |
Reimplemented in Wikimedia\Minify\IdentityMinifierState, Wikimedia\Minify\JavaScriptMapperState, and Wikimedia\Minify\JavaScriptMinifierState.
Wikimedia\Minify\MinifierState::outputFile | ( | string | $file | ) |
Set the name of the output file, to be given as the "file" key.
string | $file |
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();
callable | $onError |
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.
string | $url |