JS
extends Minify
in package
JavaScript Minifier Class.
Please report bugs on https://github.com/matthiasmullie/minify/issues
Tags
Table of Contents
Properties
- $data : array<string|int, string>
- The data to be minified.
- $keywordsAfter : array<string|int, string>
- List of JavaScript reserved words that accept a <variable, value, ...> before them. Some end of lines are not the end of a statement, like when continued by one of these keywords on the newline.
- $keywordsBefore : array<string|int, string>
- List of JavaScript reserved words that accept a <variable, value, ...> after them. Some end of lines are not the end of a statement, like with these keywords.
- $keywordsReserved : array<string|int, string>
- Full list of JavaScript reserved words.
- $operators : array<string|int, string>
- List of all JavaScript operators.
- $operatorsAfter : array<string|int, string>
- List of JavaScript operators that accept a <variable, value, ...> before them. Some end of lines are not the end of a statement, like when continued by one of these operators on the newline.
- $operatorsBefore : array<string|int, string>
- List of JavaScript operators that accept a <variable, value, ...> after them. Some end of lines are not the end of a statement, like with these operators.
- $patterns : array<string|int, string>
- Array of patterns to match.
Methods
- __construct() : mixed
- Init the minify class - optionally, code may be passed along already.
- add() : static
- Add a file or straight-up code to be minified.
- addFile() : static
- Add a file to be minified.
- cache() : CacheItemInterface
- Minify the data & write it to a CacheItemInterface object.
- execute() : string
- Minify the data.
- gzip() : string
- Minify & gzip the data & (optionally) saves it to a file.
- minify() : string
- Minify the data & (optionally) saves it to a file.
- canImportFile() : bool
- Check if the path is a regular file and can be read.
- executeReplacement() : string
- If $replacement is a callback, execute it, passing in the match data.
- extractRegex() : mixed
- JS can have /-delimited regular expressions, like: /ab+c/.match(string).
- extractStrings() : mixed
- Strings are a pattern we need to match, in order to ignore potential code-like content inside them, but we just want all of the string content to remain untouched.
- getKeywordsForRegex() : array<string|int, string>
- We'll strip whitespace around certain keywords with regular expressions.
- getOperatorsForRegex() : array<string|int, string>
- We'll strip whitespace around certain operators with regular expressions.
- load() : string
- Load data.
- openFileForWriting() : resource
- Attempts to open file specified by $path for writing.
- propertyNotation() : string
- Replaces all occurrences of array['key'] by array.key.
- registerPattern() : mixed
- Register a pattern to execute against the source content.
- replace() : string
- We can't "just" run some regular expressions against JavaScript: it's a complex language. E.g. having an occurrence of // xyz would be a comment, unless it's used within a string. Of you could have something that looks like a 'string', but inside a comment.
- restoreExtractedData() : string
- This method will restore all extracted data (strings, regexes) that were replaced with placeholder text in extract*(). The original content was saved in $this->extracted.
- save() : mixed
- Save to file.
- shortenBools() : string
- Replaces true & false by !0 and !1.
- str_replace_first() : mixed
- stripComments() : mixed
- Strip comments from source code.
- stripMultilineComments() : mixed
- Both JS and CSS use the same form of multi-line comment, so putting the common code here.
- stripWhitespace() : string
- Strip whitespace.
- writeToFile() : mixed
- Attempts to write $content to the file specified by $handler. $path is used for printing exceptions.
Properties
$data
The data to be minified.
protected
array<string|int, string>
$data
= array()
$keywordsAfter
List of JavaScript reserved words that accept a <variable, value, ...> before them. Some end of lines are not the end of a statement, like when continued by one of these keywords on the newline.
protected
array<string|int, string>
$keywordsAfter
= array()
E.g.: we shouldn't insert a ; before this instanceof variable instanceof String
Will be loaded from /data/js/keywords_after.txt
$keywordsBefore
List of JavaScript reserved words that accept a <variable, value, ...> after them. Some end of lines are not the end of a statement, like with these keywords.
protected
array<string|int, string>
$keywordsBefore
= array()
E.g.: we shouldn't insert a ; after this else else console.log('this is quite fine')
Will be loaded from /data/js/keywords_before.txt
$keywordsReserved
Full list of JavaScript reserved words.
protected
array<string|int, string>
$keywordsReserved
= array()
Will be loaded from /data/js/keywords_reserved.txt.
Tags
$operators
List of all JavaScript operators.
protected
array<string|int, string>
$operators
= array()
Will be loaded from /data/js/operators.txt
Tags
$operatorsAfter
List of JavaScript operators that accept a <variable, value, ...> before them. Some end of lines are not the end of a statement, like when continued by one of these operators on the newline.
protected
array<string|int, string>
$operatorsAfter
= array()
Note: Most operators are fine, we've only removed ), ], ++, --, ! and ~. There can't be a newline separating ! or ~ and whatever it is negating. ++ & -- have to be joined with the value they're in-/decrementing. ) & ] are "special" in that they have lots or usecases. () for example is used for function calls, for grouping, in if () and for (), ...
Will be loaded from /data/js/operators_after.txt
Tags
$operatorsBefore
List of JavaScript operators that accept a <variable, value, ...> after them. Some end of lines are not the end of a statement, like with these operators.
protected
array<string|int, string>
$operatorsBefore
= array()
Note: Most operators are fine, we've only removed ++ and --. ++ & -- have to be joined with the value they're in-/decrementing.
Will be loaded from /data/js/operators_before.txt
Tags
$patterns
Array of patterns to match.
protected
array<string|int, string>
$patterns
= array()
Methods
__construct()
Init the minify class - optionally, code may be passed along already.
public
__construct() : mixed
add()
Add a file or straight-up code to be minified.
public
add(string|array<string|int, string> $data) : static
Parameters
- $data : string|array<string|int, string>
Return values
staticaddFile()
Add a file to be minified.
public
addFile(string|array<string|int, string> $data) : static
Parameters
- $data : string|array<string|int, string>
Tags
Return values
staticcache()
Minify the data & write it to a CacheItemInterface object.
public
cache(CacheItemInterface $item) : CacheItemInterface
Parameters
- $item : CacheItemInterface
-
Cache item to write the data to
Return values
CacheItemInterface —Cache item with the minifier data
execute()
Minify the data.
public
execute([mixed $path = null ]) : string
Perform JS optimizations.
Parameters
- $path : mixed = null
Return values
string —The minified data
gzip()
Minify & gzip the data & (optionally) saves it to a file.
public
gzip([mixed $path = null ][, mixed $level = 9 ]) : string
Parameters
- $path : mixed = null
- $level : mixed = 9
Return values
string —The minified & gzipped data
minify()
Minify the data & (optionally) saves it to a file.
public
minify([mixed $path = null ]) : string
Parameters
- $path : mixed = null
Return values
string —The minified data
canImportFile()
Check if the path is a regular file and can be read.
protected
canImportFile(string $path) : bool
Parameters
- $path : string
Return values
boolexecuteReplacement()
If $replacement is a callback, execute it, passing in the match data.
protected
executeReplacement(string|callable $replacement, array<string|int, mixed> $match) : string
If it's a string, just pass it through.
Parameters
- $replacement : string|callable
-
Replacement value
- $match : array<string|int, mixed>
-
Match data, in PREG_OFFSET_CAPTURE form
Return values
stringextractRegex()
JS can have /-delimited regular expressions, like: /ab+c/.match(string).
protected
extractRegex() : mixed
The content inside the regex can contain characters that may be confused for JS code: e.g. it could contain whitespace it needs to match & we don't want to strip whitespace in there.
The regex can be pretty simple: we don't have to care about comments, (which also use slashes) because stripComments() will have stripped those already.
This method will replace all string content with simple REGEX# placeholder text, so we've rid all regular expressions from characters that may be misinterpreted. Original regex content will be saved in $this->extracted and after doing all other minifying, we can restore the original content via restoreRegex()
extractStrings()
Strings are a pattern we need to match, in order to ignore potential code-like content inside them, but we just want all of the string content to remain untouched.
protected
extractStrings([mixed $chars = ''"' ][, mixed $placeholderPrefix = '' ]) : mixed
This method will replace all string content with simple STRING# placeholder text, so we've rid all strings from characters that may be misinterpreted. Original string content will be saved in $this->extracted and after doing all other minifying, we can restore the original content via restoreStrings().
Parameters
- $chars : mixed = ''"'
- $placeholderPrefix : mixed = ''
getKeywordsForRegex()
We'll strip whitespace around certain keywords with regular expressions.
protected
getKeywordsForRegex(array<string|int, string> $keywords[, string $delimiter = '/' ]) : array<string|int, string>
This will prepare the given array by escaping all characters.
Parameters
- $keywords : array<string|int, string>
- $delimiter : string = '/'
Return values
array<string|int, string>getOperatorsForRegex()
We'll strip whitespace around certain operators with regular expressions.
protected
getOperatorsForRegex(array<string|int, string> $operators[, string $delimiter = '/' ]) : array<string|int, string>
This will prepare the given array by escaping all characters.
Parameters
- $operators : array<string|int, string>
- $delimiter : string = '/'
Return values
array<string|int, string>load()
Load data.
protected
load(string $data) : string
Parameters
- $data : string
-
Either a path to a file or the content itself
Return values
stringopenFileForWriting()
Attempts to open file specified by $path for writing.
protected
openFileForWriting(string $path) : resource
Parameters
- $path : string
-
The path to the file
Tags
Return values
resource —Specifier for the target file
propertyNotation()
Replaces all occurrences of array['key'] by array.key.
protected
propertyNotation(string $content) : string
Parameters
- $content : string
Return values
stringregisterPattern()
Register a pattern to execute against the source content.
protected
registerPattern(string $pattern[, string|callable $replacement = '' ]) : mixed
If $replacement is a string, it must be plain text. Placeholders like $1 or \2 don't work. If you need that functionality, use a callback instead.
Parameters
- $pattern : string
-
PCRE pattern
- $replacement : string|callable = ''
-
Replacement value for matched pattern
replace()
We can't "just" run some regular expressions against JavaScript: it's a complex language. E.g. having an occurrence of // xyz would be a comment, unless it's used within a string. Of you could have something that looks like a 'string', but inside a comment.
protected
replace(string $content) : string
The only way to accurately replace these pieces is to traverse the JS one character at a time and try to find whatever starts first.
Parameters
- $content : string
-
The content to replace patterns in
Return values
string —The (manipulated) content
restoreExtractedData()
This method will restore all extracted data (strings, regexes) that were replaced with placeholder text in extract*(). The original content was saved in $this->extracted.
protected
restoreExtractedData(string $content) : string
Parameters
- $content : string
Return values
stringsave()
Save to file.
protected
save(string $content, string $path) : mixed
Parameters
- $content : string
-
The minified data
- $path : string
-
The path to save the minified data to
Tags
shortenBools()
Replaces true & false by !0 and !1.
protected
shortenBools(string $content) : string
Parameters
- $content : string
Return values
stringstr_replace_first()
protected
static str_replace_first(mixed $search, mixed $replace, mixed $subject) : mixed
Parameters
- $search : mixed
- $replace : mixed
- $subject : mixed
stripComments()
Strip comments from source code.
protected
stripComments() : mixed
stripMultilineComments()
Both JS and CSS use the same form of multi-line comment, so putting the common code here.
protected
stripMultilineComments() : mixed
stripWhitespace()
Strip whitespace.
protected
stripWhitespace(string $content) : string
We won't strip all whitespace, but as much as possible. The thing that we'll preserve are newlines we're unsure about. JavaScript doesn't require statements to be terminated with a semicolon. It will automatically fix missing semicolons with ASI (automatic semi- colon insertion) at the end of line causing errors (without semicolon.)
Because it's sometimes hard to tell if a newline is part of a statement that should be terminated or not, we'll just leave some of them alone.
Parameters
- $content : string
-
The content to strip the whitespace for
Return values
stringwriteToFile()
Attempts to write $content to the file specified by $handler. $path is used for printing exceptions.
protected
writeToFile(resource $handler, string $content[, string $path = '' ]) : mixed
Parameters
- $handler : resource
-
The resource to write to
- $content : string
-
The content to write
- $path : string = ''
-
The path to the file (for exception printing only)