resourceloader: Support isKnownEmpty for general modules
authorTimo Tijhof <krinklemail@gmail.com>
Mon, 18 Sep 2017 17:37:13 +0000 (18:37 +0100)
committerTimo Tijhof <krinklemail@gmail.com>
Mon, 18 Sep 2017 17:37:13 +0000 (18:37 +0100)
It was already supported for only=scripts and only=styles, but
not yet for general modules loaded as 'combined'.

This is especially useful for general modules that are private/embedded,
as this allows us to skip the loader.implement() boilerplate.

Bug: T176159
Change-Id: I83e9f74dad75867c10ebd0a94b87c31e4adbb57f

includes/resourceloader/ResourceLoaderClientHtml.php
tests/phpunit/includes/resourceloader/ResourceLoaderClientHtmlTest.php

index 06f9841..c76ed91 100644 (file)
@@ -149,6 +149,13 @@ class ResourceLoaderClientHtml {
                                continue;
                        }
 
+                       $context = $this->getContext( $module->getGroup(), ResourceLoaderModule::TYPE_COMBINED );
+                       if ( $module->isKnownEmpty( $context ) ) {
+                               // Avoid needless request or embed for empty module
+                               $data['states'][$name] = 'ready';
+                               continue;
+                       }
+
                        if ( $module->shouldEmbedModule( $this->context ) ) {
                                // Embed via mw.loader.implement per T36907.
                                $data['embed']['general'][] = $name;
index 3530d3c..eaae9dc 100644 (file)
@@ -43,6 +43,7 @@ class ResourceLoaderClientHtmlTest extends PHPUnit_Framework_TestCase {
                        'test.top' => [ 'position' => 'top' ],
                        'test.private.top' => [ 'group' => 'private', 'position' => 'top' ],
                        'test.private.bottom' => [ 'group' => 'private', 'position' => 'bottom' ],
+                       'test.shouldembed.empty' => [ 'shouldEmbed' => true, 'isKnownEmpty' => true ],
                        'test.shouldembed' => [ 'shouldEmbed' => true ],
 
                        'test.styles.pure' => [ 'type' => ResourceLoaderModule::LOAD_STYLES ],
@@ -115,6 +116,7 @@ class ResourceLoaderClientHtmlTest extends PHPUnit_Framework_TestCase {
                        'test.private.bottom',
                        'test.private.top',
                        'test.top',
+                       'test.shouldembed.empty',
                        'test.shouldembed',
                        'test.unregistered',
                ] );
@@ -138,6 +140,7 @@ class ResourceLoaderClientHtmlTest extends PHPUnit_Framework_TestCase {
                        'states' => [
                                'test.private.top' => 'loading',
                                'test.private.bottom' => 'loading',
+                               'test.shouldembed.empty' => 'ready',
                                'test.shouldembed' => 'loading',
                                'test.styles.pure' => 'ready',
                                'test.styles.user.empty' => 'ready',