import React from 'react' import Loadable from './loadable.shared-runtime' const isServerSide = typeof window === 'undefined' type ComponentModule
= { default: React.ComponentType
} export declare type LoaderComponent
= Promise< React.ComponentType
| ComponentModule
> export declare type Loader
= | (() => LoaderComponent
) | LoaderComponent
export type LoaderMap = { [module: string]: () => Loader (mod: React.ComponentType | ComponentModule ) {
return { default: (mod as ComponentModule )?.default || mod }
}
export type DynamicOptions = LoadableGeneratedOptions & {
loading?: (loadingProps: DynamicOptionsLoadingProps) => React.ReactNode
loader?: Loader | LoaderMap
loadableGenerated?: LoadableGeneratedOptions
ssr?: boolean
}
export type LoadableOptions = DynamicOptions
export type LoadableFn = (
opts: LoadableOptions
) => React.ComponentType
export type LoadableComponent = React.ComponentType
export function noSSR (
LoadableInitializer: LoadableFn ,
loadableOptions: DynamicOptions
): React.ComponentType {
// Removing webpack and modules means react-loadable won't try preloading
delete loadableOptions.webpack
delete loadableOptions.modules
// This check is necessary to prevent react-loadable from initializing on the server
if (!isServerSide) {
return LoadableInitializer(loadableOptions)
}
const Loading = loadableOptions.loading!
// This will only be rendered on the server side
return () => (
(
dynamicOptions: DynamicOptions | Loader ,
options?: DynamicOptions
): React.ComponentType {
let loadableFn = Loadable as LoadableFn
let loadableOptions: LoadableOptions = {
// A loading component is not required, so we default it
loading: ({ error, isLoading, pastDelay }) => {
if (!pastDelay) return null
if (process.env.NODE_ENV !== 'production') {
if (isLoading) {
return null
}
if (error) {
return (
{error.message}
Loading something
const loader = () =>
loaderFn != null
? loaderFn().then(convertModule)
: Promise.resolve(convertModule(() => null))
// coming from build/babel/plugins/react-loadable-plugin.js
if (loadableOptions.loadableGenerated) {
loadableOptions = {
...loadableOptions,
...loadableOptions.loadableGenerated,
}
delete loadableOptions.loadableGenerated
}
// support for disabling server side rendering, eg: dynamic(() => import('../hello-world'), {ssr: false}).
if (typeof loadableOptions.ssr === 'boolean' && !loadableOptions.ssr) {
delete loadableOptions.webpack
delete loadableOptions.modules
return noSSR(loadableFn, loadableOptions)
}
return loadableFn({ ...loadableOptions, loader: loader as Loader })
}
{error.stack}