Jungle Ran

Blog/Bloglet about Drupal, Ops, and mORe

Drulibs.com: an easier way managing Drupal 3rd-party FE libraries with Composer

Submitted by jungle on Mon, 12/21/2020 - 23:50

Currently, no recommended/better way for 3rd party FE libraries management with Composer in the Drupal land. But there is a ~4 years old issue: Implement core management of 3rd-party FE libraries, from which we can see the efforts from the community.

Here, I am going to recommend you Drulibs (DRUpal LIBrarieS) -- a custom Composer repository built with Satis for 3rd party FE libraries used by Drupal contributed projects (modules/themes/distributions) and YOUR PROJECTS. The official site is drulibs.com powered by Github Pages

How to use it?

Add the following lines to your project's root composer.json file

  1. {
  2. "repositories": [{
  3. "type": "composer",
  4. "url": "https://drulibs.com"
  5. }]
  6. }

After adding the above lines, your root composer.json file may look like:

  1. {
  2. ...
  3. "type": "project",
  4. "repositories": [
  5. {
  6. "type": "composer",
  7. "url": "https://packages.drupal.org/8"
  8. },
  9. {
  10. "type": "composer",
  11. "url": "https://drulibs.com"
  12. }
  13. ],
  14. "require": {
  15. ...


Next, just add the library/libraries by Composer as normal. For example:

composer require drulibs-itsjavi/fontawesome-iconpicker


composer require drulibs-itsjavi/fontawesome-iconpicker:3.2.0

Yes, all library names are prepended with drulibs. Don't get yourself confused, that's the naming convention followed, the pattern is drulibs-[AUTHOR-NAME]/[REPO-NAME]. All characters are in lowercase.

How it works?

As mentioned, it's a custom Composer repository built with Satis, which means it's a static repository, all packages/libraries available are added manually. What if the libraries you want are not there? Let's add it! PRs are always WELCOME! The repository is https://github.com/drulibs/drulibs. Taking Add drulibs-itsjavi/fontawesome-iconpicker as an example of how to add a new library roughly:

  1. Add the custom package to statis.json
  1. "itsjavi/fontawesome-iconpicker:3.2.0": {
  2. "type": "package",
  3. "package": {
  4. "name": "drulibs-itsjavi/fontawesome-iconpicker",
  5. "version": "3.2.0",
  6. "type": "drupal-library",
  7. "dist": {
  8. "url": "https://github.com/itsjavi/fontawesome-iconpicker/archive/3.2.0.zip",
  9. "type": "zip"
  10. }
  11. }
  12. },
  1. Add the newly added package to the Index section in README.md
  2. Run composer build to build the repository

Pros and Cons

  • Add the custom repository once only, It's easy to use if the library exists on drulibs.com, just like managing other drupal modules/themes. The more people using it and contributing to it, the more libraries available and the easier to use. If the custom Repository could be added into Drupal core's composer.json file, the only limitation is whether the expected library exists on drulibs.com or not and all you should do is to request a PR for helping others from this limitation.
  • Unlike using asset-packagist.org, no extra packages required, and no touch to the extra section of composer.json in general, the required composer/installers package is included in drupal/drupal/Drupal core, and the drupal-library package is supported by default. Meanwhile, there is an Index section in README listed all packages available and their related project(s), which could help to choose the right library used by contributed modules/themes/distributions. Compared with asset-packagist.org, Drulibs does not include a lot of libraries, however, to me, it's ok having fewer packages and adding new ones when necessary. As the fact is that about 10 - 30 3rd-party FE libraries being used only in a general project.
  • If the module uses a lot of 3rd party FE libraries, for example, the Webform module, it's not recommended to use Drulibs, unless the module maintainer(s) adopted it one day. Personally, for managing libraries used by Webform, using composer-merge-plugin is prefered (see How to use composer to install libraries for the Webform module ). So a combination of using Drulibs and composer-merge-plugin might be better at the moment. (Note: for composer 2 users want to use the composer-merge-plugin, see the workaround here: Add support for Composer v2 #189)




©2021 ranqiangjun.com. All rights reserved.