Problem/Motivation

There is no official way how Drupal modules can require npm or bower dependencies. There are many "wrong" tutorials on the web.
asset-packagist.org repository seems to be the solution, but it takes too long to explain all the changes required to configure that repository.
There should be an official, fully supported way, which is easy to use. Adding a composer script to enable the repository should be the first step.

Steps to reproduce

see the detailed explanation how to enable asset-packagist:
http://lightning.acquia.com/blog/round-your-front-end-javascript-librari...

Proposed resolution

Drupal module installation with composer (with external npm dependency) should be

composer run enable-asset-packagist
composer require drupal/modulename

Remaining tasks

Solution is to copy
https://git.drupalcode.org/project/lightning/-/blob/8.x-3.x/src/Composer/AssetPackagist.php
into directory
https://git.drupalcode.org/project/drupal/-/tree/9.2.x/core/lib/Drupal/Core/Composer

configure the script in composer.json (under scripts)

"enable-asset-packagist": "Drupal\\Core\\Composer\\AssetPackagist::execute",

Comments

Pasqualle created an issue. See original summary.

longwave’s picture

Do we even need PHP to do this?

If we add this to the templates we hopefully just need something like

composer config repositories.asset-packagist composer https://asset-packagist.org
composer config --json --merge extra.installer-types '["bower-asset", "npm-asset"]'
composer config --json --merge 'extra.installer-paths.web/libraries/{$name}' '["type:bower-asset", "type:npm-asset"]'
pasqualle’s picture

Status: Active » Closed (won't fix)

After a discussion on Drupal slack it seems, using the asset-packagist repository is not recommended and will not be an official solution,
reasons:
- using composer as npm replacement is a bad idea
- asset-packagist reliability is questionable. Currently seems unmaintained and still does not support composer 2.

The solution seems to be to make npm a requirement and use both package managers with Drupal.
Will try to make a new issue for that, as there is no issue nor documentation how this should work with Drupal.

pasqualle’s picture