Hello everyone,
while working on 2 Yii2 apps I discovered a few strange things about AssetBundles and the asset command.
First I had a problem with dependencies in one of my custom AssetBundles. One target is "all" and then I have another target which has its own AssetBundle with dependencies. But these dependencies of the second bundle are put into "all".
(all is the general bundle for all pages)
With a little bit of experimenting and reading source code I discovered I could change the config file for the asset console command to list dependencies of my specific bundles.
So it looks like this:
return [
..
'targets' => [
..
'sec' => [
'class' => AssetBundle::class,
'basePath' => '@webroot/assets',
'baseUrl' => '@web/assets',
'js' => 'js/sec-{hash}.js',
'css' => 'css/sec-{hash}.css',
'depends' => [
SecAsset::class,
ActiveFormAsset::class,
ValidationAsset::class,
],
],
],
];
Shouldn’t be necessary in my opinion (dependencies still need to be listed in SecAsset)… but I can live with that.
Now I had to create a third (thr) bundle with the same dependencies and I get the error message:
Error: Target 'sec' and 'thr' cannot contain the bundle 'yii\validators\ValidationAsset' at the same time.
dependencies in my assets look like this:
class SecAsset extends AssetBundle
{
..
public $depends = [
AppAsset::class,
ActiveFormAsset::class,
ValidationAsset::class,
];
}
class ThrAsset extends AssetBundle
{
..
public $depends = [
AppAsset::class,
ActiveFormAsset::class,
ValidationAsset::class,
];
}
Now my question: Is there some sort of workaround? Or am I doing it wrong? How can I use the same assets in two or more bundles? And maybe, is it a bug, that dependencies in secondary bundles get loaded into the general "all" bundle?
If this is too confusing I will try to provide a better example (complete project) during the weekend.
Thanks in advance,
hofrob