feat: 增加动态组件

This commit is contained in:
ningmengchongshui 2024-06-18 10:49:03 +08:00
parent 4b2555de4a
commit 16acf39012
1 changed files with 36 additions and 7 deletions

View File

@ -1,12 +1,10 @@
import { createRequire as cRequire } from 'module' import { createRequire } from 'module'
/** /**
* @deprecated * @deprecated
* @param basePath * @returns
*/ */
export function createRequire(basePath: string) { export { createRequire }
return cRequire(basePath)
}
/** /**
* @deprecated * @deprecated
@ -15,7 +13,7 @@ export function createRequire(basePath: string) {
*/ */
export function require(path: string) { export function require(path: string) {
return (url: string) => { return (url: string) => {
return cRequire(url)(path) return createRequire(url)(path)
} }
} }
@ -44,7 +42,38 @@ export const createDynamic = (basePath: string) => {
* @param path * @param path
* @returns * @returns
*/ */
return (path: string) => return <T = any>(path: string): Promise<T> =>
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<PropsType> }>
/**
* @param basePath import.meta.url
* @returns
* ***********
*
* ***********
* env.NODE_ENV=='production'
*/
export const createDynamicComponent = (basePath: string) => {
/**
* import作用相同
* @param path
* @returns
*/
return <D extends string, T = any>(path: string): ModuleWithComponent<D, T> =>
import( import(
new URL( new URL(
`${path}${process.env.NODE_ENV == 'production' ? '' : now()}`, `${path}${process.env.NODE_ENV == 'production' ? '' : now()}`,