Nuxt/packages/nuxi/src/utils/cjs.ts

35 lines
953 B
TypeScript
Raw Normal View History

import { normalize, dirname } from 'pathe'
export function getModulePaths (paths?: string | string[]): string[] {
return [].concat(
// @ts-ignore
global.__NUXT_PREPATHS__,
...(Array.isArray(paths) ? paths : [paths]),
process.cwd(),
// @ts-ignore
global.__NUXT_PATHS__
).filter(Boolean)
2021-04-15 19:17:44 +00:00
}
export function resolveModule (id: string, paths?: string | string[]) {
return normalize(require.resolve(id, { paths: getModulePaths(paths) }))
}
export function tryResolveModule (id: string, paths?: string | string[]) {
try {
return resolveModule(id, paths)
} catch { return null }
}
export function requireModule (id: string, paths?: string | string[]) {
2021-04-15 19:17:44 +00:00
return require(resolveModule(id, paths))
}
export function getNearestPackage (id: string, paths?: string | string[]) {
while (dirname(id) !== id) {
try { return requireModule(id + '/package.json', paths) } catch { }
id = dirname(id)
}
return null
}