yii2: Compressing assets is not working as expected.

I am using the extension scroll pager:

"require": { "kop/yii2-scroll-pager": "dev-master", },

I created an asset bundle to compress the asset kop\y2sp\assets\InfiniteAjaxScrollAsset and i have only 1 target ‘all’ in my compress script.

'all' => [ 'class' => 'yii\web\AssetBundle', 'basePath' => '@webroot', 'baseUrl' => '@web', 'js' => 'min/js/all{hash}.js', 'css' => 'min/css/all{hash}.css', ],

When i run my application, the compressed js is included but the individual js present in the asset are also included.

FYI: This seems to be happening for widgets only. I tried to compress 2 of kartik assets into 1 file and it is including both the compressed and the original files.

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 18 (9 by maintainers)

Most upvoted comments

Here is an step-by-step of what i did to simulate the problem:

  • Install Yii2: composer: create-project --prefer-dist --stability=dev yiisoft/yii2-app-basic basic
  • Inside the application, run yii asset/template assets.php
  • Configure the generated file as the following (In my case I’m using gulp to compress the files):
<?php
/**
 * Configuration file for the "yii asset" console command.
 */

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

return [
    // Adjust command/callback for JavaScript files compressing:
    'jsCompressor' => 'gulp assetScripts --src {from} --dist {to}',
    // Adjust command/callback for CSS files compressing:
    'cssCompressor' => 'gulp assetStyles --src {from} --dist {to}',
    // The list of asset bundles to compress:
    'bundles' => [
        'app\assets\AppAsset',
        'yii\web\YiiAsset',
        'yii\web\JqueryAsset',
    ],
    // Asset bundle for compression output:
    'targets' => [
        'all' => [
            'class' => 'yii\web\AssetBundle',
            'basePath' => '@webroot/assets',
            'baseUrl' => '@web/assets',
            'js' => 'all.js',
            'css' => 'all.css',
        ],
    ],
    // Asset manager configuration:
    'assetManager' => [
        'basePath' => '@webroot/assets',
        'baseUrl' => '@web/assets',
    ],
];
  • In config/web.php add the following component:
<?php
$bundles = require(__DIR__ . '/assets-prod.php');

$config = [
    ...
    components => [
        'assetManager' => [
            'bundles' => $bundles,
            'appendTimestamp' => true,
        ],
        ...
  • Now, run yii asset assets.php config/assets-prod.php to compress.

If you check the assets-prod.php file, you will see that the compressing went fine and all assets are empty and depending of all.

But, if I overwrite the AppAsset to look like this:

<?php
namespace app\assets;

use yii\web\AssetBundle;

class AppAsset extends AssetBundle
{
    public $depends = [
        'yii\web\YiiAsset',
        'yii\bootstrap\BootstrapAsset',
    ];

    public function init()
    {
        $this->basePath = '@webroot';
        $this->baseUrl = '@web';
        $this->css = ['css/site.css'];

        parent::init();
    }
}

The compressing will still work, however in my application I can see the original site.css being loaded:

compress

So, my question is: Is this the expected behavior of the Asset? Shouldn’t, after compressed, the asset never being able to rewrite their files somehow?

Or you guys think that using the init() method to add properties is a bad practice for this class? As i said, I’m using an extension that have this situation and I already opened an issue there. I just would like to hear what you guys think about it or if i did something wrong in the process described.