yii2: Asset bundles approach is wrong and lead to Yii 2 extensions mess and lot of troubles

For example we have foo\yii2-gallery-widget. It require js libs:

  • jquery-gallery
  • doT

We have 2 Asset Bundles: foo\gallery\JqueryGalleryAsset and foo\gallery\DotAsset.

Second extension we have bar\yii2-file-uploader-widget. It requre js libs:

  • jquery-file-uploader
  • doT

We have 2 Asset Bundles: bar\uploader\JqueryFileUploaderAsset and bar\uploader\DotAsset.

Using this widgets in one page will lead to critical js error, because doT will be linked 2 times. Trying to use asset converter will lead to critical js error, because it will use doT 2 times. Trying to override doT bundle absolutte FAIL, because we can override 1 bundle, while there we have 2.

Because all of it troubles current Yii 2 situation is: NOT use Yii 2 extensions at all, because it just DUNGEROUS. In middle/big application you will be faced with all described troubles.

Also as you see this is simple example where trouble come. In real world extensions which requre 2 - 3 js libraries possibility of such bundle collisions is 99%.

@qiangxue It is not yet too late (there is no release yet) to totally rethink asset bundles approach. Because if it will go to release prepare to situation when yii 2 extensions will not work together and no one will use them.

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 76 (75 by maintainers)

Most upvoted comments

What you should do is as follows:

  1. Create an asset bundle for each of the widgets.
  2. If you find these third party libraries available as independent asset bundles, declare them in the dependencies of your widget asset bundles.
  3. If not, create your own copy of such asset bundles encapsulating the third party libraries and list them as the dependencies of your widget asset bundles.

Now if 2 is the case, there should be no problem. If 3 is the case, in the app that uses the widgets, you can customize the widget asset bundles’ dependencies to depend on unique 3rd party asset bundles.

Yes, case 3 is not good, but you will face similar problem even if you do not use asset bundle (e.g. one widget bundles its own version of 3rd party lib because it is not using bower or for some other reason).

If Composer can support bower (we already have some ideas), the problem of case 3 can be largely solved.