From f38f88d0d39a69cff9a944f7e2d1848ba991291a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bartosz=20Dziewo=C5=84ski?= Date: Tue, 29 Jan 2019 04:33:14 +0100 Subject: [PATCH] resourceloader: Reduce 'implement' overhead for modules without scripts Change-Id: I55728c526711545be3923d3e1e2f276cbfa52224 --- includes/resourceloader/ResourceLoader.php | 4 +++- resources/src/startup/mediawiki.js | 5 ++++- .../resourceloader/ResourceLoaderClientHtmlTest.php | 8 ++++---- 3 files changed, 11 insertions(+), 6 deletions(-) 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' => [], -- 2.20.1