Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jcicero518/2c5812950eb803a64955db2fbae391df to your computer and use it in GitHub Desktop.
Save jcicero518/2c5812950eb803a64955db2fbae391df to your computer and use it in GitHub Desktop.
Drupal and React - Create library
<?php
/**
* Implements hook_library_info_build().
*/
function drupal_and_react_library_info_build() {
# Load current module path.
$module = \Drupal::moduleHandler()->getModule('drupal_and_react');
$module_path = $module->getPath();
# Set app bundle source for develompent and prodution.
$js_dev = 'assets/js/dist/app.bundle.js';
$js_prod = 'assets/js/build/app.bundle.js';
# Generate app bundle source path
$js_src = ( isset($_ENV['APP_ENV']) && $_ENV['APP_ENV'] === 'production' ) ? $js_prod : $js_dev;
$js_src = file_exists($module_path . '/' . $js_src) ? $js_src : $js_prod;
# Build Drupal library.
$js = [];
if ( file_exists($module_path . '/' . $js_src) ) {
$js[$js_src] = [
'minified' => TRUE,
'preprocess' => FALSE,
];
$libraries = [
'app_bundle' => [
'version' => 'VERSION',
'js' => $js,
],
];
}
else {
# Generate exception if app bundle is not found.
throw new FileNotFoundException(
$module_path . '/' . $js_src
);
}
return $libraries;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment