yii2: Cannot override yii\web\JqueryAsset for asset/compress
Hello friends,
I’m trying to compress assets with the following command:
./yii asset/compress frontend/config/assets-combine.php frontend/config/assets-prod.php
Here is the content of frontend/config/assets-combine.php:
<?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' => 'java -jar frontend/nodejs/node_modules/google-closure-compiler/compiler.jar --js {from} --js_output_file {to}',
// Adjust command/callback for CSS files compressing:
'cssCompressor' => 'java -jar frontend/nodejs/node_modules/yuicompressor/build/yuicompressor-2.4.8.jar --type css {from} -o {to}',
// The list of asset bundles to compress:
'bundles' => [
'yii\web\JqueryAsset',
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
'yii\bootstrap\BootstrapPluginAsset',
'frontend\assets\ExternalAsset',
'frontend\assets\BowerAsset',
'frontend\assets\AppAsset',
],
// Asset bundle for compression output:
'targets' => [
'frontend.external' => [
'class' => 'yii\web\AssetBundle',
'basePath' => '@webroot/assets',
'baseUrl' => '@web/assets',
'js' => 'combined/js/external-{hash}.js',
'css' => 'combined/css/external-{hash}.css',
'depends' => [
'yii\web\JqueryAsset',
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
'yii\bootstrap\BootstrapPluginAsset',
'frontend\assets\ExternalAsset',
],
],
'frontend.vendor' => [
'class' => 'yii\web\AssetBundle',
'basePath' => '@webroot/assets',
'baseUrl' => '@web/assets',
'js' => 'combined/js/vendor-{hash}.js',
'css' => 'combined/css/vendor-{hash}.css',
'depends' => [
'frontend\assets\BowerAsset'
],
],
'frontend.app' => [
'class' => 'yii\web\AssetBundle',
'basePath' => '@webroot/assets',
'baseUrl' => '@web/assets',
'js' => 'combined/js/app-{hash}.js',
'css' => 'combined/css/app-{hash}.css',
'depends' => [
'frontend\assets\AppAsset',
],
],
],
// Asset manager configuration:
'assetManager' => [
'basePath' => '@webroot/assets',
'baseUrl' => '@web',
'bundles' => [
'yii\web\JqueryAsset' => [
'sourcePath' => null,
'basePath' => null,
'js' => ['//cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js']
]
],
'converter' => [
'class'=>'nizsheanez\assetConverter\Converter',
'force' => false, // true : If you want convert your sass each time without time dependency
'destinationDir' => 'compiled', //at which folder of @webroot put compiled files
'parsers' => [
// ...
'scss' => [ // file extension to parse
'class' => 'nizsheanez\assetConverter\Scss',
'output' => 'css', // parsed output file type
'options' => [ // optional options
'enableCompass' => true, // default is true
'importPaths' => ['@webroot/scss/'], // import paths, you may use path alias here,
// e.g., `['@path/to/dir', '@path/to/dir1', ...]`
'lineComments' => false, // if true — compiler will place line numbers in your compiled output
'outputStyle' => 'nested', // May be `compressed`, `crunched`, `expanded` or `nested`,
// see more at http://sass-lang.com/documentation/file.SASS_REFERENCE.html#output_style
],
],
],
],
],
];
Everything works fine except overriding the jQuery asset. This should be a trivial thing to override yii\web\JqueryAsset but I spent 2+ hours and still didn’t make it work.
In the resulting frontend/config/assets-prod.php I got:
[
'yii\\web\\JqueryAsset' => [
'sourcePath' => null,
'js' => [],
'css' => [],
'depends' => [],
]
]
I tried to debug the AssetController, AssetManager and AssetBundle. On this line ( https://github.com/yiisoft/yii2/blob/2.0.6/framework/console/controllers/AssetController.php#L191 ) I have the following object in $bundles variable:
[yii\web\JqueryAsset] => yii\web\JqueryAsset Object
(
[sourcePath] =>
[js] => Array
(
[0] => //cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js
)
[basePath] =>
[baseUrl] =>
[depends] => Array
(
)
[css] => Array
(
)
[jsOptions] => Array
(
)
[cssOptions] => Array
(
)
[publishOptions] => Array
(
)
)
And on this line ( https://github.com/yiisoft/yii2/blob/2.0.6/framework/console/controllers/AssetController.php#L192 ) I have the following object in $targets variable:
[yii\web\JqueryAsset] => yii\web\JqueryAsset Object
(
[sourcePath] => /home/.../vendor/bower/jquery/dist
[js] => Array
(
[0] => jquery.js
)
[basePath] =>
[baseUrl] =>
[depends] => Array
(
)
[css] => Array
(
)
[jsOptions] => Array
(
)
[cssOptions] => Array
(
)
[publishOptions] => Array
(
)
)
I believe that’s because Yii::createObject ( https://github.com/yiisoft/yii2/blob/2.0.6/framework/console/controllers/AssetController.php#L401 ) doesn’t care about anything from the $bundles’ object.
Maybe this is some kind of bug or I just misunderstood something, but I have no idea how to solve this correct.
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 19 (12 by maintainers)
Huge reports are always hard to read. Could you reduce your report to required minimum for reproducing?