Skip to content

Instantly share code, notes, and snippets.

@df-a
Created October 11, 2021 14:42
Show Gist options
  • Save df-a/b2bf1e11c4eadf4430ed1662cf2e6930 to your computer and use it in GitHub Desktop.
Save df-a/b2bf1e11c4eadf4430ed1662cf2e6930 to your computer and use it in GitHub Desktop.
Get composer to accept external Javascript libraries hosted on packagist

Composer (recommended) If you would like to install the select2 library with composer, you probably used the drupal composer template to set up your project. It's recommended to use asset-packagist to install JavaScript libraries. So you will need to add the following to your composer.json file into the repositories section:

    {
        "type": "composer",
        "url": "https://asset-packagist.org"
    }

It's also needed to extend the 'installer-path' section:

    "web/libraries/{$name}": [
        "type:drupal-library",
        "type:bower-asset",
        "type:npm-asset"
    ],

And add a new 'installer-types' section next to the 'installer-path' in the 'extra' section:

    "installer-types": [
      "bower-asset",
      "npm-asset"
    ],

Be mindful of comma's! After this you can install the library with composer require oomphinc/composer-installers-extender npm-asset/select2 and the library will be downloaded into the libraries folder.

@df-a
Copy link
Author

df-a commented Oct 11, 2021

Expanded example

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

@df-a
Copy link
Author

df-a commented Oct 11, 2021

Repositories example

  "repositories": {
    "drupal": {
      "type": "composer",
      "url": "https://packages.drupal.org/8"
    },
    "javascript": {
      "type": "composer",
      "url": "https://asset-packagist.org"
    },
    "magnific-popup": {
      "type": "vcs",
      "url": "https://github.com/dimsemenov/magnific-popup"
    },
    "chosen": {
      "type": "vcs",
      "url": "https://github.com/harvesthq/chosen"
    },
    "spectrum": {
      "type": "vcs",
      "url": "[email protected]:df-a/spectrum.git"
    },
    "slick": {
      "type": "vcs",
      "url": "[email protected]:df-a/slick.git"
    },
    "jquery-easing": {
      "type": "vcs",
      "url": "[email protected]:df-a/jquery.easing.git"
    },
    "blazy": {
      "type": "vcs",
      "url": "[email protected]:df-a/blazy.git"
    },
    "novacancy": {
      "type": "vcs",
      "url": "[email protected]:df-a/novacancy.js.git"
    },
    "editormd": {
      "type": "vcs",
      "url": "[email protected]:unicorn-fail/editor.md.git"
    },
    "select2": {
      "type": "vcs",
      "url": "[email protected]:select2/select2.git"
    }
  },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment