interface Module { exports: any; } type ModuleKey = `${string}@${string}`; type ModuleState = | "error" | "executing" | "loaded" | "loading" | "missing" | "ready" | "registered"; type ModuleMessages = Record; type ModuleStyle = Record; type ModuleTemplates = Record; interface ModuleDeclarator { (): [ module: string, script?: ModuleScript | null, style?: ModuleStyle | null, messages?: ModuleMessages | null, templates?: ModuleTemplates | null, deprecationWarning?: string | null ]; } interface ModuleRequire { /** * Get the exported value of a module. * * @param moduleName Module name * @returns Exported value */ (moduleName: string): any; } type ModuleScript = | string[] | (($: JQuery, jQuery: JQuery, require: ModuleRequire, module: Module) => void) | { files: { [key: string]: any }; main: string; } | string; interface ModuleRegistryEntry { /** * @since 1.41 */ declarator?: ModuleDeclarator | null; dependencies: string[]; /** * @since 1.41 */ deprecationWarning?: string | null; group: number | null; messages?: ModuleMessages | null; module: Module; packageExports: any; script?: ModuleScript | null; skip: string | null; source: string; state: "error" | "loaded" | "missing" | "registered" | "ready"; version: string; } interface JsonModuleStore { asOf: number; items: string; vary: string; } export interface ResourceLoaderStoreStats { expired: number; failed: number; hits: number; misses: number; } declare global { namespace mw { /** * Client for ResourceLoader server end point. * * This client is in charge of maintaining the module registry and state * machine, initiating network (batch) requests for loading modules, as * well as dependency resolution and execution of source code. * * For more information, refer to * {@link https://www.mediawiki.org/wiki/Special:MyLanguage/ResourceLoader/Features} * * @see https://doc.wikimedia.org/mediawiki-core/master/js/mw.loader.html */ namespace loader { /** * Create a new style element and add it to the DOM. * * @param text CSS text * @param nextNode The element where the style tag should be inserted before * @returns Reference to the created style element * @see https://doc.wikimedia.org/mediawiki-core/master/js/mw.loader.html#.addStyleTag */ function addStyleTag(text: string, nextNode?: Node | null): HTMLStyleElement; /** * Get the names of all registered ResourceLoader modules. * * @see https://doc.wikimedia.org/mediawiki-core/master/js/mw.loader.html#.getModuleNames */ function getModuleNames(): string[]; /** * Load a script by URL. * * @example * ```js * mw.loader.getScript( * 'https://example.org/x-1.0.0.js' * ) * .then( function () { * // Script succeeded. You can use X now. * }, function ( e ) { * // Script failed. X is not avaiable * mw.log.error( e.message ); // => "Failed to load script" * } ); * } ); * ``` * @param url Script URL * @returns Resolved when the script is loaded * @see https://doc.wikimedia.org/mediawiki-core/master/js/mw.loader.html#.getScript */ function getScript(url: string): JQuery.Promise; /** * Get the state of a module. * * Possible states for the public API: * * - `registered`: The module is available for loading but not yet requested. * - `loading`, `loaded`, or `executing`: The module is currently being loaded. * - `ready`: The module was successfully and fully loaded. * - `error`: The module or one its dependencies has failed to load, e.g. due to * uncaught error from the module's script files. * - `missing`: The module was requested but is not defined according to the server. * * Internal mw.loader state machine: * * - `registered`: * The module is known to the system but not yet required. * Meta data is stored by {@link register()}. * Calls to that method are generated server-side by StartupModule. * - `loading`: * The module was required through mw.loader (either directly or as dependency of * another module). The client will fetch module contents from {@link mw.loader.store} * or from the server. The contents should later be received by {@link implement()}. * - `loaded`: * The module has been received by {@link implement()}. * Once the module has no more dependencies in-flight, the module will be executed, * controlled via `setAndPropagate()` and `doPropagation()`. * - `executing`: * The module is being executed (apply messages and stylesheets, execute scripts) * by `execute()`. * - `ready`: * The module has been successfully executed. * - `error`: * The module (or one of its dependencies) produced an uncaught error during execution. * - `missing`: * The module was registered client-side and requested, but the server denied knowledge * of the module's existence. * * @param module Name of module * @returns The state, or null if the module (or its state) is not in the registry. * @see https://doc.wikimedia.org/mediawiki-core/master/js/mw.loader.html#.getState */ function getState(module: string): ModuleState | null; /** * Load an external script or one or more modules. * * This method takes a list of unrelated modules. Use cases: * * - A web page will be composed of many different widgets. These widgets independently * queue their ResourceLoader modules (`OutputPage::addModules()`). If any of them * have problems, or are no longer known (e.g. cached HTML), the other modules * should still be loaded. * - This method is used for preloading, which must not throw. Later code that * calls {@link using()} will handle the error. * * @param modules Either the name of a module, array of modules, * or a URL of an external script or style * @param type MIME type to use if calling with a URL of an * external script or style; acceptable values are "text/css" and * "text/javascript"; if no type is provided, `text/javascript` is assumed. * @throws {Error} If type is invalid * @see https://doc.wikimedia.org/mediawiki-core/master/js/mw.loader.html#.load */ function load(modules: string | string[], type?: "text/css" | "text/javascript"): void; /** * Execute a function after one or more modules are ready. * * Use this method if you need to dynamically control which modules are loaded * and/or when they loaded (instead of declaring them as dependencies directly * on your module.) * * This uses the same loader as for regular module dependencies. This means * ResourceLoader will not re-download or re-execute a module for the second * time if something else already needed it. And the same browser HTTP cache, * and localStorage are checked before considering to fetch from the network. * And any on-going requests from other dependencies or using() calls are also * automatically re-used. * * @example * ```js * // Inline dependency on OOjs * mw.loader.using( 'oojs', function () { * OO.compare( [ 1 ], [ 1 ] ); * } ); * ``` * @example * ```js * // Inline dependency obtained via require() * mw.loader.using( [ 'mediawiki.util' ], function ( require ) { * var util = require( 'mediawiki.util' ); * } ); * ``` * @since 1.23 - this returns a promise. * @since 1.28 - the promise is resolved with a `require` function. * @param dependencies Module name or array of modules names the * callback depends on to be ready before executing * @param ready Callback to execute when all dependencies are ready * @param error Callback to execute if one or more dependencies failed * @returns With a `require` function * @see https://doc.wikimedia.org/mediawiki-core/master/js/mw.loader.html#.using */ function using( dependencies: string | string[], ready?: (require: ModuleRequire) => void, error?: (error: Error, ...args: any[]) => void ): JQuery.Promise; /** * Exposed for testing and debugging only. * * @private */ const maxQueryLength: number; /** * The module registry is exposed as an aid for debugging and inspecting page * state; it is not a public interface for modifying the registry. * * @private */ const moduleRegistry: Record; /** * Utility function for execute() * * @private * @since 1.39 * @param url URL * @param media Media attribute * @param nextNode */ function addLinkTag( url: string, media?: string, nextNode?: Node | null ): HTMLLinkElement; /** * Load and execute a script. * * @private * @since 1.39 * @param src URL to script, will be used as the src attribute in the script tag * @param callback Callback to run after request resolution * @param modules List of modules being requested, for state to be marked as error * in case the script fails to load */ function addScriptTag( src: string, callback?: () => void, modules?: string[] ): HTMLScriptElement; /** * Register a source. * * The {@link work()} method will use this information to split up requests by source. * * @example * ```js * mw.loader.addSource( { mediawikiwiki: 'https://www.mediawiki.org/w/load.php' } ); * ``` * @private * @param ids An object mapping ids to load.php end point urls * @throws {Error} If source id is already registered */ function addSource(ids: Record): void; /** * Add one or more modules to the module load queue. * * See also {@link work()}. * * @private * @param dependencies Array of module names in the registry * @param ready Callback to execute when all dependencies are ready * @param error Callback to execute when any dependency fails */ function enqueue( dependencies: string[], ready?: () => void, error?: (error: Error, ...args: any[]) => void ): void; /** * Implement a module given a function which returns the components of the module * * @private * @since 1.41 * @param declarator The declarator should return an array with the following keys: * * - 0. {string} module Name of module and current module version. Formatted * as `[name]@[version]`. This version should match the requested version * (from #batchRequest and #registry). This avoids race conditions (T117587). * * - 1. {ModuleScript} [script] Module code. This can be a function, * a list of URLs to load via `