2023-03-10 14:55:01 +00:00
|
|
|
import { pathToFileURL } from 'node:url'
|
|
|
|
import { interopDefault, resolvePath } from 'mlly'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolve a module from a given root path using an algorithm patterned on
|
|
|
|
* the upcoming `import.meta.resolve`. It returns a file URL
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*/
|
2023-03-13 10:14:27 +00:00
|
|
|
export async function tryResolveModule (id: string, url: string | string[] = import.meta.url) {
|
2023-03-10 14:55:01 +00:00
|
|
|
try {
|
|
|
|
return await resolvePath(id, { url })
|
|
|
|
} catch { }
|
|
|
|
}
|
|
|
|
|
2023-03-13 10:14:27 +00:00
|
|
|
export async function importModule (id: string, url: string | string[] = import.meta.url) {
|
2023-03-10 14:55:01 +00:00
|
|
|
const resolvedPath = await resolvePath(id, { url })
|
|
|
|
return import(pathToFileURL(resolvedPath).href).then(interopDefault)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function tryImportModule (id: string, url = import.meta.url) {
|
|
|
|
try {
|
|
|
|
return importModule(id, url).catch(() => undefined)
|
|
|
|
} catch { }
|
|
|
|
}
|