resourceloader: Reduce 'implement' overhead for modules without scripts
authorBartosz Dziewoński <matma.rex@gmail.com>
Tue, 29 Jan 2019 03:33:14 +0000 (04:33 +0100)
committerTimo Tijhof <krinklemail@gmail.com>
Sat, 9 Feb 2019 22:46:58 +0000 (22:46 +0000)
Change-Id: I55728c526711545be3923d3e1e2f276cbfa52224

includes/resourceloader/ResourceLoader.php
resources/src/startup/mediawiki.js
tests/phpunit/includes/resourceloader/ResourceLoaderClientHtmlTest.php

index b7c85d4..b648260 100644 (file)
@@ -1218,7 +1218,9 @@ MESSAGE;
                $name, $scripts, $styles, $messages, $templates
        ) {
                if ( $scripts instanceof XmlJsCode ) {
-                       if ( self::inDebugMode() ) {
+                       if ( $scripts->value === '' ) {
+                               $scripts = null;
+                       } elseif ( self::inDebugMode() ) {
                                $scripts = new XmlJsCode( "function ( $, jQuery, require, module ) {\n{$scripts->value}\n}" );
                        } else {
                                $scripts = new XmlJsCode( 'function($,jQuery,require,module){' . $scripts->value . '}' );
index 967c529..b5ba6a6 100644 (file)
 
                                                try {
                                                        if ( typeof descriptor.script === 'function' ) {
+                                                               // Function literal: cast to string
                                                                encodedScript = String( descriptor.script );
                                                        } else if (
-                                                               // Plain object: an object that is not null and is not an array
+                                                               // Plain object: serialise as object literal (not JSON),
+                                                               // making sure to preserve the functions.
                                                                typeof descriptor.script === 'object' &&
                                                                descriptor.script &&
                                                                !Array.isArray( descriptor.script )
                                                                        } ).join( ',' ) +
                                                                        '}}';
                                                        } else {
+                                                               // Array of urls, or null.
                                                                encodedScript = JSON.stringify( descriptor.script );
                                                        }
                                                        args = [
index dbc757f..70056ba 100644 (file)
@@ -225,7 +225,7 @@ Deprecation message.' ]
                        . '<script>(window.RLQ=window.RLQ||[]).push(function(){'
                        . 'mw.config.set({"key":"value"});'
                        . 'mw.loader.state({"test.exempt":"ready","test.private":"loading","test.styles.pure":"ready","test.styles.private":"ready","test.styles.deprecated":"ready","test.scripts":"loading"});'
-                       . 'mw.loader.implement("test.private@{blankVer}",function($,jQuery,require,module){},{"css":[]});'
+                       . 'mw.loader.implement("test.private@{blankVer}",null,{"css":[]});'
                        . 'RLPAGEMODULES=["test"];mw.loader.load(RLPAGEMODULES);'
                        . 'mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test.scripts\u0026only=scripts\u0026skin=fallback");'
                        . '});</script>' . "\n"
@@ -343,7 +343,7 @@ Deprecation message.' ]
                                'context' => [],
                                'modules' => [ 'test.private' ],
                                'only' => ResourceLoaderModule::TYPE_COMBINED,
-                               'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.private@{blankVer}",function($,jQuery,require,module){},{"css":[]});});</script>',
+                               'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.private@{blankVer}",null,{"css":[]});});</script>',
                        ],
                        [
                                'context' => [],
@@ -393,7 +393,7 @@ Deprecation message.' ]
                                'context' => [],
                                'modules' => [ 'test.shouldembed' ],
                                'only' => ResourceLoaderModule::TYPE_COMBINED,
-                               'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.shouldembed@09p30q0",function($,jQuery,require,module){},{"css":[]});});</script>',
+                               'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.shouldembed@09p30q0",null,{"css":[]});});</script>',
                        ],
                        [
                                'context' => [],
@@ -411,7 +411,7 @@ Deprecation message.' ]
                                'context' => [],
                                'modules' => [ 'test', 'test.shouldembed' ],
                                'only' => ResourceLoaderModule::TYPE_COMBINED,
-                               'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test\u0026skin=fallback");mw.loader.implement("test.shouldembed@09p30q0",function($,jQuery,require,module){},{"css":[]});});</script>',
+                               'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test\u0026skin=fallback");mw.loader.implement("test.shouldembed@09p30q0",null,{"css":[]});});</script>',
                        ],
                        [
                                'context' => [],