UNPKG

moduleraid

Version:
188 lines (187 loc) 6.31 kB
import { AnyFunction, ConstructorModuleTuple, ModuleRaidParameters, WebpackModule, WebpackModuleList, WebpackRequire } from './types'; /** * Main moduleRaid class */ export declare class ModuleRaid { /** * The Webpack entrypoint present on the global window object * * @default `webpackJsonp` */ private entrypoint; /** * Option enabling or disabling debug output * * @default `false` */ private debug; /** * Option enabling strict mode (only defined entrypoint) or entrypoint guessing */ private strict; /** * A random generated module ID we use for injecting into Webpack */ private moduleID; /** * An array containing different argument injection methods for * Webpack (before version 4), and subsequently pulling out methods and modules * @internal */ private functionArguments; /** * An array containing different argument injection methods for * Webpack (after version 4), and subsequently pulling out methods and modules * @internal */ private arrayArguments; /** * Storage for the modules we extracted from Webpack */ modules: WebpackModuleList; /** * Storage for the constructors we extracted from Webpack */ constructors: AnyFunction[]; /** * Intermediary storage for __webpack_require__ if we were able to extract it */ get?: WebpackRequire; /** * moduleRaid constructor * * @example * Constructing an instance without any arguments: * ```ts * const mR = new ModuleRaid() * ``` * * Constructing an instance with the optional `opts` object: * ```ts * const mR = new ModuleRaid({ entrypoint: 'webpackChunk_custom_name' }) * ``` * * @param opts a object containing options to initialize moduleRaid with * - **opts:** * - _entrypoint_: the Webpack entrypoint present on the global window object * - _debug_: whether debug mode is enabled or not * - _strict_: whether strict mode is enabled or not */ constructor(opts?: ModuleRaidParameters | boolean); /** * Debug logging method, outputs to the console when {@link ModuleRaid.debug} is true * * @param {*} message The message to be logged * @internal */ private log; /** * Method to set an alternative getter if we weren't able to extract __webpack_require__ * from Webpack * @internal */ private replaceGet; /** * Method that will try to inject a module into Webpack or get modules * depending on it's success it might be more or less brute about it * @internal */ private fillModules; /** * Method to hook into `window[this.entrypoint].push` adding a listener for new * chunks being pushed into Webpack * * @example * You can listen for newly pushed packages using the `moduleraid:webpack-push` event * on `document` * * ```ts * document.addEventListener('moduleraid:webpack-push', (e) => { * // e.detail contains the arguments push() was called with * console.log(e.detail) * }) * ``` * @internal */ private setupPushEvent; /** * Method to try autodetecting a Webpack JSONP entrypoint based on common naming * * If the default entrypoint, or the entrypoint that's passed to the moduleRaid constructor * already matches, the method exits early * * If `options.strict` has been set in the constructor and the initial entrypoint cannot * be found, this method will error, demanding a strictly set entrypoint * @internal */ private detectEntrypoint; /** * Recursive object-search function for modules * * @param object the object to search through * @param query the query the object keys/values are searched for * @returns boolean state of `object` containing `query` somewhere in it * @internal */ private searchObject; /** * Method to search through the module object, searching for the fitting content * if a string is supplied * * If query is supplied as a function, everything that returns true when passed * to the query function will be returned * * @example * With a string as query argument: * ```ts * const results = mR.findModule('feature') * // => Array of module results * ``` * * With a function as query argument: * ```ts * const results = mR.findModule((module) => { typeof module === 'function' }) * // => Array of module results * ``` * * @param query query to search the module list for * @return a list of modules fitting the query */ findModule(query: string | ((query: WebpackModule) => boolean)): WebpackModule[]; /** * Method to search through the constructor array, searching for the fitting content * if a string is supplied * * If query is supplied as a function, everything that returns true when passed * to the query function will be returned * * @example * With a string as query argument: * ```ts * const results = mR.findConstructor('feature') * // => Array of constructor/module tuples * ``` * * With a function as query argument: * ```ts * const results = mR.findConstructor((constructor) => { constructor.prototype.value !== undefined }) * // => Array of constructor/module tuples * ``` * * Accessing the resulting data: * ```ts * // With array destructuring (ES6) * const [constructor, module] = results[0] * * // ...or... * * // regular access * const constructor = results[0][0] * const module = results[0][1] * ``` * * @param query query to search the constructor list for * @returns a list of constructor/module tuples fitting the query */ findConstructor(query: string | ((query: WebpackModule) => boolean)): ConstructorModuleTuple[]; }