How to Create Custom Libraries to be Composer Managed for a Drupal Project

Back Story

Recently, I have been working on migrating a Drupal site for 8 to 9. After starting the migration, I found out the site was using the Ludwig module as a composer alternative for handling PHP libraries. This started the path into converting the Drupal 8 site into a properly structured composer managed site, which I followed Pantheon's documentation on Converting a Standard Drupal 8 Site to a Composer Managed Site.

After following the documentation and properly pushing the newly composer managed site to the live environment, I ran into an issue with trying to add/update custom libraries. Normally, with Pantheon I would use SFTP or git to add/update any changes to the custom libraries, but after switching this site to be composer managed, these methods seemed to not work properly anymore. Anytime I would try to add the changes these changes wouldn't show up or stick in the environment.

This issue with the new composer managed site forced the idea of taking the custom libraries and creating GitHub repository's for them and then using composer to pull-in and manage the libraries.

How to Create Custom Libraries to be Composer Managed for a Drupal Project

This will be a step-by-step guide from creating a project in Github to adding the information needed in your composer.json file to properly pull your repository into the libraries directory of your Drupal project.

Step 1 - Creating a Github Repository

In your local file directory you will want to run the following commands below

git init
git add .
git commit -m "Initial commit"
git branch -M master
git push

Step 2 - Adding the repository information in the composer.json file

{
  "type": "package",
  "package": {
      "name": "drupal/name-of-repo",
      "version": "1.0.0",
      "type":"drupal-library",
      "source": {
          "url": "https://github.com/greeottd/name-of-repo",
          "type": "git",
          "reference": "master"
      }
  }
},

Step 3 - List all projects in the require object in the composer.json file

"require": {
        ...
        "greeottd/name-of-repo": "1.0.0",
        ...
    },

Step 4 - Run

composer update

Conclusion

After following these step you will have created a Github repository, added all the necessary information need to access that repository in your composer.json, and you will have used composer to pull the projects into your environment.

Lastly, please note this process is an easy personal way to host your custom Drupal libraries with composer. In order to create and publish a composer package you might want to look further into the steps needed to be taken in doing so here.