UNPKG

@graphql-tools/load

Version:

A set of utils for faster development of GraphQL tools

158 lines (157 loc) • 5.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.resolveLoaderModuleUrl = resolveLoaderModuleUrl; exports.getCustomLoaderByPath = getCustomLoaderByPath; exports.useCustomLoader = useCustomLoader; exports.useCustomLoaderSync = useCustomLoaderSync; const tslib_1 = require("tslib"); const fs_1 = require("fs"); const module_1 = require("module"); const path_1 = require("path"); const url_1 = require("url"); function extractLoaderFromModule(loaderModule) { if (loaderModule == null) { return undefined; } if (typeof loaderModule.default === 'function') { return loaderModule.default; } if (typeof loaderModule === 'function') { return loaderModule; } } function createLoaderRequire(cwd) { return (0, module_1.createRequire)((0, path_1.join)(cwd, 'noop.js')); } function isBarePackageSpecifier(specifier) { return !(0, path_1.isAbsolute)(specifier) && !specifier.startsWith('.') && !specifier.startsWith('file:'); } function findPackageDir(packageName, cwd) { let dir = cwd; while (true) { const candidate = (0, path_1.join)(dir, 'node_modules', ...packageName.split('/')); if ((0, fs_1.existsSync)((0, path_1.join)(candidate, 'package.json'))) { return candidate; } const parent = (0, path_1.dirname)(dir); if (parent === dir) { return undefined; } dir = parent; } } function stringFromConditionalExport(entry) { if (typeof entry === 'string') { return entry; } if (entry == null || typeof entry !== 'object') { return undefined; } const record = entry; const importEntry = record['import']; if (typeof importEntry === 'string') { return importEntry; } if (importEntry != null && typeof importEntry === 'object') { const nested = importEntry['default']; if (typeof nested === 'string') { return nested; } } const defaultEntry = record['default']; if (typeof defaultEntry === 'string') { return defaultEntry; } return undefined; } function packageImportEntry(exportsField) { if (typeof exportsField === 'string') { return exportsField; } if (exportsField == null || typeof exportsField !== 'object') { return undefined; } const record = exportsField; if (Object.prototype.hasOwnProperty.call(record, '.')) { return stringFromConditionalExport(record['.']); } return stringFromConditionalExport(record); } function resolvePackageImportPath(packageDir) { const pkg = JSON.parse((0, fs_1.readFileSync)((0, path_1.join)(packageDir, 'package.json'), 'utf8')); const entry = packageImportEntry(pkg.exports) ?? pkg.module ?? pkg.main; if (entry == null) { return undefined; } return (0, path_1.join)(packageDir, entry); } function resolveLoaderModuleUrl(path, cwd, requireFn) { try { return (0, url_1.pathToFileURL)(requireFn.resolve(path)).href; } catch { if (isBarePackageSpecifier(path)) { const packageDir = findPackageDir(path, cwd); const entry = packageDir != null ? resolvePackageImportPath(packageDir) : undefined; if (entry != null) { return (0, url_1.pathToFileURL)(entry).href; } } const absolutePath = (0, path_1.isAbsolute)(path) ? path : (0, path_1.join)(cwd, path); return (0, url_1.pathToFileURL)(absolutePath).href; } } async function getCustomLoaderByPath(path, cwd) { const requireFn = createLoaderRequire(cwd); try { const loader = extractLoaderFromModule(requireFn(path)); if (typeof loader === 'function') { return loader; } } catch { // createRequire cannot load ESM; fall through to import(). } try { const href = resolveLoaderModuleUrl(path, cwd, requireFn); const importedModule = await Promise.resolve(`${href}`).then(s => tslib_1.__importStar(require(s))); return extractLoaderFromModule(importedModule); } catch { return null; } } function getCustomLoaderByPathSync(path, cwd) { try { return extractLoaderFromModule(createLoaderRequire(cwd)(path)) ?? null; } catch { return null; } } async function useCustomLoader(loaderPointer, cwd) { let loader; if (typeof loaderPointer === 'string') { loader = await getCustomLoaderByPath(loaderPointer, cwd); } else if (typeof loaderPointer === 'function') { loader = loaderPointer; } if (typeof loader !== 'function') { throw new Error(`Failed to load custom loader: ${loaderPointer}`); } return loader; } function useCustomLoaderSync(loaderPointer, cwd) { let loader; if (typeof loaderPointer === 'string') { loader = getCustomLoaderByPathSync(loaderPointer, cwd); } else if (typeof loaderPointer === 'function') { loader = loaderPointer; } if (typeof loader !== 'function') { throw new Error(`Failed to load custom loader: ${loaderPointer}`); } return loader; }