Skip to content

Instantly share code, notes, and snippets.

@annelyse
Last active November 18, 2021 15:53
Show Gist options
  • Save annelyse/dcfa1b910927ba512755e93a06ca8b73 to your computer and use it in GitHub Desktop.
Save annelyse/dcfa1b910927ba512755e93a06ca8b73 to your computer and use it in GitHub Desktop.
read that :
https://adamjberkowitz.com/blog/post/managing-webpack-vendor-chunks-and-wordpress
https://adamjberkowitz.com/blog/post/managing-webpack-vendor-chunks-and-wordpress
plugin to test :
https://github.com/j-arens/wordpress-enqueue-chunks-webpack-plugin
function enqueue_my_scripts(){
if (!is_admin()) {
wp_deregister_script('google-hosted-jquery');
wp_dequeue_script('google-hosted-jquery');
// load style in dist folder
foreach( glob( get_template_directory(). '/dist/*.css' ) as $file ) {
$info = pathinfo($file);
// $file contains the name and extension of the file
wp_enqueue_style( 'mysite-'.$info["filename"], get_template_directory_uri().'/dist/'.$info["filename"].'.css');
}
$chunkNames = []; // all js vendors
$mainChunk = ''; // main js
foreach( glob( get_template_directory(). '/dist/*.js' ) as $file ) {
$info = pathinfo($file);
$pos = strpos( $info['filename'], 'app');
if ( $pos !== 0) { // app is the name of main js file
array_push($chunkNames, $info['filename']);
} else {
$mainChunk = $info['filename'];
}
}
// load all js
foreach ($chunkNames as $name) {
wp_enqueue_script(
'mysite-' . $name,
get_template_directory_uri().'/dist/'.$name.'.js',
[],
false,
true
);
}
wp_enqueue_script("tarteaucitron", get_teqwmplate_directory_uri() . "/src/js/modules/tarteaucitronjs/tarteaucitron.js", false, "", true);
wp_enqueue_script( 'mysite-'.$mainChunk, get_template_directory_uri().'/dist/'.$mainChunk.'.js', array("jquery-core", "gform_gravityforms"), "", true );
}
}
add_action('wp_enqueue_scripts', 'enqueue_my_scripts');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment