@vitjs/runtime
Version:
@vitjs/runtime
59 lines (58 loc) • 2.1 kB
JavaScript
import get from 'lodash/get';
import React from 'react';
import Loadable from './loadable';
function FixedLoadable(options) {
return Loadable({
...options,
loader: () => {
return new Promise((resolve) => {
options.loader?.().then((module) => {
if (get(module, 'default')) {
// ref: https://stackoverflow.com/a/34130767/8335317
// 返回 module 页面报错,无法正常渲染
resolve(get(module, 'default'));
}
else {
return module;
}
});
});
},
});
}
export default function dynamic(options) {
const loadableFn = FixedLoadable;
let loadableOptions = {
loading: ({ error, isLoading }) => {
if (process.env.NODE_ENV === 'development') {
if (isLoading) {
return React.createElement("p", null, "loading...");
}
if (error) {
return (React.createElement("p", null,
error.message,
React.createElement("br", null),
error.stack));
}
}
return React.createElement("p", null, "loading...");
},
};
// Support for direct import(),
// eg: dynamic(() => import('../hello-world'))
if (typeof options === 'function') {
loadableOptions.loader = options;
// Support for having first argument being options,
// eg: dynamic({loader: import('../hello-world')})
}
else if (typeof options === 'object') {
loadableOptions = { ...loadableOptions, ...options };
}
else {
throw new Error(`Unexpected arguments ${options}`);
}
// Support for passing options,
// eg: dynamic(import('../hello-world'), {loading: () => <p>Loading something</p>})
// loadableOptions = { ...loadableOptions, ...options };
return loadableFn(loadableOptions);
}