diff --git a/src/utils/module.ts b/src/utils/module.ts index 61f054f..d47d526 100644 --- a/src/utils/module.ts +++ b/src/utils/module.ts @@ -1,12 +1,10 @@ -import { createRequire as cRequire } from 'module' +import { createRequire } from 'module' /** * @deprecated 已废弃 - * @param basePath + * @returns */ -export function createRequire(basePath: string) { - return cRequire(basePath) -} +export { createRequire } /** * @deprecated 已废弃 @@ -15,7 +13,7 @@ export function createRequire(basePath: string) { */ export function require(path: string) { return (url: string) => { - return cRequire(url)(path) + return createRequire(url)(path) } } @@ -44,7 +42,38 @@ export const createDynamic = (basePath: string) => { * @param path 相对路径 * @returns */ - return (path: string) => + return (path: string): Promise => + import( + new URL( + `${path}${process.env.NODE_ENV == 'production' ? '' : now()}`, + basePath + ).href + ) +} + +/** + * 动态组件类型 + */ +export type ModuleWithComponent< + ComponentName extends string, + PropsType = any +> = Promise<{ [K in ComponentName]: React.ComponentType }> + +/** + * @param basePath import.meta.url + * @returns + * *********** + * 创建动态组件 + * *********** + * 在env.NODE_ENV=='production'下禁用 + */ +export const createDynamicComponent = (basePath: string) => { + /** + * 与import作用相同 + * @param path 相对路径 + * @returns + */ + return (path: string): ModuleWithComponent => import( new URL( `${path}${process.env.NODE_ENV == 'production' ? '' : now()}`,