After some long searching there appears to be a way.
First of all, this means using the requirejs optimiser, i.e. pre-compiling the module, possibly into a single minified file.
Secondly, in the optimiser options there is a namespace
option, which essentially prefixes all require, define and module calls, so they no longer interfere with another requirejs present on the site.
So calling $ node r.js -o build.js
with the following build.js file:
({ paths: { 'modulename': 'path/to/modulefile', 'modulename': 'path/to/modulefile', 'modulename': 'path/to/modulefile', 'requireLib': 'path/to/requirejs' }, name: "js/main", out: "js/bundle.js", include: "requireLib", namespace: "mynamespace", })
will transform the module defined in js/main.js
to js/bundle.js
in minified form and including all require
d modules and all require()
calls in that minified file will be mynamespace.require()
calls in the output file.
The include
option is important so that the optimiser will actually include requirejs
itself in the bundle, and calling it something else than just "require" (i.e. "requireLib") is required so the optimiser internally doesn't get confused.
Finally, this file has all available optimizer build.js options.
It is insane to think that any plugin or theme author using requirejs should be aware of this, and in fact I think most developers aren't until they hit this incompatibility combination with another plugin or theme. This, however, is a way to be certain that requirejs won't cause problems when used in a Wordpress project.