Last active
June 17, 2019 17:49
-
-
Save jorislucius/4b7118c97f8d9c43035efefbb0d93b45 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Implements hook_library_info_build(). | |
*/ | |
function YOURMODULENAME_library_info_build() { | |
$libraries = []; | |
$module = \Drupal::moduleHandler()->getModule('YOURMODULENAME'); | |
$module_path = $module->getPath(); | |
$path_to_js = \Drupal::service('file_system')->realpath($module_path) . '/js'; | |
$path_to_manifest = $path_to_js . '/build/asset-manifest.json'; | |
$manifest = Json::decode(file_get_contents($path_to_manifest)); | |
$path_prefix = 'js/build'; | |
// Check if we have files in the manifest. | |
if (!empty($manifest)) { | |
$js = []; | |
$css = []; | |
foreach ($manifest as $manifest_file) { | |
// If this is not a map file. | |
if (stripos($manifest_file, '.map.') === FALSE) { | |
if (substr($manifest_file, -3) === '.js' | |
&& stripos($manifest_file, 'service-worker') === FALSE | |
) { | |
$js[$path_prefix . $manifest_file] = [ | |
'minified' => TRUE, | |
'preprocess' => FALSE, | |
]; | |
} | |
elseif (substr($manifest_file, -4) === '.css') { | |
$css[$path_prefix . $manifest_file] = []; | |
} | |
} | |
} | |
$libraries = [ | |
'YOURLIBRARY.NAME' => [ | |
'version' => 'VERSION', | |
'js' => $js, | |
'css' => [ | |
'theme' => $css, | |
], | |
], | |
]; | |
} | |
return $libraries; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment