import { ComponentType, FC, ReactNode } from "react"; //#region src/index.d.ts interface LoadingComponentProps { isLoading: boolean; pastDelay: boolean; timedOut: boolean; error: unknown; retry: () => void; } interface CommonOptions { /** * React component displayed after delay until loader() succeeds. Also responsible for displaying errors. * * If you don't want to render anything you can pass a function that returns null * (this is considered a valid React component). */ loading: ComponentType; /** * Defaults to 200, in milliseconds. * * Only show the loading component if the loader() has taken this long to succeed or error. */ delay?: number | false | null; /** * Disabled by default. * * After the specified time in milliseconds passes, the component's `timedOut` prop will be set to true. */ timeout?: number | false | null; /** * Optional array of module paths that `Loadable.Capture`'s `report` function will be applied on during * server-side rendering. This helps the server know which modules were imported/used during SSR. * ```ts * Loadable({ * loader: () => import('./my-component'), * modules: ['./my-component'], * }); * ``` */ modules?: string[] | null; /** * An optional function which returns an array of Webpack module ids which you can get * with require.resolveWeak. This is used by the client (inside `Loadable.preloadReady`) to * guarantee each webpack module is preloaded before the first client render. * ```ts * Loadable({ * loader: () => import('./Foo'), * webpack: () => [require.resolveWeak('./Foo')], * }); * ``` */ webpack?: (() => (string | number)[]) | null; } type ResolvableComponent = ComponentType | { default: ComponentType; __esModule?: boolean; }; interface OptionsWithoutRender extends CommonOptions { /** * Function returning a promise which returns a React component displayed on success. * * Resulting React component receives all the props passed to the generated component. */ loader: () => Promise>; } interface OptionsWithRender extends CommonOptions { /** * Function returning a promise which returns an object to be passed to `render` on success. */ loader: () => Promise; /** * If you want to customize what gets rendered from your loader you can also pass `render`. * * Note: If you want to load multiple resources at once, you can also use `Loadable.Map`. * * ```ts * Loadable({ * // ... * render(loaded, props) { * const Component = loaded.default; * return * } * }); * ``` */ render(loaded: Exports, props: Props): ReactNode; } type Options = OptionsWithoutRender | OptionsWithRender; interface OptionsWithMap> extends CommonOptions { /** * An object containing functions which return promises, which resolve to an object to be passed to `render` on success. */ loader: { [P in keyof Exports]: () => Promise; }; /** * If you want to customize what gets rendered from your loader you can also pass `render`. * * Note: If you want to load multiple resources at once, you can also use `Loadable.Map`. * * ```ts * Loadable({ * // ... * render(loaded, props) { * const Component = loaded.default; * return * } * }); * ``` */ render(loaded: Exports, props: Props): ReactNode; } interface LoadableComponent { /** * The generated component has a static method preload() for calling the loader function ahead of time. * This is useful for scenarios where you think the user might do something next and want to load the * next component eagerly. * * Note: This returns a promise, but you should avoid waiting for that promise to resolve * to update your UI. In most cases doing so creates a bad user experience. */ preload(): Promise; } interface LoadableCaptureProps { /** * Function called for every moduleName that is rendered via React Loadable. */ report: (moduleName: string) => void; children: ReactNode; } declare function Loadable(options: OptionsWithRender): ComponentType & LoadableComponent; declare function Loadable(options: OptionsWithoutRender): ComponentType & LoadableComponent; declare namespace Loadable { export let Map: typeof LoadableMap; export let Capture: FC; /** * This will call all of the LoadableComponent.preload methods recursively until they are all * resolved. Allowing you to preload all of your dynamic modules in environments like the server. * ```ts * Loadable.preloadAll().then(() => { * app.listen(3000, () => { * console.log('Running on http://localhost:3000/'); * }); * }); * ``` */ export let preloadAll: () => Promise; /** * Check for modules that are already loaded in the browser and call the matching * `LoadableComponent.preload` methods. * ```ts * window.main = () => { * Loadable.preloadReady().then(() => { * ReactDOM.hydrate( * , * document.getElementById('app'), * ); * }); * }; * ``` */ export let preloadReady: () => Promise; } declare function LoadableMap>(options: OptionsWithMap): ComponentType & LoadableComponent; //#endregion export { CommonOptions, LoadableCaptureProps, LoadableComponent, LoadingComponentProps, Options, OptionsWithMap, OptionsWithRender, OptionsWithoutRender, Loadable as default };