require.js to load javascript

Though WordPress already has a basic .js API (with register and enqueue script), I don’t see why you cannot use it in conjunction with require.js, at least at the modular level.

From what I can tell (never used it) it only needs to be loaded, so you can,

wp_enqueue_script( 
     $handle, 
     '...folder../require.js',
      $deps,
      $ver,
      $in_footer 
);

Then use the recommended require folder structure and js code to take advantage of it. I believe that require.js is framework agnostic, even so it has it’s own namespace function to avoid conflicts.

With that being said your still going to be limited to using it for your own code as opposed to plugins and themes you might be using.

Meaning, if a plugin or theme author does it the right way you can wp_dequeue_script the script and manage it with require.js, but what if they don’t? There is not much you can do outside writing some sort of hacky plugin or manually changing the code.

For this reason using a js framework with wordpress is probably not worth the effect, unless your doing it from scratch and have control over the plugins and themes.

Leave a Comment