How to avoid redefining requirejs in plugin when another plugin also uses requirejs

Now here is a little pickle I have yet so solve properly.

I authored a WordPress plugin and it uses requirejs for loading javascript modules. All is well, until occasionally I get very obscure error reports form users. As it turns out, when other themes or plugins also had the bright idea of using requirejs you get the infamous “mismatch anonymous define() modules (problem)[http://www.requirejs.org/docs/errors.html#mismatch] due two several instances of requirejs itself.

Is there any way around this?

2 Answers
2

You could take a look at the enqueued scripts in $wp_scripts ($wp_scripts->registered), but the naming could be different (“it’s requirejs but let’s call it loadstuffjs, just because”), so I doubt it’ll be close to perfect.

Also, your plugin might be loaded before the other plugin, and they might trigger the problem without you having any control.

Maybe make loading requirejs optional (but enabled) and tell people that they ought to try disable loading it if they run into this problem (the reason being that the “other” requirejs will be loaded and work just fine). Also, I’d get a list of plugin/themes that you have these problems with and inform the user about incompatibility and ask whether they want to enable your “compatibility mode” (aka not loading requirejs through your plugin).

Leave a Comment