Monday, 11 June 2018

Should you combine or enqueue Javascript project files?


In a previous post we covered what the best way was to include jQuery in WordPress. In this post, I’m going to talk a little more about the best ways to include Javascipt files.
When your doing a lot with Javascript, you can end up with a few scripts/libraries supporting your custom script. You might also have several custom scripts to include, but what’s the best way of including these? Would you enqueue each of them seperately? Would you combine the custom ones but enqueue the third party ones? Or would you combine everything you are using in your project into one script and enqueue that?
I’m going to discuss my approach and why I think it’s best.
So, what you might do is to enqueue each supporting scrip then list your custom script, like so:
wp_enqueue_script( ‘select2’, trailingslashit( get_template_directory_uri() ) . ‘select2.js’, array( ‘jquery’ ) );
wp_enqueue_script( ‘custom, trailingslashit( get_template_directory_uri() ) . custom.js’, array( ‘jquery’, ‘select2’) );
It’s important in this scenario that you list the dependant scripts as dependencies, rather then just list them in order. This is to make sure that they are loaded prior to the one
Source: https://managewp.org/articles/17512/should-you-combine-or-enqueue-javascript-project-files



source https://williechiu40.wordpress.com/2018/06/11/should-you-combine-or-enqueue-javascript-project-files/

No comments:

Post a Comment