There is a library that has some CSS, js assets. I want to include this library and use its assets in Magento 2. I am prepared to write a module for this, but I don't know where to start.

I first went with the approach of copying the files to the lib/web directory, but when it went to Magento-cloud the code collapsed as we do not have write access to that folder. Then I stumbled upon the file 'vendor/magento/module-deploy/Source/Lib.php' and tried to recreate it. I was not able to place the files in the static directory and I got the error of 'Unable to resolve the source file'.

My code in the di.xml

<type name="Magento\Deploy\Source\SourcePool">
        <argument name="sources" xsi:type="array">
            <item name="customassets" xsi:type="object">*\*\Source\Assets</item>

Assets.php file


namespace **\Source;

use Magento\Deploy\Package\Package; use Magento\Deploy\Package\PackageFileFactory; use Magento\Deploy\Source\SourceInterface; use Magento\Framework\App\Utility\Files;

class Assets implements SourceInterface { const TYPE = 'customassets'; /** * @var Files / private $filesUtil; /* * @var PackageFileFactory */ private $packageFileFactory;

public function __construct(
    Files $filesUtil,
    PackageFileFactory $packageFileFactory
) {
    $this->filesUtil          = $filesUtil;
    $this->packageFileFactory = $packageFileFactory;

public function get(): array
    $files              = [];
    $pattern            = BP . "/vendor/*/*/src/*/*/assets";
    foreach (Files::getFiles([$pattern], '*') as $file) {
        $path          = str_replace(DIRECTORY_SEPARATOR, '/', BP);
        $fileName      = $this->_parseAssets($file, $path);
        $params             = [
            'area'       => Package::BASE_AREA,
            'theme'      => null,
            'locale'     => null,
            'module'     => null,
            'fileName'   => $fileName,
            'sourcePath' => $file
        $files[]        = $this->packageFileFactory->create($params);

    return $files;

 * Parse file path from the absolute path of static library
 * @param string $file
 * @param string $path
 * @return string
protected function _parseAssets(string $file, string $path): string
        '/^' . preg_quote("{$path}/vendor/*/*/src/*/*/", '/') . '(.+)$/i', $file,

    return 'custom/assets/' . $matches[1];


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.