From: Bartosz DziewoƄski Date: Tue, 29 Jan 2019 03:33:14 +0000 (+0100) Subject: resourceloader: Reduce 'implement' overhead for modules without scripts X-Git-Tag: 1.34.0-rc.0~2866^2 X-Git-Url: http://git.cyclocoop.org/?a=commitdiff_plain;h=f38f88d0d39a69cff9a944f7e2d1848ba991291a;p=lhc%2Fweb%2Fwiklou.git resourceloader: Reduce 'implement' overhead for modules without scripts Change-Id: I55728c526711545be3923d3e1e2f276cbfa52224 --- diff --git a/includes/resourceloader/ResourceLoader.php b/includes/resourceloader/ResourceLoader.php index b7c85d43b1..b64826063c 100644 --- a/includes/resourceloader/ResourceLoader.php +++ b/includes/resourceloader/ResourceLoader.php @@ -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 . '}' ); diff --git a/resources/src/startup/mediawiki.js b/resources/src/startup/mediawiki.js index 967c52992a..b5ba6a66a6 100644 --- a/resources/src/startup/mediawiki.js +++ b/resources/src/startup/mediawiki.js @@ -2341,9 +2341,11 @@ 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 ) @@ -2358,6 +2360,7 @@ } ).join( ',' ) + '}}'; } else { + // Array of urls, or null. encodedScript = JSON.stringify( descriptor.script ); } args = [ diff --git a/tests/phpunit/includes/resourceloader/ResourceLoaderClientHtmlTest.php b/tests/phpunit/includes/resourceloader/ResourceLoaderClientHtmlTest.php index dbc757f90b..70056baf62 100644 --- a/tests/phpunit/includes/resourceloader/ResourceLoaderClientHtmlTest.php +++ b/tests/phpunit/includes/resourceloader/ResourceLoaderClientHtmlTest.php @@ -225,7 +225,7 @@ Deprecation message.' ] . '' . "\n" @@ -343,7 +343,7 @@ Deprecation message.' ] 'context' => [], 'modules' => [ 'test.private' ], 'only' => ResourceLoaderModule::TYPE_COMBINED, - 'output' => '', + 'output' => '', ], [ 'context' => [], @@ -393,7 +393,7 @@ Deprecation message.' ] 'context' => [], 'modules' => [ 'test.shouldembed' ], 'only' => ResourceLoaderModule::TYPE_COMBINED, - 'output' => '', + 'output' => '', ], [ 'context' => [], @@ -411,7 +411,7 @@ Deprecation message.' ] 'context' => [], 'modules' => [ 'test', 'test.shouldembed' ], 'only' => ResourceLoaderModule::TYPE_COMBINED, - 'output' => '', + 'output' => '', ], [ 'context' => [],