chore(packages/tuono): remove dynamic orphan files (#385)

This commit is contained in:
Marco Pasqualetti
2025-01-20 16:30:30 +01:00
committed by GitHub
parent 550830c232
commit f0d042c85b
2 changed files with 0 additions and 112 deletions
@@ -1,24 +0,0 @@
import { lazy, createElement } from 'react'
import type { ReactElement } from 'react'
import type { RouteComponent } from 'tuono-router'
type ImportFn = () => Promise<{ default: RouteComponent }>
export const RouteLazyLoading = (factory: ImportFn): RouteComponent => {
let LoadedComponent: RouteComponent | undefined
const LazyComponent = lazy<RouteComponent>(factory)
const loadComponent = (): Promise<void> =>
factory().then((module) => {
LoadedComponent = module.default
})
const Component = (
props: React.ComponentProps<RouteComponent>,
): ReactElement => createElement(LoadedComponent || LazyComponent, props)
Component.preload = loadComponent
return Component
}
-88
View File
@@ -1,88 +0,0 @@
/**
* This component is heavily inspired by Next.js dynamic function
* Link: https://github.com/vercel/next.js/blob/1df81bcea62800198884438a2bb27ba14c9d506a/packages/next/src/shared/lib/dynamic.tsx
*/
import { lazy, Suspense, Fragment } from 'react'
import type { ComponentType } from 'react'
const isServerSide = typeof window === 'undefined'
interface ComponentModule<T> {
default: React.ComponentType<T>
}
interface DynamicOptions {
ssr?: boolean
loading?: React.ComponentType<unknown> | null
}
type Loader<T = object> = () => Promise<
React.ComponentType<T> | ComponentModule<T>
>
interface LoadableOptions<T> extends DynamicOptions {
loader: Loader<T>
}
type LoadableFn = <T = object>(options: LoadableOptions<T>) => ComponentType<T>
const defaultLoaderOptions: LoadableOptions<object> = {
ssr: true,
loading: null,
loader: () => Promise.resolve(() => null),
}
function noSSR<T = object>(
LoadableInitializer: LoadableFn,
loadableOptions: LoadableOptions<T>,
): React.ComponentType<T> {
if (!isServerSide) {
return LoadableInitializer(loadableOptions)
}
if (!loadableOptions.loading) return () => null
const Loading = loadableOptions.loading
// This will only be rendered on the server side
function NoSSRLoading(): React.JSX.Element {
return <Loading />
}
return NoSSRLoading
}
function Loadable<T = object>(options: LoadableOptions<T>): ComponentType<T> {
const opts = { ...defaultLoaderOptions, ...options }
const Lazy = lazy(() => opts.loader().then())
const Loading = opts.loading
function LoadableComponent(props: T): React.JSX.Element {
const fallbackElement = Loading ? <Loading /> : null
const Wrap = Loading ? Suspense : Fragment
const wrapProps = Loading ? { fallback: fallbackElement } : {}
// TODO: In case ssr = false handle also the assets preloading
return (
<Wrap {...wrapProps}>
<Lazy {...props} />
</Wrap>
)
}
LoadableComponent.displayName = 'LoadableComponent'
return LoadableComponent
}
/**
* This function lets you dynamically import a component.
* It uses [React.lazy()](https://react.dev/reference/react/lazy) with [Suspense](https://react.dev/reference/react/Suspense) under the hood.
*/
export function dynamic<T = object>(
importFn: Loader<T>,
opts?: DynamicOptions,
): ComponentType<T> {
if (typeof opts?.ssr === 'boolean' && !opts.ssr) {
return noSSR<T>(Loadable, { ...opts, loader: importFn })
}
return Loadable<T>({ ...opts, loader: importFn })
}