From 93a27630c28a3c8ded89852a4f9c4792de3fc525 Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Thu, 14 Sep 2023 14:48:33 -0700 Subject: [PATCH] ResourceLoader: Set 'virtualFilePath' for startup.js Source maps are now enabled on www.mediawiki.org, and one of the things that stands out is the (one) virtual file on every page view for startup.js. Bug: T47514 Change-Id: I7017de5757b22263f1076cfdcef0b5c9536dcc6a --- includes/ResourceLoader/StartUpModule.php | 55 +++++++++++++++-------- 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/includes/ResourceLoader/StartUpModule.php b/includes/ResourceLoader/StartUpModule.php index 984a0cd52f6..b19049384fe 100644 --- a/includes/ResourceLoader/StartUpModule.php +++ b/includes/ResourceLoader/StartUpModule.php @@ -358,9 +358,9 @@ class StartUpModule extends Module { /** * @param Context $context - * @return string JavaScript code + * @return string|array JavaScript code */ - public function getScript( Context $context ): string { + public function getScript( Context $context ) { global $IP; $conf = $this->getConfig(); @@ -368,6 +368,8 @@ class StartUpModule extends Module { return '/* Requires only=scripts */'; } + $enableJsProfiler = $conf->get( MainConfigNames::ResourceLoaderEnableJSProfiler ); + $startupCode = file_get_contents( "$IP/resources/src/startup/startup.js" ); // The files read here MUST be kept in sync with maintenance/jsduck/eg-iframe.html. @@ -405,23 +407,27 @@ class StartUpModule extends Module { '$VARS.sourceMapLinks' => $context->encodeJson( $conf->get( MainConfigNames::ResourceLoaderEnableSourceMapLinks ) ), + + // When profiling is enabled, insert the calls. + // When disabled (the default), insert nothing. + '$CODE.profileExecuteStart();' => $enableJsProfiler + ? 'mw.loader.profiler.onExecuteStart( module );' + : '', + '$CODE.profileExecuteEnd();' => $enableJsProfiler + ? 'mw.loader.profiler.onExecuteEnd( module );' + : '', + '$CODE.profileScriptStart();' => $enableJsProfiler + ? 'mw.loader.profiler.onScriptStart( module );' + : '', + '$CODE.profileScriptEnd();' => $enableJsProfiler + ? 'mw.loader.profiler.onScriptEnd( module );' + : '', + + // Debug stubs + '$CODE.consoleLog();' => $context->getDebug() + ? 'console.log.apply( console, arguments );' + : '', ]; - $profilerStubs = [ - '$CODE.profileExecuteStart();' => 'mw.loader.profiler.onExecuteStart( module );', - '$CODE.profileExecuteEnd();' => 'mw.loader.profiler.onExecuteEnd( module );', - '$CODE.profileScriptStart();' => 'mw.loader.profiler.onScriptStart( module );', - '$CODE.profileScriptEnd();' => 'mw.loader.profiler.onScriptEnd( module );', - ]; - $debugStubs = [ - '$CODE.consoleLog();' => 'console.log.apply( console, arguments );', - ]; - // When profiling is enabled, insert the calls. When disabled (by default), insert nothing. - $mwLoaderPairs += $conf->get( MainConfigNames::ResourceLoaderEnableJSProfiler ) - ? $profilerStubs - : array_fill_keys( array_keys( $profilerStubs ), '' ); - $mwLoaderPairs += $context->getDebug() - ? $debugStubs - : array_fill_keys( array_keys( $debugStubs ), '' ); $mwLoaderCode = strtr( $mwLoaderCode, $mwLoaderPairs ); // Perform string replacements for startup.js @@ -432,7 +438,18 @@ class StartUpModule extends Module { ]; $startupCode = strtr( $startupCode, $pairs ); - return $startupCode; + return [ + 'plainScripts' => [ + [ + 'virtualFilePath' => new FilePath( + 'resources/src/startup/startup.js', + MW_INSTALL_PATH, + $conf->get( MainConfigNames::ResourceBasePath ) + ), + 'content' => $startupCode, + ], + ], + ]; } /**