Created
June 28, 2020 21:43
-
-
Save jonginn/21a025421fc9ebf3bf7025d043fa0349 to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* Using entrypoints.json, grab all the scripts that relate to an entrypoint, such as 'app'. | |
* This is then passed to wp_enqueue_script so all the scripts are automatically added. | |
* Thanks to wp_enqueue_script preventing de-dupes, we don't need to worry about runtime.js being duplicated when you use this function for multiple entrypoints. | |
* | |
* @param String $entryName 'app' or other entrypoints defined in your webpack.config.js | |
*/ | |
function get_scripts( $entryName ) { | |
$distDir = '/dist'; | |
$entryPoints = get_template_directory() . $distDir . '/entrypoints.json'; | |
$hash = file_exists( $entryPoints ) ? json_decode( file_get_contents( $entryPoints ), true ) : []; | |
if (false === isset($hash['entrypoints'][$entryName]['js'])) { | |
return; | |
} | |
foreach ($hash['entrypoints'][$entryName]['js'] as $entry) { | |
wp_enqueue_script($entry, $entry, null, false, true); | |
} | |
} | |
/** | |
* Using entrypoints.json, grab all the stylesheets that relate to an entrypoint, such as 'app'. | |
* This is then passed to wp_enqueue_style so all the styles are automatically added. | |
* | |
* @param String $entryName 'app' or other entrypoints defined in your webpack.config.js | |
*/ | |
function get_styles( $entryName ) { | |
$distDir = '/dist'; | |
$entryPoints = get_template_directory() . $distDir . '/entrypoints.json'; | |
$hash = file_exists( $entryPoints ) ? json_decode( file_get_contents( $entryPoints ), true ) : []; | |
if (false === isset($hash['entrypoints'][$entryName]['css'])) { | |
return; | |
} | |
foreach ($hash['entrypoints'][$entryName]['css'] as $entry) { | |
wp_enqueue_style($entry, $entry); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment