Simple CSS & JS minify library.
This project is port of YUI CSSmin and JShrink.
Via Composer
composer require asika/minify
Use Factory
use Asika\Minifier\MinifierFactory;
$cssMinify = MinifierFactory::create('css');
$jsMinify = MinifierFactory::create('js');
Directly new object:
$minify = new \Asika\Minifier\JsMinifier;
// OR
$minify = new \Asika\Minifier\CssMinifier;
Use object
use Asika\Minifier\MinifierFactory;
$minify = MinifierFactory::create($type);
$minify->addFile($path); // Add file path
$minify->addContent($path); // Add text content
// Get minify content
$minify->minify();
// To file
$minify->toFile($minifyFile);
Use static class to process single file:
$minified = \Asika\Minifier\JsMinifier::process($fileOrContent);
$minified = \Asika\Minifier\CssMinifier::process($fileOrContent);
Use flaggedComments = false
options to remove /*!
comments:
// Add options for every file
$minify->addFile($path, ['flaggedComments' => false]);
// OR
$minify->addContent($content, ['flaggedComments' => false]);
// OR
\Asika\Minifier\JsMinifier::process($fileOrContent, ['flaggedComments' => false]);
// Use constant as key
$minify->addFile($path, [
\Asika\Minifier\AbstractMinifier::FLAGGED_COMMENTS => false
]);
Add uri_rewrite
option for every file:
// This css in HTML is `{PUBLIC_ROOT}/foo/bar/yoo.css`
$minify->addFile(
$file,
[
'uri_rewrite' => [ // OR \Asika\Minifier\CssMinifier::URI_REWRITE
'current_dir' => 'foo/bar/yoo.css',
'doc_root' => $_SERVER['DOCUMENT_ROOT'] // [Optional]
]
]
);
Or use UriRewriter
class to rewrite css content:
$css = \Asika\Minifier\CSS\UriRewriter::rewrite(
$content,
'foo/bar/yoo.css'
);