From 16acf39012803bb1cf1d76b153c705f29a46d506 Mon Sep 17 00:00:00 2001 From: ningmengchongshui <916415899@qq.com> Date: Tue, 18 Jun 2024 10:49:03 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E5=8A=A8=E6=80=81?= =?UTF-8?q?=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/module.ts | 43 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-) 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()}`,