Page MenuHomePhabricator

ResourceLoader: Remove obsolete closures from files
Closed, ResolvedPublic

Description

Situation:

  • ResourceLoader wraps modules in closures
  • MediaWiki developers put a closure in each file
  • As a result loading 1 module with 2 files will have 3 closures
mw.loader.implement( 'foo', function ( $ ) {
  --- foo.a.js
  ( function ( $, mw ) { 
    ...
  }( jQuery, mediaWiki ) )

  --- foo.b.js
  ( function ( $, mw ) { 
    ...
  }( jQuery, mediaWiki ) )
} );

inside mw.loader#execute:

  script( jQuery );

It has been suggested we add "mw" to ResourceLoader's closure and get rid of the per-file closure.

The downside of doing that would be that they are less safe to use "standalone". e.g. a jquery plugin would be using $ directly, which, outside MediaWiki ResourceLoader context is bad.

There is also the pending refactoring of ResourceLoader debug mode to not be completely different and near useless by executing in the global scope.

Details

Event Timeline

bzimport raised the priority of this task from to Medium.Nov 22 2014, 1:36 AM
bzimport set Reference to bz48886.
bzimport added a subscriber: Unknown Object (MLST).
Krinkle created this task.May 28 2013, 9:33 AM

Also:

modules['bar'] = {

scripts: ['bar.a.js', 'bar.b.js'],
globals: {
  ba: Bar
}

};

mw.loader.implement( 'foo', function ( $, mw, ba ) {

  • bar.a.js ...
  • bar.b.js ...

} );

  • startup:

register(
..,
[ 'ext.visualEditor', 1234, ...., ['VisualEditor']
);

  • mw.loader: script( $, global[propKeys..].. );
  • load.php response when loading visualeditor:

mw.loader.implement( 'ext.visualEditor.core', function ( $, ve ) {

  • include ve.js
  • include ve.A.js
  • include ve.B.js
  • include ve.C.js

} );

Note that this bug isn't blocked on implementing global variable mappings.

This bug basically says:

  • Closures added by ResourceLoader are not merely a by-product of the deferred loading. They are part of the eco system and to be relied on.
  • Drop the tradition that each .js file should be loadable raw and standalone and instead embrace the dynamic build system.

The main thing blocking this is debug mode. We need to refactor debug mode to not load raw files (bug 62605). Instead it should merely keep them deminified. Ideally with support for source maps (bug 45514).

He7d3r added a subscriber: He7d3r.Jan 3 2015, 2:21 PM
Krinkle updated the task description. (Show Details)Apr 10 2015, 5:46 PM
Krinkle removed a project: Future-Release.
Krinkle set Security to None.
Krinkle removed a subscriber: Unknown Object (MLST).
Krinkle lowered the priority of this task from Medium to Low.Apr 28 2015, 5:51 PM
Krinkle moved this task from Inbox to Backlog on the MediaWiki-ResourceLoader board.

Change 501960 had a related patch set uploaded (by Krinkle; owner: Krinkle):
[mediawiki/core@master] mediawiki.Title: Remove redundant closure

https://gerrit.wikimedia.org/r/501960

Restricted Application added a project: Performance-Team. · View Herald TranscriptApr 6 2019, 8:35 PM
Krinkle closed this task as Resolved.Apr 6 2019, 8:38 PM
Krinkle claimed this task.

Effectively fixed by T133462, which makes this a possibility regardless of debug mode.

I'm closing this as resolved as it is now possible with T133462 to lint a file as a "module" (similar to Node.js) without needing a closure. The only reason our files had closures is for compatibility with debug mode. And for modules using packageFiles, debug mode has been fixed to not change the execution scope.

This does mean we can remove them from files not loaded as part of a module bundle that uses packageFiles, but it seems good enough for me.

Maybe as part of T85805, we will make it possible for legacy modules, but that's TBD, and separate from this task.

Change 501960 merged by jenkins-bot:
[mediawiki/core@master] mediawiki.Title: Remove redundant closure

https://gerrit.wikimedia.org/r/501960

Change 501993 had a related patch set uploaded (by Fomafix; owner: Fomafix):
[mediawiki/core@master] Remove redundant closure for all packageFiles with own directory

https://gerrit.wikimedia.org/r/501993

Change 501993 merged by jenkins-bot:
[mediawiki/core@master] Remove redundant closure for all packageFiles with own directory

https://gerrit.wikimedia.org/r/501993

Change 512519 had a related patch set uploaded (by Fomafix; owner: Fomafix):
[mediawiki/core@master] Remove redundant closure for all modules with packageFiles

https://gerrit.wikimedia.org/r/512519

Change 518951 had a related patch set uploaded (by Fomafix; owner: Fomafix):
[mediawiki/extensions/CentralNotice@master] Remove redundant closure for all packageFiles with own directory

https://gerrit.wikimedia.org/r/518951

Change 534762 had a related patch set uploaded (by Fomafix; owner: Fomafix):
[mediawiki/core@master] Remove redundant closure for module 'mediawiki.util'

https://gerrit.wikimedia.org/r/534762

Change 534762 merged by jenkins-bot:
[mediawiki/core@master] mediawiki.util: Remove redundant file closures

https://gerrit.wikimedia.org/r/534762