From 91f950d6b0677166134bc58eb487943a63f97b00 Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Wed, 29 Aug 2018 05:19:10 +0100 Subject: [PATCH] resourceloader: Remove obsolete aliases from closure == 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 | 6 ++++-- resources/src/mediawiki.base/mediawiki.base.js | 1 - resources/src/mediawiki.base/mediawiki.errorLogger.js | 4 ++-- resources/src/startup/mediawiki.js | 2 +- resources/src/startup/mediawiki.log.js | 4 ++-- resources/src/startup/mediawiki.requestIdleCallback.js | 4 ++-- resources/src/startup/profiler.js | 1 - resources/src/startup/startup.js | 2 +- 8 files changed, 12 insertions(+), 12 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index da5d409765..8f2dace87c 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -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": { diff --git a/resources/src/mediawiki.base/mediawiki.base.js b/resources/src/mediawiki.base/mediawiki.base.js index 284b21ad82..16994f5bba 100644 --- a/resources/src/mediawiki.base/mediawiki.base.js +++ b/resources/src/mediawiki.base/mediawiki.base.js @@ -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'; diff --git a/resources/src/mediawiki.base/mediawiki.errorLogger.js b/resources/src/mediawiki.base/mediawiki.errorLogger.js index e86aff6b78..d1fbff047e 100644 --- a/resources/src/mediawiki.base/mediawiki.errorLogger.js +++ b/resources/src/mediawiki.base/mediawiki.errorLogger.js @@ -4,7 +4,7 @@ * @class mw.errorLogger * @singleton */ -( function ( mw ) { +( function () { 'use strict'; mw.errorLogger = { @@ -55,4 +55,4 @@ }; mw.errorLogger.installGlobalHandler( window ); -}( mediaWiki ) ); +}() ); diff --git a/resources/src/startup/mediawiki.js b/resources/src/startup/mediawiki.js index 3fb7ffc84c..22bfede6ae 100644 --- a/resources/src/startup/mediawiki.js +++ b/resources/src/startup/mediawiki.js @@ -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 diff --git a/resources/src/startup/mediawiki.log.js b/resources/src/startup/mediawiki.log.js index a3f3c682eb..af59c7f943 100644 --- a/resources/src/startup/mediawiki.log.js +++ b/resources/src/startup/mediawiki.log.js @@ -6,7 +6,7 @@ * @author Michael Dale * @author Trevor Parscal */ -( 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 ) ); +}() ); diff --git a/resources/src/startup/mediawiki.requestIdleCallback.js b/resources/src/startup/mediawiki.requestIdleCallback.js index 650092bd8e..9f8c598876 100644 --- a/resources/src/startup/mediawiki.requestIdleCallback.js +++ b/resources/src/startup/mediawiki.requestIdleCallback.js @@ -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 -}( mediaWiki ) ); +}() ); diff --git a/resources/src/startup/profiler.js b/resources/src/startup/profiler.js index 5e9b6ab2a6..588750cce0 100644 --- a/resources/src/startup/profiler.js +++ b/resources/src/startup/profiler.js @@ -4,7 +4,6 @@ * @author Timo Tijhof * @since 1.32 */ -/* global mw */ ( function () { 'use strict'; diff --git a/resources/src/startup/startup.js b/resources/src/startup/startup.js index ee72166072..b381eef7a3 100644 --- a/resources/src/startup/startup.js +++ b/resources/src/startup/startup.js @@ -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 -- 2.20.1