resourceloader: Remove obsolete aliases from closure
authorTimo Tijhof <krinklemail@gmail.com>
Wed, 29 Aug 2018 04:19:10 +0000 (05:19 +0100)
committerKrinkle <krinklemail@gmail.com>
Thu, 30 Aug 2018 16:21:04 +0000 (16:21 +0000)
== jQuery

ResourceLoader wraps and executes all modules in the system via
a closure that explicitly binds '$', 'jQuery'. This means there is
no point in aliasing jQuery to $ in every single file.
ResourceLoader already does this.

This is a very very old habit that was introduced in 2009 when we
didn't have ResourceLoader and were concerned with wikis loading
their own copy of jQuery that could redefine the global 'jQuery'
and '$' variables. We simply hoped that "our module" initialised
before "that module" cache the reference we got in the file closure.

Then in 2010, when building ResourceLoader, we found this didn't
always work. And we also sometimes forgot to add the closure.
Which is why in 2010 (before ResourceLoader went to prod, in 2011)
we fixed the above issue in ResourceLoader itself by "magically"
providing a private binding to '$' and 'jQuery' in every
mw.loader.implement() closure. (r79246, bd93eeb85).

So, these in-file closure references are redundant.
And have been since 2010.

== jQuery, again.

While redundant, they remained in most files. Harmless, right?

However, that same problem of duplicate jQuery copys on a page
came up again in 2013. Why did our magic binding not work?

It was *because* the file also did its own binding:

1. ResourceLoader stores reference to proper jQuery.
2. ResourceLoader provides private reference to it as '$'.
3. .. time passes ..
4. Module executes, and is given the proper jQuery via the
  private '$' reference. The module file ignores this because
  it instead looks up current jQuery, and caches that.

So, we expande the magic binding to also bind the name 'jQuery'.
(2013-2014; 5742c1f38527).

== mediaWiki

We export the binding as 'mw' and 'mediaWiki'. We internally
mostly use 'mw' (in HTML, and documentation, and the canonical
name in the JSDuck index). But, rather than using the shorter name,
we use the longer name and alias it in every single file.

There was never a concern about this global being redefined
as far as I know. However, if that happens one day, we should..
provide a magic binding for it.

Change-Id: Id6d13bbea6927a4c7354ca1edd98f13f0fae30c1

.eslintrc.json
resources/src/mediawiki.base/mediawiki.base.js
resources/src/mediawiki.base/mediawiki.errorLogger.js
resources/src/startup/mediawiki.js
resources/src/startup/mediawiki.log.js
resources/src/startup/mediawiki.requestIdleCallback.js
resources/src/startup/profiler.js
resources/src/startup/startup.js

index da5d409..8f2dace 100644 (file)
@@ -1,13 +1,15 @@
 {
        "extends": "wikimedia",
        "env": {
-               "browser": true,
-               "jquery": true
+               "browser": true
        },
        "globals": {
                "require": false,
                "module": false,
+               "mw": false,
+               "$": false,
                "mediaWiki": false,
+               "jQuery": false,
                "OO": false
        },
        "rules": {
index 284b21a..16994f5 100644 (file)
@@ -14,7 +14,6 @@
  *   "mediawiki" module, and will remain a default/implicit dependency for all
  *   regular modules, just like jquery and wikibits already are.
  */
-/* globals mw */
 ( function () {
        'use strict';
 
index e86aff6..d1fbff0 100644 (file)
@@ -4,7 +4,7 @@
  * @class mw.errorLogger
  * @singleton
  */
-( function ( mw ) {
+( function () {
        'use strict';
 
        mw.errorLogger = {
@@ -55,4 +55,4 @@
        };
 
        mw.errorLogger.installGlobalHandler( window );
-}( mediaWiki ) );
+}() );
index 3fb7ffc..22bfede 100644 (file)
@@ -1,7 +1,7 @@
 /**
  * Base library for MediaWiki.
  *
- * Exposed globally as `mediaWiki` with `mw` as shortcut.
+ * Exposed globally as `mw`, with `mediaWiki` as alias.
  *
  * @class mw
  * @alternateClassName mediaWiki
index a3f3c68..af59c7f 100644 (file)
@@ -6,7 +6,7 @@
  * @author Michael Dale <mdale@wikimedia.org>
  * @author Trevor Parscal <tparscal@wikimedia.org>
  */
-( function ( mw ) {
+( function () {
        /* global console */
        /* eslint-disable no-console */
        var original = mw.log;
@@ -22,4 +22,4 @@
                mw.log.error = original.error;
                mw.log.deprecate = original.deprecate;
        }
-}( mediaWiki ) );
+}() );
index 650092b..9f8c598 100644 (file)
@@ -1,4 +1,4 @@
-( function ( mw ) {
+( function () {
        var maxBusy = 50;
 
        mw.requestIdleCallbackInternal = function ( callback ) {
@@ -49,4 +49,4 @@
        // Note: Polyfill was previously disabled due to
        // https://bugs.chromium.org/p/chromium/issues/detail?id=647870
        // See also <http://codepen.io/Krinkle/full/XNGEvv>
-}( mediaWiki ) );
+}() );
index 5e9b6ab..588750c 100644 (file)
@@ -4,7 +4,6 @@
  * @author Timo Tijhof
  * @since 1.32
  */
-/* global mw */
 ( function () {
        'use strict';
 
index ee72166..b381eef 100644 (file)
@@ -4,7 +4,7 @@
  * - Beware: This file MUST parse without errors on even the most ancient of browsers!
  */
 /* eslint-disable vars-on-top, no-unmodified-loop-condition */
-/* global mw, isCompatible, $VARS, $CODE */
+/* global isCompatible, $VARS, $CODE */
 
 /**
  * See <https://www.mediawiki.org/wiki/Compatibility#Browsers>