Krinkle has uploaded a new change for review. https://gerrit.wikimedia.org/r/227364
Change subject: resourceloader: Move startup code from PHP to startup.js ...................................................................... resourceloader: Move startup code from PHP to startup.js 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 --- M includes/resourceloader/ResourceLoaderStartUpModule.php M resources/src/startup.js 2 files changed, 32 insertions(+), 34 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core refs/changes/64/227364/1 diff --git a/includes/resourceloader/ResourceLoaderStartUpModule.php b/includes/resourceloader/ResourceLoaderStartUpModule.php index a578ece..aff469b 100644 --- a/includes/resourceloader/ResourceLoaderStartUpModule.php +++ b/includes/resourceloader/ResourceLoaderStartUpModule.php @@ -268,7 +268,7 @@ } // Register modules - $out .= ResourceLoader::makeLoaderRegisterScript( $registrations ); + $out .= "\n" . ResourceLoader::makeLoaderRegisterScript( $registrations ); return $out; } @@ -321,40 +321,25 @@ */ public function getScript( ResourceLoaderContext $context ) { global $IP; - - $out = file_get_contents( "$IP/resources/src/startup.js" ); - if ( $context->getOnly() === 'scripts' ) { - - // Startup function - $configuration = $this->getConfigSettings( $context ); - $registrations = $this->getModuleRegistrations( $context ); - // 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}"; + if ( $context->getOnly() !== 'scripts' ) { + return '/* Requires only=script */'; } - return $out; + $out = file_get_contents( "$IP/resources/src/startup.js" ); + + $pairs = array_map( function ( $value ) { + $value = FormatJson::encode( $value, ResourceLoader::inDebugMode(), FormatJson::ALL_OK ); + // Fix indentation + $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 ); } /** diff --git a/resources/src/startup.js b/resources/src/startup.js index 5ee295b..9592e5d 100644 --- a/resources/src/startup.js +++ b/resources/src/startup.js @@ -26,6 +26,7 @@ */ /*jshint unused: false */ +/*globals $VARS, $CODE */ function isCompatible( ua ) { if ( ua === undefined ) { ua = navigator.userAgent; @@ -68,5 +69,17 @@ } /** - * 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 ); +} -- To view, visit https://gerrit.wikimedia.org/r/227364 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I758acb78de1cdf2128e81c86f992807ef0dbf444 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/core Gerrit-Branch: master Gerrit-Owner: Krinkle <krinklem...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits