D
D
Dmitry Kim2016-05-31 08:46:07
Yii
Dmitry Kim, 2016-05-31 08:46:07

yii asset/template command in YII?

Trying to tame JquerySparkline . The problem is that there is no ready-built sparkline-js file on the github in the native repository (strange, but the author believes that this is better and publishes only individual modules that everyone should build for themselves, well, or all together).
Writing a resource file for Sparkline

common/assets/sparkline/Gwatts_JquerySparklineAsset_combine.php
:
class Gwatts_JquerySparklineAsset_combine extends AssetBundle
    {
        public $sourcePath = '@vendor/gwatts/jquery.sparkline/src';
        public $js         = [
            'header.js',
            'defaults.js',
            'utils.js',
            'simpledraw.js',
            'rangemap.js',
            'interact.js',
            'base.js',
            'chart-line.js',
            'chart-bar.js',
            'chart-tristate.js',
            'chart-discrete.js',
            'chart-bullet.js',
            'chart-pie.js',
            'chart-box.js',
            'vcanvas-base.js',
            'vcanvas-canvas.js',
            'vcanvas-vml.js',
            'footer.js',
        ];
    }

I will note right away that it will not work, because js-pieces in reality are unrelated pieces and the browser does not eat them. It's good that YII has a package builder. I go to the console and write
yii asset/template common/assets/sparkline/Gwatts_JquerySparklineAsset_config.php
, a file is created, I go into it and configure:
// я не хочу сразу публиковать собранный файл на прод, а хочу оставить его внутри проекта, чтобы потом подключать к двум поддоменам поотдельности
// файл будет лежать в папке common/assets/sparkline/sparkline.js
Yii::setAlias('@webroot', __DIR__.'/sparkline');
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'       => [
            // этот файл содержит список тех самых js-кусков кода, которые необходимо собрать
            // описание его ниже
            common\assets\Gwatts_JquerySparklineAsset_combine::className(),
        ],
        // Asset bundle for compression output:
        'targets'       => [
            'Gwatts_JquerySparklineAsset' => [
                'class'      => common\assets\Gwatts_JquerySparklineAsset_combine::className(),
                'basePath'   => '@webroot',
                'baseUrl'    => '@web',
                'js'         => 'sparkline.js',
            ],
        ],
        // Asset manager configuration:
        'assetManager'  => [
            'basePath' => '@webroot',
            'baseUrl'  => '@web',
        ],
    ];

launching
yii asset/compress common/assets/sparkline/Gwatts_JquerySparklineAsset_config.php common/assets/sparkline/Gwatts_JquerySparklineAsset_bundle.php
. As a result, a common/assets/sparklinefolder 3c99cc05with all our js pieces and a sparkline.js. It also creates a file Gwatts_JquerySparklineAsset_bundle.phpwith the following content:
return [
    'Gwatts_JquerySparklineAsset' => [
        'class' => 'common\\assets\\Gwatts_JquerySparklineAsset_combine',
        'basePath' => '@webroot',
        'baseUrl' => '@web',
        'js' => [
            'sparkline.js',
        ],
        'css' => [],
    ],
    'common\\assets\\Gwatts_JquerySparklineAsset_combine' => [
        'sourcePath' => null,
        'js' => [],
        'css' => [],
        'depends' => [
            'Gwatts_JquerySparklineAsset',
        ],
    ],
];

We don't really need it. Again, I only needed to assemble sparkline into one file, so we create another resource file Gwatts_JquerySparklineAsset:
class Gwatts_JquerySparklineAsset extends AssetBundle
    {
        public $sourcePath = '@common/assets/sparkline';
        public $js         = [
            'sparkline.js',
        ];
    }

That's it, and you can cling to the project anywhere.
Now the questions are:
1. Is it possible to somehow simplify the whole thing?
2. Was it necessary to create so many files?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2016-05-31
@webinar

Have you read? How to build and minify CSS and JS in Yii2?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question