packages/xmi-router/src/dynamic.js
function dynamicLoad(dynamicOptions, options, defaultOptions) {
    let loadableFn = Loadable
    let loadableOptions = defaultOptions

    if (typeof dynamicOptions.then === 'function') {
        // Support for direct import(),
        // eg: dynamic(import('../hello-world'))
        loadableOptions.loader = () => dynamicOptions
    } else if (typeof dynamicOptions === 'object') {
        // Support for having first argument being options,
        // eg: dynamic({loader: import('../hello-world')})
        loadableOptions = { ...loadableOptions, ...dynamicOptions }
    }

    // Support for passing options,
    // eg: dynamic(import('../hello-world'), {loading: () => <p>Loading something</p>})
    loadableOptions = { ...loadableOptions, ...options }

    // Support for `render` when using a mapping,
    // eg: `dynamic({ modules: () => {return {HelloWorld: import('../hello-world')}, 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) {
        loadableFn = Loadable.Map
        const loadModules = {}
        const modules = dynamicOptions.modules()
        Object.keys(modules).forEach(key => {
            const value = modules[key]
            if (typeof value.then === 'function') {
                loadModules[key] = () => value.then(mod => mod.default || mod)
                return
            }
            loadModules[key] = value
        })
        loadableOptions.loader = loadModules
    }

    return loadableFn(loadableOptions)
}
function dynamicLoad(dynamicOptions, options, defaultOptions) {
    let loadableFn = Loadable
    let loadableOptions = defaultOptions

    if (typeof dynamicOptions.then === 'function') {
        // Support for direct import(),
        // eg: dynamic(import('../hello-world'))
        loadableOptions.loader = () => dynamicOptions
    } else if (typeof dynamicOptions === 'object') {
        // Support for having first argument being options,
        // eg: dynamic({loader: import('../hello-world')})
        loadableOptions = { ...loadableOptions, ...dynamicOptions }
    }

    // Support for passing options,
    // eg: dynamic(import('../hello-world'), {loading: () => <p>Loading something</p>})
    loadableOptions = { ...loadableOptions, ...options }

    // Support for `render` when using a mapping,
    // eg: `dynamic({ modules: () => {return {HelloWorld: import('../hello-world')}, 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) {
        loadableFn = Loadable.Map
        const loadModules = {}
        const modules = dynamicOptions.modules()
        Object.keys(modules).forEach(key => {
            const value = modules[key]
            if (typeof value.then === 'function') {
                loadModules[key] = () => value.then(mod => mod.default || mod)
                return
            }
            loadModules[key] = value
        })
        loadableOptions.loader = loadModules
    }

    return loadableFn(loadableOptions)
}
function dynamicLoad ( dynamicOptions , options , defaultOptions ) { let loadableFn = Loadable let loadableOptions = defaultOptions if ( typeof dynamicOptions . then === 'function' ) { // Support for direct import(), // eg: dynamic(import('../hello-world')) loadableOptions . loader = ( ) => dynamicOptions } else if ( typeof dynamicOptions === 'object' ) { // Support for having first argument being options, // eg: dynamic({loader: import('../hello-world')}) loadableOptions = { ... loadableOptions , ... dynamicOptions } } // Support for passing options, // eg: dynamic(import('../hello-world'), {loading: () => <p>Loading something</p>}) loadableOptions = { ... loadableOptions , ... options } // Support for `render` when using a mapping, // eg: `dynamic({ modules: () => {return {HelloWorld: import('../hello-world')}, 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 ) { loadableFn = Loadable . Map const loadModules = { } const modules = dynamicOptions . modules ( ) Object . keys ( modules ) . forEach ( key => { const value = modules [ key ] if ( typeof value . then === 'function' ) { loadModules [ key ] = ( ) => value . then ( mod => mod . default || mod ) return } loadModules [ key ] = value } ) loadableOptions . loader = loadModules } return loadableFn ( loadableOptions ) }
Thanks to umi
Thanks to umi