resourceloader: Move startup code from PHP to startup.js
authorTimo Tijhof <krinklemail@gmail.com>
Mon, 27 Jul 2015 22:47:05 +0000 (15:47 -0700)
committerTimo Tijhof <krinklemail@gmail.com>
Mon, 27 Jul 2015 23:41:16 +0000 (16:41 -0700)
The code is easier to maintain in an actual JavaScript file.
Especially with how variables were declared and concatenated in
a different order.

Change-Id: I758acb78de1cdf2128e81c86f992807ef0dbf444

includes/resourceloader/ResourceLoaderStartUpModule.php
resources/src/startup.js
tests/phpunit/includes/resourceloader/ResourceLoaderStartUpModuleTest.php

index a578ece..16424a0 100644 (file)
@@ -268,7 +268,7 @@ class ResourceLoaderStartUpModule extends ResourceLoaderModule {
                }
 
                // Register modules
-               $out .= ResourceLoader::makeLoaderRegisterScript( $registrations );
+               $out .= "\n" . ResourceLoader::makeLoaderRegisterScript( $registrations );
 
                return $out;
        }
@@ -321,40 +321,25 @@ class ResourceLoaderStartUpModule extends ResourceLoaderModule {
         */
        public function getScript( ResourceLoaderContext $context ) {
                global $IP;
+               if ( $context->getOnly() !== 'scripts' ) {
+                       return '/* Requires only=script */';
+               }
 
                $out = file_get_contents( "$IP/resources/src/startup.js" );
-               if ( $context->getOnly() === 'scripts' ) {
 
-                       // Startup function
-                       $configuration = $this->getConfigSettings( $context );
-                       $registrations = $this->getModuleRegistrations( $context );
+               $pairs = array_map( function ( $value ) {
+                       $value = FormatJson::encode( $value, ResourceLoader::inDebugMode(), FormatJson::ALL_OK );
                        // Fix indentation
-                       $registrations = str_replace( "\n", "\n\t", trim( $registrations ) );
-                       $mwMapJsCall = Xml::encodeJsCall(
-                               'mw.Map',
-                               array( $this->getConfig()->get( 'LegacyJavaScriptGlobals' ) )
-                       );
-                       $mwConfigSetJsCall = Xml::encodeJsCall(
-                               'mw.config.set',
-                               array( $configuration ),
-                               ResourceLoader::inDebugMode()
-                       );
-
-                       $out .= "var startUp = function () {\n" .
-                               "\tmw.config = new " .
-                               $mwMapJsCall . "\n" .
-                               "\t$registrations\n" .
-                               "\t" . $mwConfigSetJsCall .
-                               "};\n";
-
-                       // Conditional script injection
-                       $scriptTag = Html::linkedScript( self::getStartupModulesUrl( $context ) );
-                       $out .= "if ( isCompatible() ) {\n" .
-                               "\t" . Xml::encodeJsCall( 'document.write', array( $scriptTag ) ) .
-                               "\n}";
-               }
-
-               return $out;
+                       $value = str_replace( "\n", "\n\t", $value  );
+                       return $value;
+               }, array(
+                       '$VARS.wgLegacyJavaScriptGlobals' => $this->getConfig()->get( 'LegacyJavaScriptGlobals' ),
+                       '$VARS.configuration' => $this->getConfigSettings( $context ),
+                       '$VARS.baseModulesScript' => Html::linkedScript( self::getStartupModulesUrl( $context ) ),
+               ) );
+               $pairs['$CODE.registrations()'] = str_replace( "\n", "\n\t", trim( $this->getModuleRegistrations( $context ) ) );
+
+               return strtr( $out, $pairs );
        }
 
        /**
index 5ee295b..ac42854 100644 (file)
@@ -25,7 +25,8 @@ performance.mark( 'mediaWikiStartUp' );
  * - https://jquery.com/browser-support/
  */
 
-/*jshint unused: false */
+/*jshint unused: false, evil: true */
+/*globals mw, $VARS, $CODE */
 function isCompatible( ua ) {
        if ( ua === undefined ) {
                ua = navigator.userAgent;
@@ -68,5 +69,17 @@ function isCompatible( ua ) {
 }
 
 /**
- * The startUp() function will be auto-generated and added below.
+ * The $CODE and $VARS placeholders are substituted in ResourceLoaderStartUpModule.php.
  */
+function startUp() {
+       mw.config = new mw.Map( $VARS.wgLegacyJavaScriptGlobals );
+
+       $CODE.registrations();
+
+       mw.config.set( $VARS.configuration );
+}
+
+// Conditional script injection
+if ( isCompatible() ) {
+       document.write( $VARS.baseModulesScript );
+}
index 490f5c6..cb91614 100644 (file)
@@ -10,7 +10,8 @@ class ResourceLoaderStartUpModuleTest extends ResourceLoaderTestCase {
                                'out' => '
 mw.loader.addSource( {
     "local": "/w/load.php"
-} );mw.loader.register( [] );'
+} );
+mw.loader.register( [] );'
                        ) ),
                        array( array(
                                'msg' => 'Basic registry',
@@ -20,7 +21,8 @@ mw.loader.addSource( {
                                'out' => '
 mw.loader.addSource( {
     "local": "/w/load.php"
-} );mw.loader.register( [
+} );
+mw.loader.register( [
     [
         "test.blank",
         "wvTifjse"
@@ -37,7 +39,8 @@ mw.loader.addSource( {
                                'out' => '
 mw.loader.addSource( {
     "local": "/w/load.php"
-} );mw.loader.register( [
+} );
+mw.loader.register( [
     [
         "test.blank",
         "wvTifjse"
@@ -65,7 +68,8 @@ mw.loader.addSource( {
                                'out' => '
 mw.loader.addSource( {
     "local": "/w/load.php"
-} );mw.loader.register( [
+} );
+mw.loader.register( [
     [
         "test.blank",
         "wvTifjse"
@@ -87,7 +91,8 @@ mw.loader.addSource( {
 mw.loader.addSource( {
     "local": "/w/load.php",
     "example": "http://example.org/w/load.php"
-} );mw.loader.register( [
+} );
+mw.loader.register( [
     [
         "test.blank",
         "wvTifjse",
@@ -123,7 +128,8 @@ mw.loader.addSource( {
                                'out' => '
 mw.loader.addSource( {
     "local": "/w/load.php"
-} );mw.loader.register( [
+} );
+mw.loader.register( [
     [
         "test.x.core",
         "wvTifjse"
@@ -219,7 +225,8 @@ mw.loader.addSource( {
 mw.loader.addSource( {
     "local": "/w/load.php",
     "example": "http://example.org/w/load.php"
-} );mw.loader.register( [
+} );
+mw.loader.register( [
     [
         "test.blank",
         "wvTifjse"
@@ -342,7 +349,7 @@ mw.loader.addSource( {
                $rl->register( $modules );
                $module = new ResourceLoaderStartUpModule();
                $this->assertEquals(
-'mw.loader.addSource({"local":"/w/load.php"});'
+'mw.loader.addSource({"local":"/w/load.php"});' . "\n"
 . 'mw.loader.register(['
 . '["test.blank","wvTifjse"],'
 . '["test.min","wvTifjse",[0],null,null,'
@@ -364,7 +371,8 @@ mw.loader.addSource( {
                $this->assertEquals(
 'mw.loader.addSource( {
     "local": "/w/load.php"
-} );mw.loader.register( [
+} );
+mw.loader.register( [
     [
         "test.blank",
         "wvTifjse"