A
A
archangel_gavriil2015-10-16 23:49:42
Yii
archangel_gavriil, 2015-10-16 23:49:42

YII2 Assets minification and correct connection of CSS and JS to the project?

Hello!
Actually, this is the question: There is an AppAssets class on the YII 2 project, this class contains css and js files that should be included in the project.

class AppAsset extends AssetBundle
{
    public $sourcePath = '@app/assets/themes/';

    public $css = [
        'css/normalize.css',
        'css/template_2.css',
        'css/template.css',
        'css/roboto.css',
        'css/jquery.selectbox.css',
        'css/jquery.ui.css',
        'css/icheck.css'
    ];

    public $js = [
        'js/vendors/jquery-1.7.2.min.js',
        'js/ui/jquery.ui.core.js',
        'js/ui/jquery.ui.widget.js',
        'js/ui/jquery.ui.autocomplete.js',
        'js/ui/jquery.ui.dialog.js',
        'js/vendors/jquery.maskedinput.js',
        'js/vendors/jquery.selectbox-0.2.min.js',
        'js/vendors/jquery.html5-placeholder-shim.js',
        'js/vendors/jquery.icheck.min.js',
        'js/vendors/underscore-min.js',
        'themes/smart_admin/theme/js/plugin/jquery-validate/jquery.validate.min.js',
        'js/public/common.js',
        'js/page/evac_request.js',
        'js/page/buy_cards.js'
    ];

    public $jsOptions = [
        'position' => \yii\web\View::POS_HEAD
    ];

    public $cssOptions = [
        'position' => \yii\web\View::POS_HEAD
    ];

    public $depends = [
        'yii\web\YiiAsset',
    ];

    public $publishOptions = [
        'forceCopy' => true
    ];
}

Next, there is a configured build file, compressor jar files are at the root (compiler.jar and yuicompressor.jar)
Yii::setAlias('@webroot', __DIR__ . '/../web');
Yii::setAlias('@web', '/');

return [
    // Adjust command/callback for JavaScript files compressing:
    'jsCompressor' => 'java -jar compiler.jar --js {from} --js_output_file {to}',
    // Adjust command/callback for CSS files compressing:
    'cssCompressor' => 'java -jar yuicompressor.jar --type css {from} -o {to}',

    // The list of asset bundles to compress:
    'bundles' => [
        'app\assets\AppAsset',
        'yii\web\YiiAsset',
        'yii\web\JqueryAsset',
    ],

    // Asset bundle for compression output:
    'targets' => [
        'app\assets\AppAsset' => [
            'basePath' => '@webroot/',
            'baseUrl' => '@web/',

            'css' => 'css/all-front-{hash}.css',
            'js' => 'js/all-front-{hash}.js',

            'depends' => [
                // Include all assets shared between 'backend' and 'frontend'
                'app\assets\AppAsset',
                'yii\web\YiiAsset',
                'yii\web\JqueryAsset'
            ],
        ],
    ],
    // Asset manager configuration:
    'assetManager' => [
        'basePath' => '@webroot/assets',
        'baseUrl' => '@web/assets',
    ],
];

Compilation is started with the command
sudo ./yii asset config/assets.php assets/assets_min.php

The necessary minified files appear where they should
  1. all-front-e9bcdd242e24b630ddff6b382bbc843c.css
  2. all-front-1bf96460d930621ec14c72db351fefc7.js

However, oddly enough, in the final assets_min.php, these minified files are not included, and all project files are dumped in their place:
return [
    'app\\assets\\AppAsset' => [
        'class' => 'app\\assets\\AppAsset',
        'basePath' => '@webroot/',
        'baseUrl' => '@web/',
        'js' => [
            'js/vendors/jquery-1.7.2.min.js',
            'js/ui/jquery.ui.core.js',
            'js/ui/jquery.ui.widget.js',
            'js/ui/jquery.ui.autocomplete.js',
            'js/ui/jquery.ui.dialog.js',
            'js/vendors/jquery.maskedinput.js',
            'js/vendors/jquery.selectbox-0.2.min.js',
            'js/vendors/jquery.html5-placeholder-shim.js',
            'js/vendors/jquery.icheck.min.js',
            'js/vendors/underscore-min.js',
            'themes/smart_admin/theme/js/plugin/jquery-validate/jquery.validate.min.js',
            'js/public/common.js',
            'js/page/evac_request.js',
            'js/page/buy_cards.js',
        ],
        'css' => [
            'css/normalize.css',
            'css/template_2.css',
            'css/template.css',
            'css/roboto.css',
            'css/jquery.selectbox.css',
            'css/jquery.ui.css',
            'css/icheck.css',
        ],
    ],
    'yii\\web\\JqueryAsset' => [
        'sourcePath' => null,
        'js' => [],
        'css' => [],
        'depends' => [
            'app\\assets\\AppAsset',
        ],
    ],
    'yii\\web\\YiiAsset' => [
        'sourcePath' => null,
        'js' => [],
        'css' => [],
        'depends' => [
            'yii\\web\\JqueryAsset',
            'app\\assets\\AppAsset',
        ],
    ],
];

The question is:
why are paths to minified files not generated in assets_min.php, but just a general list of files?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2015-10-17
@archangel_gavriil

And if so?

...
// Asset bundle for compression output:
    'targets' => [
        'frontend' => [
            'class' => 'yii\web\AssetBundle',
            'basePath' => '@webroot/',
            'baseUrl' => '@web/',

            'css' => 'css/all-front-{hash}.css',
            'js' => 'js/all-front-{hash}.js',

            'depends' => [
                // Include all assets shared between 'backend' and 'frontend'
                'app\assets\AppAsset',
                'yii\web\YiiAsset',
                'yii\web\JqueryAsset'
            ],
        ],
    ],
...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question