Skip to content

Instantly share code, notes, and snippets.

@jorislucius
Last active June 17, 2019 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jorislucius/4b7118c97f8d9c43035efefbb0d93b45 to your computer and use it in GitHub Desktop.
Save jorislucius/4b7118c97f8d9c43035efefbb0d93b45 to your computer and use it in GitHub Desktop.
<?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