Commit 7dee8d12 authored by Luis Fernando Alvarez D's avatar Luis Fernando Alvarez D Committed by Tim Neutkens

Add types for next/dynamic (#7157)

* Add types for next/dynamic

* Updated types

* .d.ts files are now included by the taskfile of next-server
parent 3d28f7fe
...@@ -3,7 +3,42 @@ import Loadable from './loadable' ...@@ -3,7 +3,42 @@ import Loadable from './loadable'
const isServerSide = typeof window === 'undefined' const isServerSide = typeof window === 'undefined'
export function noSSR (LoadableInitializer, loadableOptions) { export type LoaderComponent<P = {}> = Promise<React.ComponentType<P> | { default: React.ComponentType<P> }>
export type Loader<P = {}> = (() => LoaderComponent<P>) | LoaderComponent<P>
export type LoaderMap = { [mdule: string]: () => Loader<any> }
export type LoadableGeneratedOptions = {
webpack?(): any,
modules?(): LoaderMap,
}
export type LoadableBaseOptions<P = {}> = LoadableGeneratedOptions & {
loading?: ({ error, isLoading, pastDelay }: {
error?: Error | null;
isLoading?: boolean;
pastDelay?: boolean;
timedOut?: boolean
}) => JSX.Element | null;
loader?: Loader<P> | LoaderMap
loadableGenerated?: LoadableGeneratedOptions
ssr?: boolean
}
export type LoadableOptions<P = {}> = LoadableBaseOptions<P> & {
render?(loader: any, props: any): JSX.Element,
}
export type DynamicOptions<P = {}> = LoadableBaseOptions<P> & {
render?(props: P, loaded: any): JSX.Element,
}
export type LoadableFn<P = {}> = (opts: LoadableOptions<P>) => React.ComponentType<P>
export type LoadableComponent<P = {}> = React.ComponentType<P>
export function noSSR<P = {}>(LoadableInitializer: LoadableFn<P>, loadableOptions: LoadableOptions<P>) {
// Removing webpack and modules means react-loadable won't try preloading // Removing webpack and modules means react-loadable won't try preloading
delete loadableOptions.webpack delete loadableOptions.webpack
delete loadableOptions.modules delete loadableOptions.modules
...@@ -13,17 +48,23 @@ export function noSSR (LoadableInitializer, loadableOptions) { ...@@ -13,17 +48,23 @@ export function noSSR (LoadableInitializer, loadableOptions) {
return LoadableInitializer(loadableOptions) return LoadableInitializer(loadableOptions)
} }
const Loading = loadableOptions.loading!
// This will only be rendered on the server side // This will only be rendered on the server side
return () => <loadableOptions.loading error={null} isLoading pastDelay={false} timedOut={false} /> return () => <Loading error={null} isLoading pastDelay={false} timedOut={false} />
} }
function DefaultLoading () { function DefaultLoading() {
return <p>loading...</p> return <p>loading...</p>
} }
export default function dynamic (dynamicOptions, options) { // function dynamic<P = {}, O extends DynamicOptions>(options: O):
let loadableFn = Loadable
let loadableOptions = { export default function dynamic<P = {}>(
dynamicOptions: DynamicOptions<P> | Loader<P>,
options?: DynamicOptions<P>,
): React.ComponentType<P> {
let loadableFn: LoadableFn<P> = Loadable
let loadableOptions: LoadableOptions<P> = {
// A loading component is not required, so we default it // A loading component is not required, so we default it
loading: ({ error, isLoading, pastDelay }) => { loading: ({ error, isLoading, pastDelay }) => {
if (!pastDelay) return null if (!pastDelay) return null
...@@ -37,14 +78,14 @@ export default function dynamic (dynamicOptions, options) { ...@@ -37,14 +78,14 @@ export default function dynamic (dynamicOptions, options) {
} }
return <DefaultLoading /> return <DefaultLoading />
} },
} }
// Support for direct import(), eg: dynamic(import('../hello-world')) // Support for direct import(), eg: dynamic(import('../hello-world'))
// Note that this is only kept for the edge case where someone is passing in a promise as first argument // Note that this is only kept for the edge case where someone is passing in a promise as first argument
// The react-loadable babel plugin will turn dynamic(import('../hello-world')) into dynamic(() => import('../hello-world')) // The react-loadable babel plugin will turn dynamic(import('../hello-world')) into dynamic(() => import('../hello-world'))
// To make sure we don't execute the import without rendering first // To make sure we don't execute the import without rendering first
if (typeof dynamicOptions.then === 'function') { if (dynamicOptions instanceof Promise) {
loadableOptions.loader = () => dynamicOptions loadableOptions.loader = () => dynamicOptions
// Support for having import as a function, eg: dynamic(() => import('../hello-world')) // Support for having import as a function, eg: dynamic(() => import('../hello-world'))
} else if (typeof dynamicOptions === 'function') { } else if (typeof dynamicOptions === 'function') {
...@@ -57,24 +98,26 @@ export default function dynamic (dynamicOptions, options) { ...@@ -57,24 +98,26 @@ export default function dynamic (dynamicOptions, options) {
// Support for passing options, eg: dynamic(import('../hello-world'), {loading: () => <p>Loading something</p>}) // Support for passing options, eg: dynamic(import('../hello-world'), {loading: () => <p>Loading something</p>})
loadableOptions = { ...loadableOptions, ...options } loadableOptions = { ...loadableOptions, ...options }
// Support for `render` when using a mapping, eg: `dynamic({ modules: () => {return {HelloWorld: import('../hello-world')}, render(props, loaded) {} } }) if (typeof dynamicOptions === 'object' && !(dynamicOptions instanceof Promise)) {
if (dynamicOptions.render) { // Support for `render` when using a mapping, eg: `dynamic({ modules: () => {return {HelloWorld: import('../hello-world')}, render(props, loaded) {} } })
loadableOptions.render = (loaded, props) => dynamicOptions.render(props, loaded) if (dynamicOptions.render) {
} loadableOptions.render = (loaded, props) => dynamicOptions.render!(props, loaded)
// Support for `modules` when using a mapping, eg: `dynamic({ modules: () => {return {HelloWorld: import('../hello-world')}, render(props, loaded) {} } }) }
if (dynamicOptions.modules) { // Support for `modules` when using a mapping, eg: `dynamic({ modules: () => {return {HelloWorld: import('../hello-world')}, render(props, loaded) {} } })
loadableFn = Loadable.Map if (dynamicOptions.modules) {
const loadModules = {} loadableFn = Loadable.Map
const modules = dynamicOptions.modules() const loadModules: LoaderMap = {}
Object.keys(modules).forEach(key => { const modules = dynamicOptions.modules()
const value = modules[key] Object.keys(modules).forEach((key) => {
if (typeof value.then === 'function') { const value = modules[key]
loadModules[key] = () => value.then(mod => mod.default || mod) if (value instanceof Promise) {
return loadModules[key] = () => value.then((mod: any) => mod.default || mod)
} return
loadModules[key] = value }
}) loadModules[key] = value
loadableOptions.loader = loadModules })
loadableOptions.loader = loadModules
}
} }
// coming from build/babel/plugins/react-loadable-plugin.js // coming from build/babel/plugins/react-loadable-plugin.js
......
/* tslint:disable */
import React from 'react'
declare namespace LoadableExport {
interface ILoadable {
<P = {}>(opts: any): React.ComponentClass<P>
Map<P = {}>(opts: any): React.ComponentType<P>
preloadAll(): Promise<any>
}
}
declare const LoadableExport: LoadableExport.ILoadable;
export = LoadableExport
...@@ -15,6 +15,10 @@ try { ...@@ -15,6 +15,10 @@ try {
} }
const ext = extname(file.base) const ext = extname(file.base)
// Include declaration files as they are
if (file.base.endsWith('.d.ts')) return
// For example files without an extension don't have to be rewritten // For example files without an extension don't have to be rewritten
if (ext) { if (ext) {
// Replace `.ts` with `.js` // Replace `.ts` with `.js`
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment