@exabytellc/app
Version:
EB react app to make everything a little easier!
47 lines (45 loc) • 1.53 kB
JavaScript
import React, { lazy } from "react";
import { jsx as _jsx } from "react/jsx-runtime";
export default function lazyImport(componentImport) {
let componentProps = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
let FallbackComponent = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : () => null;
return /*#__PURE__*/lazy(async () => {
try {
const module = await componentImport();
if (!module.default) {
console.warn("Lazy-loaded module has no default export. Using first available export.");
// Auto-pick first available export
const firstKey = Object.keys(module)[0];
if (firstKey) {
return {
default: props => {
const Component = module[firstKey];
return /*#__PURE__*/_jsx(Component, {
...props,
...componentProps
});
}
};
}
throw new Error("Lazy-loaded module does not have a default export.");
}
// If the module has a default export, wrap it to pass the props
return {
default: props => {
const Component = module.default;
return /*#__PURE__*/_jsx(Component, {
...props,
...componentProps
});
}
};
} catch (error) {
console.error("Lazy import failed:", error);
return {
default: () => /*#__PURE__*/_jsx(FallbackComponent, {
error: error
})
};
}
});
}