WordPress Trac: Ticket #23285: Implement an AMD JavaScript loader in WordPresshttps://core.trac.wordpress.org/ticket/23285
<p>
AMD = Asynchronous Module Definition
</p>
<p>
See comment: <a class="ext-link" href="http://core.trac.wordpress.org/ticket/22896#comment:29"><span class="icon">​</span>http://core.trac.wordpress.org/ticket/22896#comment:29</a>
</p>
<p>
I think this is a must for the future viability of WordPress and plugins written for it.
</p>
<p>
Multiple versions of JavaScript libraries are a fact of life and we have to deal with it somehow.
</p>
<p>
If WordPress provides the framework for the use of multiple versions of JavaScript libraries then a lot of the problems we see with WordPress now will be a thing of the past.
</p>
<p>
This represents a major re-think of the current WordPress JavaScript loading paradigm, but I think it is necessary and would be well worth it.
</p>
<p>
I'm not a terribly advanced developer, and there might be better solutions out there or others to come, but for now, I think this is the way to go.
</p>
<p>
Example:
</p>
<p>
<a class="ext-link" href="http://requirejs.org/docs/why.html"><span class="icon">​</span>http://requirejs.org/docs/why.html</a>
</p>
<p>
<a class="ext-link" href="http://requirejs.org/docs/whyamd.html"><span class="icon">​</span>http://requirejs.org/docs/whyamd.html</a>
</p>
<p>
<a class="ext-link" href="https://github.com/amdjs/amdjs-api/wiki/AMD"><span class="icon">​</span>https://github.com/amdjs/amdjs-api/wiki/AMD</a>
</p>
<p>
It does not have to be RequireJS, but it seems the most mature and popular of the implementations.
</p>
<p>
From the unminified jQuery source:
</p>
<pre class="wiki">// Expose jQuery as an AMD module, but only for AMD loaders that
// understand the issues with loading multiple versions of jQuery
// in a page that all might call define(). The loader will indicate
// they have special allowances for multiple jQuery versions by
// specifying define.amd.jQuery = true. Register as a named module,
// since jQuery can be concatenated with other files that may use define,
// but not use a proper concatenation script that understands anonymous
// AMD modules. A named AMD is safest and most robust way to register.
// Lowercase jquery is used because AMD module names are derived from
// file names, and jQuery is normally delivered in a lowercase file name.
// Do this after creating the global so that if an AMD module wants to call
// noConflict to hide this version of jQuery, it will work.
if ( typeof define === "function" &amp;&amp; define.amd &amp;&amp; define.amd.jQuery ) {
define( "jquery", [], function () { return jQuery; } );
}
</pre><p>
Note: <strong>multiple versions of jQuery</strong>
</p>
en-usWordPress Trachttps://core.trac.wordpress.org/chrome/site/your_project_logo.pnghttps://core.trac.wordpress.org/ticket/23285
Trac 1.0.1auniquenameThu, 24 Jan 2013 14:48:36 GMThttps://core.trac.wordpress.org/ticket/23285#comment:1
https://core.trac.wordpress.org/ticket/23285#comment:1
<p>
Though thinking about it, perhaps the existing wp_register_script model could be modified to be AMD literate while preserving backward compatibility by doing the AMD implementation transparently behind the WordPress API. (?)
</p>
<p>
Perhaps include all versions of jQuery as they are released and take the dependency array passed to wp_register_script and use it to create Asynchronous Module Definitions behind the scenes...
</p>
<pre class="wiki">wp_register_script( 'myplugin-script', MYPLUGIN_URL . 'js/myplugin.js',
array( 'jquery 1.9.0',
'jquery-ui-dialog 1.10.0'
)
);
</pre><p>
Or something like that?
</p>
TicketscribuThu, 24 Jan 2013 23:33:40 GMTkeywords sethttps://core.trac.wordpress.org/ticket/23285#comment:2
https://core.trac.wordpress.org/ticket/23285#comment:2
<ul>
<li><strong>keywords</strong>
<em>reporter-feedback</em> added
</li>
</ul>
<blockquote class="citation">
<p>
If WordPress provides the framework for the use of multiple versions of JavaScript libraries then a lot of the problems we see with WordPress now will be a thing of the past.
</p>
</blockquote>
<p>
Problems such as?
</p>
TicketauniquenameFri, 25 Jan 2013 00:28:57 GMThttps://core.trac.wordpress.org/ticket/23285#comment:3
https://core.trac.wordpress.org/ticket/23285#comment:3
<p>
See ticket linked above (and again below):
</p>
<p>
<a class="ext-link" href="http://core.trac.wordpress.org/ticket/22896"><span class="icon">​</span>http://core.trac.wordpress.org/ticket/22896</a>
</p>
<p>
Plugin collisions with other plugins and the core functionality of WordPress (usually, but not always, in admin).
</p>
<p>
I have had to resort to an ugly and inefficient hack to ensure that the jQuery I want to use in my plugin does not conflict with the one used in WordPress or with the jQuery used by other plugins. And to attempt to have a plugin with a working lifespan longer than the WordPress and jQuery release cycles.
</p>
<p>
However I have hit other significant issues with jQuery itself and I am attempting to report issues there, but their bug tracker registration is broken and I have to resort to their forums.
</p>
<p>
Basically, neither WordPress or jQuery is ready for a plugin as complicated as I want to produce. I hope by submitting this feedback that someday they will be.
</p>
<p>
Ultimately the goal is to use multiple jQuery UI themes in a plugin in such a way that it does not break anything else and will work for an extended period of time with little to no maintenance (see long comment in ticket linked above).
</p>
<p>
As I see it, this needs to happen. Though perhaps I expect too much?
</p>
TicketwonderboymusicFri, 25 Jan 2013 00:33:10 GMThttps://core.trac.wordpress.org/ticket/23285#comment:4
https://core.trac.wordpress.org/ticket/23285#comment:4
<p>
"Basically, neither WordPress or jQuery is ready for a plugin as complicated as I want to produce"
</p>
<p>
What does that even mean? Please describe so someone can help you get there - I doubt it's impossible
</p>
TicketscribuFri, 25 Jan 2013 00:37:34 GMThttps://core.trac.wordpress.org/ticket/23285#comment:5
https://core.trac.wordpress.org/ticket/23285#comment:5
<p>
Since jQuery hardcodes the module name as 'jquery', I don't see how AMD would help with having two versions loaded separately.
</p>
<p>
From the ticket you mention:
</p>
<blockquote class="citation">
<p>
With jQuery there is a better way to do this: let the WordPress jQuery load first, then load your desired version in the footer or right before it's needed [...]
</p>
</blockquote>
<p>
<a class="ext-link" href="http://core.trac.wordpress.org/ticket/22896#comment:30"><span class="icon">​</span>http://core.trac.wordpress.org/ticket/22896#comment:30</a>
</p>
TicketauniquenameFri, 25 Jan 2013 03:19:19 GMThttps://core.trac.wordpress.org/ticket/23285#comment:6
https://core.trac.wordpress.org/ticket/23285#comment:6
<p>
@scribu - Roger. I've got that now. Though I will use the solution outlined later in that ticket. WordPress does not necessarily need to use AMD to implement multiple js lib versions dependencies, but why re-invent the wheel? Alternatively, if plugin authors are encouraged to bundle and isolate jQuery themselves as discussed in that other ticket, then why make the jQuery used internally by WordPress available to plugin authors as it is now at all? And why is the solution I have arrived at with the assistance here not documented? (I know, it is in the jQuery docs, but the WordPress codex leads one to register their scripts with dependencies on the jQuery included with WordPress)
</p>
<p>
@wonderboymusic - Basically, it comes down to using multiple fully css scoped jQuery UI themes on a single page that will continue to work across at least a few releases of WordPress and jQuery. (If called for, depending on how the user sets the plugin options, and without breaking any other plugins or native WordPress functionality.) I am not so much concerned with what happens in admin as on the front end. I now have a way to make WordPress do it by isolating my jQuery and its plugins as discussed (but this is not the standard or documented way to use jQuery in WordPress). Currently the challenge that I face now that I can reliably isolate jQuery and its plugins in WordPress is the failings of jQuery UI itself. When you fully css scope a theme (every single css selector, not just the ones they scope in the themeroller - to preserve a theme behaviour across mutliple jQuery UI versions without collision with other jQuery UI themes from earlier or later versions) - you soon discover that many of the jQuery UI functions do not really take into account a css scoped theme at all (even a partially scoped one as the themeroller does).
</p>
<p>
What happens is that things like overlays, and element clones used for animations and effects fall out of the css scope by being appended to elements (usually body) outside your defined css scope - and one cannot apply their jQuery UI scoping class to the body of a document without potentially affecting jQuery UI themes elements added before or after the ones you are working with.
</p>
<p>
Hopefully that fills in some of the blanks for you. Right now my issue is with overlays (I have my own hack for that and it has been reported in the jQuery bug tracker), and with the explode effect falling out of theme scope, which has not been reported, and I seem to be unable to register on their bug tracker all names no matter how unique (even tried GUIDs) are reported as taken. Grrr. So I have registered on their forums to report some of this stuff.
</p>
<p>
In jQuery UI 1.9.2 scoped overlays did not appear at all and had to be kicked into scope, now in 1.10.0 they appear in scope but out of z-order and over the element they are supposed to be behind. (I have hacks for both cases.) What is clear is that the jQuery UI developers are not testing under multiple scoped themes at all. That is half the point of jQuery UI - themed UIs!!! Arg!
</p>
<p>
The reason I wanted to use jQuery UI 1.10.0 is that it corrects the dialog falling out of scope by allowing it to be appended to elements other than the body without a hack that adversely affects the body dimensions. i.e. this:
<a class="ext-link" href="http://stackoverflow.com/questions/2199888/custom-css-scope-jquery-ui-dialog-themes"><span class="icon">​</span>http://stackoverflow.com/questions/2199888/custom-css-scope-jquery-ui-dialog-themes</a>
is no longer required.
</p>
<p>
However there is still the overlay and explode issues (both existed prior to 1.10.0) and now icon button focus and sprite issues with the themes as implemented in 1.10.0 that did not exist in 1.9.2.
</p>
<p>
So neither product is really ready for what I want to make them do, but it is so close... I can taste it. But the bulk of this reply is actually for the broken jQuery bug tracker...
</p>
<p>
The issue relating directly to <strong>this ticket</strong> is that I still see the need to use some way to handle multiple versions of jQuery in WordPress in a standard way. I suggested AMD as it seems to be the direction this type of thing is going, though it need not be done that way, there is more than one way to skin this cat. The alternative, and perhaps better solution (?) is not to encourage plugin authors to use the jQuery included in WordPress at all and lock it down for internal use only, and document or link the proper way to isolate your own copy of jQuery to use in your plugin as I have arrived at for my current solution. It is all about plugin stability and longevity.
</p>
<p>
When a new plugin breaks an old one on a site, all the client knows is that their site is broken. Or when a WordPress update breaks a plugin that a site depends on... the user is inclined not to update, but what if that update contains a critical security update? I see a lot of job postings to fix old WordPress sites that have been compromised. The current situation does not really do much to prevent that from happening. I found a way to do it, and then a better ("the right way") to do with with the help here, but how many plugin authors will go to the trouble that I have to ensure that?
</p>
<p>
And lo and behold... as I have been writing this:
</p>
<blockquote class="citation">
<ul><li>Work around some misconfigurations that may have caused some JavaScript in the WordPress admin area to fail.
</li></ul><p>
...
</p>
<ul><li>Server-side request forgery (SSRF) and remote port scanning via pingbacks. Fixed by the WordPress security team.
</li><li>Cross-site scripting (XSS) via shortcodes and post content. Discovered by Jon Cave of the WordPress security team.
</li><li>Cross-site scripting (XSS) in the external library Plupload. Plupload 1.5.5 was released to address this issue.
</li></ul></blockquote>
<p>
<a class="ext-link" href="http://codex.wordpress.org/Version_3.5.1"><span class="icon">​</span>http://codex.wordpress.org/Version_3.5.1</a>
</p>
<p>
Though I see this after updating:
</p>
<blockquote class="citation">
<p>
External Libraries
WordPress now includes the Underscore and Backbone JavaScript libraries. TinyMCE, jQuery, jQuery UI, and SimplePie have all been updated to the latest versions.
</p>
</blockquote>
<p>
Not quite so. WordPress' jQuery and jQuery UI remain at 1.8.3 and 1.9.2 respectively, while the current versions are 1.9.0 and 1.10.0. WordPress' Backbone is at 0.9.2 not the current 0.9.10 ...
</p>
<p>
This adds weight to my argument.
</p>
TicketSergeyBiryukovFri, 25 Jan 2013 04:00:30 GMThttps://core.trac.wordpress.org/ticket/23285#comment:7
https://core.trac.wordpress.org/ticket/23285#comment:7
<p>
Replying to <a class="new" href="https://core.trac.wordpress.org/ticket/23285#comment:6" title="Comment 6 for Ticket #23285">auniquename</a>:
</p>
<blockquote class="citation">
<p>
Alternatively, if plugin authors are encouraged to bundle and isolate jQuery themselves as discussed in that other ticket, then why make the jQuery used internally by WordPress available to plugin authors as it is now at all?
</p>
</blockquote>
<p>
Quite the contrary, plugin authors are encouraged to use the jQuery version bundled with WordPress, and update the plugins accordingly. Your situation, as described in <a class="closed" href="https://core.trac.wordpress.org/ticket/22896#comment:29" title="Comment 29 for Ticket #22896">ticket:22896:29</a>, sounds like an edge case to me.
</p>
TicketauniquenameFri, 25 Jan 2013 04:26:50 GMThttps://core.trac.wordpress.org/ticket/23285#comment:8
https://core.trac.wordpress.org/ticket/23285#comment:8
<p>
Perhaps so in my case, as I am more particular than most.
</p>
<p>
That said, my comments on people not wanting to update in order to preserve the functionality of unmaintained plugins stand, and that is damaging overall to the reputation of WordPress, particularly with regard to security.
</p>
<p>
People have custom plugins written for their site alone, and then may never hear from the developer again and are unable to maintain code themselves. Ya, their bed, lie in it, yada, yada... But if we can prevent that, then why not?
</p>
<p>
I also have this crazy half baked idea of snapshotting the current version of jQuery in WordPress when a plugin that references it is installed, then storing it in the db and feed the plugin that copy henceforth... until the plugin itself is updated. I haven't really thought that all the way through though. Probably lots of pitfalls.
</p>
<p>
I just don't see any reason why not include some way to deal with js lib version dependencies. Not doing so causes more problems than doing so as I see it.
</p>
<p>
Also, the release cycle is frequent enough to make all this a lot of work, too much? Even just thinking about how to write a plugin that supports multiple versions of WordPress and multiple versions of js libs is giving me braincramps. I cannot count on users updating their sites to the latest versions, so if I want my plugin to work on the most sites possible I need to write it to account for a whole quarterly history of jQuery revisions back a few years at least. Why put people through that? Overall man hours expended to implement an AMD style loader or some kind of mechanism to deal with multiple versions of js libs in WordPress would be considerably less. Set it and forget it I say. Computers and software are meant for automating such arduous and burdensome tasks.
</p>
TicketauniquenameFri, 25 Jan 2013 05:01:41 GMTkeywords deletedhttps://core.trac.wordpress.org/ticket/23285#comment:9
https://core.trac.wordpress.org/ticket/23285#comment:9
<ul>
<li><strong>keywords</strong>
<em>reporter-feedback</em> removed
</li>
</ul>
TicketbpettyFri, 25 Jan 2013 05:57:02 GMThttps://core.trac.wordpress.org/ticket/23285#comment:10
https://core.trac.wordpress.org/ticket/23285#comment:10
<p>
Putting aside @auniquename's personal issues working with jQuery and other JS libs themselves, the idea of using RequireJS with <a class="ext-link" href="http://requirejs.org/docs/api.html#multiversion"><span class="icon">​</span>multiversion support</a> actually does have some merit, and is at least worth some investigation, discussion, and possible patch down the road.
</p>
<p>
As was mentioned, WP does update bundled JS libraries very frequently, and they are often updated to API incompatible versions. They might as well be since there's no stable API policy around 3rd party JS libraries still. Most of the libraries WP bundles are bundled because they are quality, popular libraries, and so they often do have good policies around backwards compatibility even when upgrading to API incompatible versions (often only removing really old API like WP occasionally does, or infrequently used API). So it doesn't happen very frequently, but plugins do occasionally break when WP is updated and those JS libraries the plugin was originally built on are updated with it to API incompatible versions. That is the definition of "API incompatible", code originally written to work with older versions is not guaranteed to work with the new API incompatible branch.
</p>
<p>
I'd be surprised if the plugin review team hasn't seen this with old plugins. Do they even review old plugins, particularly ones with high incompatible votes in the compatibility matrix, or only new ones?
</p>
<p>
Anyway, I believe these are the same issues @auniquename is referring to. They are also the same issues I've referred to in the past when discussing <a class="ext-link" href="http://ibaku.wordpress.com/2012/11/08/wordpress-plugin-and-theme-api-manifest-versioning/"><span class="icon">​</span>plugin API manifest versioning</a>. I actually do believe this is one approach to the problem that could potentially work.
</p>
<p>
Replying to <a class="new" href="https://core.trac.wordpress.org/ticket/23285#comment:8" title="Comment 8 for Ticket #23285">auniquename</a>:
</p>
<blockquote class="citation">
<p>
People have custom plugins written for their site alone, and then may never hear from the developer again and are unable to maintain code themselves. Ya, their bed, lie in it, yada, yada... But if we can prevent that, then why not?
</p>
</blockquote>
<p>
Sorry, but you'll generally find that most of the WP community doesn't care about these people, and the response you'll almost always find from WP developers is "upgrade your plugins or fix them" - and if their developer has abandoned them or disappeared, the answer is hire another. You won't get far with that argument.
</p>
<p>
Also keep in mind though that if you feel like you're being pushed back, it's because you're making a suggestion to change something without even providing a patch to review and test out. Anyone that agrees with you in a situation like this seems to believe they are volunteering to do the work (and I wish they didn't because they aren't). When that happens, they're always inclined to defend however it is that WP currently handles (or doesn't handle) that issue.
</p>
TicketbpettyFri, 25 Jan 2013 05:59:48 GMTcc sethttps://core.trac.wordpress.org/ticket/23285#comment:11
https://core.trac.wordpress.org/ticket/23285#comment:11
<ul>
<li><strong>cc</strong>
<em>bpetty</em> added
</li>
</ul>
TicketauniquenameFri, 25 Jan 2013 06:57:33 GMThttps://core.trac.wordpress.org/ticket/23285#comment:12
https://core.trac.wordpress.org/ticket/23285#comment:12
<p>
Right. So why build something over and over and over when you can build it once? I'm glad you see the benefit of the idea.
</p>
<p>
If it has to wait for me to do it for WordPress myself, then so be it. Fine, I can deal with that. But it will take me much longer than a more experienced or skilled developer. I'm a chemist by education and training, not a computer scientist. (Though I understand all they teach in school now is Java with a big easy button.) Also, I would test it to death so that it is guaranteed to work and be near perfect in every way.
</p>
<blockquote class="citation">
<p>
Anyone that agrees with you in a situation like this seems to believe they are volunteering to do the work (and I wish they didn't because they aren't).
</p>
</blockquote>
<p>
(This is an excellent point, thank you for reminding me.)
</p>
<p>
The problem is that it will not pay the rent and I have about 8 weeks left until I am on the street. So it just is not going to happen right now. Sorry I don't have a patch for you, but the idea is a good one. My dilemma continues unresolved in the other ticket, there are just too many possible use cases.
</p>
<p>
I am not alone in my issues with jQuery UI, it is far from personal, I just seem to have found more bugs than others by pushing the product to its (intended) limits.
</p>
<p>
And whether you like it or not, compromised WP sites reflect poorly on WP as much as on their owners.
</p>
<p>
Sorry if this post comes across as snarky, I do not mean it to be, I'm just tired and frustrated and should really go to bed. Everyone participating in this discussion has been most civil and helpful and I appreciate that a great deal, it is a rare thing on the internet.
</p>
TicketlkraavWed, 09 Oct 2013 05:51:08 GMTcc changedhttps://core.trac.wordpress.org/ticket/23285#comment:13
https://core.trac.wordpress.org/ticket/23285#comment:13
<ul>
<li><strong>cc</strong>
<em>leho@…</em> added
</li>
</ul>
TicketericlewisSun, 15 Dec 2013 19:24:25 GMThttps://core.trac.wordpress.org/ticket/23285#comment:14
https://core.trac.wordpress.org/ticket/23285#comment:14
<p>
+1 on this.
</p>
<p>
There's basic performance boosts that would be gained from utilizing AMD.
</p>
TicketnacinSun, 19 Jan 2014 08:27:28 GMTcomponent changedhttps://core.trac.wordpress.org/ticket/23285#comment:15
https://core.trac.wordpress.org/ticket/23285#comment:15
<ul>
<li><strong>component</strong>
changed from <em>General</em> to <em>Script Loader</em>
</li>
</ul>
TicketircbotWed, 12 Feb 2014 21:23:02 GMThttps://core.trac.wordpress.org/ticket/23285#comment:16
https://core.trac.wordpress.org/ticket/23285#comment:16
<p>
<em>This ticket was mentioned in IRC in #wordpress-dev by tierra. <a class="ext-link" href="http://irclogs.wordpress.org/chanlog.php?channel=wordpress-dev&amp;day=2014-02-12&amp;sort=asc#m790468"><span class="icon">​</span>View the logs</a>.</em>
</p>
TicketircbotWed, 11 Jun 2014 21:59:01 GMThttps://core.trac.wordpress.org/ticket/23285#comment:17
https://core.trac.wordpress.org/ticket/23285#comment:17
<p>
<em>This ticket was mentioned in IRC in #wordpress-dev by gcorne. <a class="ext-link" href="http://irclogs.wordpress.org/chanlog.php?channel=wordpress-dev&amp;day=2014-06-11&amp;sort=asc#m868017"><span class="icon">​</span>View the logs</a>.</em>
</p>
TicketwonderboymusicSat, 28 Jun 2014 05:12:42 GMTmilestone changed; keywords sethttps://core.trac.wordpress.org/ticket/23285#comment:18
https://core.trac.wordpress.org/ticket/23285#comment:18
<ul>
<li><strong>keywords</strong>
<em>needs-patch</em> added
</li>
<li><strong>milestone</strong>
changed from <em>Awaiting Review</em> to <em>Future Release</em>
</li>
</ul>
TicketwonderboymusicSat, 03 Jan 2015 05:55:25 GMTmilestone changedhttps://core.trac.wordpress.org/ticket/23285#comment:19
https://core.trac.wordpress.org/ticket/23285#comment:19
<ul>
<li><strong>milestone</strong>
changed from <em>Future Release</em> to <em>4.2</em>
</li>
</ul>
<p>
I want us to figure out CommonJS in 4.2
</p>
TicketDrewAPictureSat, 07 Mar 2015 21:59:15 GMTmilestone changedhttps://core.trac.wordpress.org/ticket/23285#comment:20
https://core.trac.wordpress.org/ticket/23285#comment:20
<ul>
<li><strong>milestone</strong>
changed from <em>4.2</em> to <em>Future Release</em>
</li>
</ul>
<p>
No activity in 2 months and it's definitely too late to explore this for 4.2.
</p>
TicketwonderboymusicSat, 07 Mar 2015 22:00:49 GMThttps://core.trac.wordpress.org/ticket/23285#comment:21
https://core.trac.wordpress.org/ticket/23285#comment:21
<p>
we're basically doing this with Media now
</p>
Ticket