$chunks = [];
// Change "client-nojs" class to client-js. This allows easy toggling of UI components.
- // This happens synchronously on every page view to avoid flashes of wrong content.
+ // This must happen synchronously on every page view to avoid flashes of wrong content.
// See also #getDocumentAttributes() and /resources/src/startup.js.
- $chunks[] = Html::inlineScript(
- 'document.documentElement.className = document.documentElement.className'
- . '.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );',
- $nonce
- );
+ $script = <<<JAVASCRIPT
+document.documentElement.className = document.documentElement.className
+ .replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );
+JAVASCRIPT;
- // Inline RLQ: Set page variables
+ // Inline script: Declare mw.config variables for this page.
if ( $this->config ) {
- $chunks[] = ResourceLoader::makeInlineScript(
- ResourceLoader::makeConfigSetScript( $this->config ),
- $nonce
- );
+ $confJson = ResourceLoader::encodeJsonForScript( $this->config );
+ $script .= <<<JAVASCRIPT
+RLCONF = {$confJson};
+JAVASCRIPT;
}
- // Inline RLQ: Initial module states
+ // Inline script: Declare initial module states for this page.
$states = array_merge( $this->exemptStates, $data['states'] );
if ( $states ) {
- $chunks[] = ResourceLoader::makeInlineScript(
- ResourceLoader::makeLoaderStateScript( $states ),
+ $stateJson = ResourceLoader::encodeJsonForScript( $states );
+ $script .= <<<JAVASCRIPT
+RLSTATE = {$stateJson};
+JAVASCRIPT;
+ }
+
+ // Inline script: Declare general modules to load on this page.
+ if ( $data['general'] ) {
+ $pageModulesJson = ResourceLoader::encodeJsonForScript( $data['general'] );
+ $script .= <<<JAVASCRIPT
+RLPAGEMODULES = {$pageModulesJson};
+JAVASCRIPT;
+ }
+
+ if ( $this->context->getDebug() ) {
+ $chunks[] = Html::inlineScript( $script, $nonce );
+ } else {
+ $chunks[] = Html::inlineScript(
+ ResourceLoader::filter( 'minify-js', $script, [ 'cache' => false ] ),
$nonce
);
}
);
}
- // Inline RLQ: Load general modules
- if ( $data['general'] ) {
- $chunks[] = ResourceLoader::makeInlineScript(
- 'RLPAGEMODULES='
- . ResourceLoader::encodeJsonForScript( $data['general'] )
- . ';'
- . 'mw.loader.load(RLPAGEMODULES);',
- $nonce
- );
- }
-
// External stylesheets (only=styles)
if ( $data['styles'] ) {
$chunks[] = $this->getLoad(
// Async scripts. Once the startup is loaded, inline RLQ scripts will run.
// Pass-through a custom 'target' from OutputPage (T143066).
- $startupQuery = [];
+ $startupQuery = [ 'raw' => '1' ];
foreach ( [ 'target', 'safemode' ] as $param ) {
if ( $this->options[$param] !== null ) {
$startupQuery[$param] = (string)$this->options[$param];
private static function makeContext( ResourceLoaderContext $mainContext, $group, $type,
array $extraQuery = []
) {
- // Create new ResourceLoaderContext so that $extraQuery may trigger isRaw().
+ // Create new ResourceLoaderContext so that $extraQuery is supported (eg. for 'sync=1').
$req = new FauxRequest( array_merge( $mainContext->getRequest()->getValues(), $extraQuery ) );
// Set 'only' if not combined
$req->setVal( 'only', $type === ResourceLoaderModule::TYPE_COMBINED ? null : $type );
);
}
} else {
- // See if we have one or more raw modules
- $isRaw = false;
- foreach ( $moduleSet as $key => $module ) {
- $isRaw |= $module->isRaw();
- }
-
// Special handling for the user group; because users might change their stuff
// on-wiki like user pages, or user preferences; we need to find the highest
// timestamp of these user-changeable modules so we can ensure cache misses on change
// Decide whether to use 'style' or 'script' element
if ( $only === ResourceLoaderModule::TYPE_STYLES ) {
$chunk = Html::linkedStyle( $url );
- } elseif ( $context->getRaw() || $isRaw ) {
+ } elseif ( $context->getRaw() ) {
+ // This request is asking for the module to be delivered standalone,
+ // (aka "raw") without communicating to any mw.loader client.
+ // Use cases:
+ // - startup (naturally because this is what will define mw.loader)
+ // - html5shiv (loads synchronously in old IE before the async startup module arrives)
+ // - QUnit (needed in SpecialJavaScriptTest before async startup)
$chunk = Html::element( 'script', [
- // In SpecialJavaScriptTest, QUnit must load synchronous
+ // The 'sync' option is only supported in combination with 'raw'.
'async' => !isset( $extraQuery['sync'] ),
'src' => $url
] );