Nov 08, 2019 in Drupal 8, Snippets

A common issue you would have when using javascript plug-ins is that you do not want them to be added to your version control. You would want to add it to the main composer.json file that has to donwload the library for you. Here is how the solution looks like.

In your composer.json add the following

    "repositories": [
        {
            "type": "composer",
            "url": "https://packages.drupal.org/8"
        },
        {
            "type": "package",
            "package": {
                "name": "harvesthq/chosen",
                "version": "1.8.7",
                "type":"drupal-library",
                "source": {
                    "url": "https://github.com/harvesthq/chosen.git",
                    "type": "git",
                    "reference": "master"
                }
            }
        },
  ]

After that, you can require it:

composer require harvesthq/chosen

This would recognize the name of the package as not coming from packagist. Make sure you use the composer installers library:

 "composer/installers": "^1.0"
    

This will look into your installer-paths under the "extra" section of your composer.json file:

        "installer-paths": {
            "web/core": ["type:drupal-core"],
            "web/libraries/{$name}": ["type:drupal-library"],
            "web/modules/contrib/{$name}": ["type:drupal-module"],
            "web/profiles/contrib/{$name}": ["type:drupal-profile"],
            "web/themes/contrib/{$name}": ["type:drupal-theme"],
            "drush/Commands/{$name}": ["type:drupal-drush"]
        },

Right there it finds it has to download the library and put it in the web/libraries folder.

A last thing, because you now manage your libraries with composer now, add the web/libaries folder to your .gitignore file.