PHP code example of nedarta / php-assetopt

1. Go to this page and download the library: Download nedarta/php-assetopt library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nedarta / php-assetopt example snippets


use nedarta\AssetOpt\Minifier;

$compressedCss = (new Minifier)->run($css);

use nedarta\AssetOpt\JsStrip;

$compressedJs = (new JsStrip)->compress($js);

use nedarta\AssetOpt\ImportResolver;
use nedarta\AssetOpt\Minifier;

// css with @import at-rules
$css = file_get_contents('path/to/styles.css');

// resolve @import at-rules: $baseDir for relative @import urls
$css = (new ImportResolver)->resolve($css, 'path/to');

// minify resolved css
$compressedCss = (new Minifier)->run($css);

use nedarta\AssetOpt\Minifier as CSSmin;

$cssmin = new Minifier([
    /* Raise PHP limits */
    'raise_php_limits' => true
]);

$cssmin->setMaxExecutionTime(30); // Set maximum execution time to 30 seconds by default
$cssmin->setPcreBacktrackLimit(2000000); // Set PCRE backtrack limit to 2 x 2 by default
$cssmin->setPcreRecursionLimit(500000); // Set PCRE recursion limit to 5 x 0.5 by default
$cssmin->keepSourceMapComment(); // Keeps sourcemap special comment in the output by default
$cssmin->removeImportantComments(); // Removes !important comments in the output by default
$cssmin->setLineBreakPosition(500); // Set line break position

$compressedCss = $cssmin->run($css);

/**
 * Simple Yii2 AssetBundle with minification of contents
 * of its $css and $js files in the production environment.
 */
class AppAsset extends AssetBundle
{
    public $sourcePath = '@app/assets';

    public $css = [
        'css/app.css',
        'https://fonts.googleapis.com/css?family=PT+Serif:400,700,400italic,700italic',
    ];

    public $js = [
        'js/app.js',
    ];

    public $depends = [
        \yii\web\JqueryAsset::class,
        \yii\bootstrap\BootstrapAsset::class,
    ];

    ...
}

'components' => [
    'assetManager' => [
        'forceCopy' => YII_DEBUG,
        'linkAssets' => true,
        // compress css and js assets of the app
        'compress' => !YII_DEBUG,
        'cssCompressor' => 'php vendor/bin/assetopt {from} -o {to}',
        'jsCompressor' => 'php vendor/bin/assetopt --type js {from} -o {to}',
    ],
],

'components' => [
    'assetManager' => [
        // ... the settings above can replace tools such as:
        'cssCompressor' => 'java -jar yuicompressor-2.4.8.jar --disable-optimizations --type css {from} -o {to}',
        'jsCompressor' => 'java -jar closure-compiler-v20250820.jar --js {from} --js_output_file {to} --warning_level=QUIET',
    ],
],