php – Assets bundles in yii2 generate empty files-ThrowExceptions

Exception or error:

Asset bundle generate empty JavaScript and CSS files

namespace frontend\assets;

return [

    'bundles' => [
        'frontend\assets\AppAsset',

    ],

    'targets' => [
        'frontend\assets\AppAsset' => [
            'basePath' => 'e:/path/yii2.loc/www',
            'baseUrl' => '',
            'js' => 'js/{ts}.js',
            'css' => 'css/{ts}.css',
        ],
    ],

    'assetManager' => [
        'basePath' => 'e:/path/yii2.loc/www/assets',
        'baseUrl' => '',
    ],
];

config.php

return [

    'bundles' => [
        'frontend\assets\AppAsset',
    ],

    'targets' => [
        'frontend\assets\AppAsset' => [
            'basePath' => 'e:/path/yii2.loc/www',
            'baseUrl' => '',
            'js' => 'cache/{ts}.js',
            'css' => 'cache/{ts}.css',
        ],
    ],

    'assetManager' => [
        'basePath' => 'e:/path/yii2.loc/www/assets',
        'baseUrl' => '',
    ],
];

Then in console

yii asset e:\path\config.php e:\path\compressed.php
//compresed.php it's result file with name of compressed files

And in config

'assetManager' => [
    'bundles' => require dirname(__DIR__) . '/assets/compressed.php',
],

CSS and JavaScript files are in a directory:

e:/path/yii2.loc/www/css

And

e:/path/yii2.loc/www/js

Bundle generate empties to:

e:/path/yii2.loc/www/cache/css and e:/path/yii2.loc/www/cache/js

What did I do wrong?

How to solve:

Inside your config.php, try to config component ‘assetManager’ like as the following LOCs:

'components' => [
    'assetManager' => [
        'class' => 'yii\web\AssetManager', 
        'basePath' => 'YOUR_BASE_PATH' 
    ],  
],

Answer´╝Ü

You should set aliases @web and @webroot, because of this file will be use in console script. Then use aliases for setting basePath and baseUrl parameters

Check out for more info https://www.yiiframework.com/doc/guide/2.0/en/structure-assets

Leave a Reply

Your email address will not be published. Required fields are marked *