A
A
Artyom Blinkov2021-05-14 16:34:54
Yii
Artyom Blinkov, 2021-05-14 16:34:54

Compressing CSS and JS resources in Yii2 by group?

Good afternoon, the question is the following:

I compress resources according to the instructions, such a config:

/**
 * Configuration file for the "yii asset" console command.
 */

// In the console environment, some path aliases may not exist. Please define these:
Yii::setAlias('@webroot', realpath(__DIR__ . '/../web'));
Yii::setAlias('@web', '/');

return [
    // Adjust command/callback for JavaScript files compressing:
    'jsCompressor' => 'java -jar optimize/closure-compiler-v20210505.jar --js {from} --js_output_file {to}',
    // Adjust command/callback for CSS files compressing:
    'cssCompressor' => 'java -jar optimize/yuicompressor.jar --type css {from} -o {to}',
    // Whether to delete asset source after compression:
    'deleteSource' => true,
    // The list of asset bundles to compress:
    'bundles' => [
        'yii\web\YiiAsset',
  /*другие бандлы*/
    ],
    // Asset bundle for compression output:
   'targets' => [
        'app\\assets\\CompressedAsset' => [
            'class' => 'yii\web\AssetBundle',
            'basePath' => '@webroot',
            'baseUrl' => '@web',
            'js' => 'js/main-{hash}.js',
            'css' => 'css/main-{hash}.css',
        ],
    ],

    // Asset manager configuration:
    'assetManager' => [
        'basePath' => '@webroot/assets',
        'baseUrl' => '@web/assets',
    ],
];


Everything works fine.

But, when trying to group resources like this:

'targets' => [
        'app\\assets\\MainAsset' => [
            'class' => 'yii\web\AssetBundle',
            'basePath' => '@webroot',
            'baseUrl' => '@web',
            'js' => 'js/main-{hash}.js',
            'css' => 'css/main-{hash}.css',
            'depends' => [
                'yii\web\YiiAsset',
            ],
        ],
        'app\\assets\\CalcAsset' => [
            'class' => 'yii\web\AssetBundle',
            'basePath' => '@webroot',
            'baseUrl' => '@web',
            'js' => 'js/main-{hash}.js',
            'css' => 'css/main-{hash}.css',
            'depends' => [
                'app\assets\AppAsset'
            ],
        ],
        'app\\assets\\ContactAsset' => [
            'class' => 'yii\web\AssetBundle',
            'basePath' => '@webroot',
            'baseUrl' => '@web',
            'js' => 'js/main-{hash}.js',
            'css' => 'css/main-{hash}.css',
            'depends' => [
                'yii\widgets\ActiveFormAsset',
                'yii\validators\ValidationAsset',
            ],
        ],
    ],


At the stage of file creation, I catch an error in the console:

Creating new bundle configuration...
Error: Invalid argument supplied for foreach()

Has anyone come across this? Where to dig?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artyom Blinkov, 2021-05-14
@Razor0077x


I figured out the Bundle that is cut on each page (Yii, Jqeury, etc.) must be specified without dependencies

'targets' => [
        'app\\assets\\CompressCalcAsset' => [
            'class' => 'yii\web\AssetBundle',
            'basePath' => '@webroot',
            'baseUrl' => '@web',
            'js' => 'js/calc-{hash}.js',
            'css' => 'css/calc-{hash}.css',
            'depends' => [
                'app\assets\CalcAsset',
            ],
        ],
        'app\\assets\\ContactAsset' => [
            'class' => 'yii\web\AssetBundle',
            'basePath' => '@webroot',
            'baseUrl' => '@web',
            'js' => 'js/contact-{hash}.js',
            'css' => 'css/contact-{hash}.css',
            'depends' => [
                'yii\widgets\ActiveFormAsset',
                'yii\validators\ValidationAsset',
            ],
        ],
        'app\\assets\\MainAsset' => [           //критический бандл
            'class' => 'yii\web\AssetBundle',
            'basePath' => '@webroot',
            'baseUrl' => '@web',
            'js' => 'js/main-{hash}.js',
            'css' => 'css/main-{hash}.css',
            'depends' => [],
        ],
    ],

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question