From 3013ef126d304b2425887f8cd044a6504b779ad4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Mon, 22 Oct 2018 11:39:38 +0100 Subject: [PATCH] Update generate-pnp-map-api.tpl.js --- src/util/generate-pnp-map-api.tpl.js | 41 +++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/src/util/generate-pnp-map-api.tpl.js b/src/util/generate-pnp-map-api.tpl.js index f594353b07..dca837e40b 100644 --- a/src/util/generate-pnp-map-api.tpl.js +++ b/src/util/generate-pnp-map-api.tpl.js @@ -646,12 +646,45 @@ exports.setup = function setup() { if (!enableNativeHooks) { return originalModuleResolveFilename.call(Module, request, parent, isMain, options); } + + let issuers; + + if (options) { + const optionNames = new Set(Object.keys(optionNames)); + optionNames.delete('paths'); + + if (options.size > 0) { + throw makeError(`UNSUPPORTED`, `Some options passed to require() aren't supported by PnP yet (${Array.from(optionNames).join(', ')})`); + } + + if (options.paths) { + issuers = options.paths.map(entry => `${path.normalize(entry)}/`); + } + } + + if (!issuers) { + const issuerModule = getIssuerModule(parent); + const issuer = issuerModule ? issuerModule.filename : `${process.cwd()}/`; + + issuers = [issuer]; + } + + let firstError; - const issuerModule = getIssuerModule(parent); - const issuer = issuerModule ? issuerModule.filename : process.cwd() + '/'; + for (const issuer of issuers) { + let resolution; + + try { + resolution = exports.resolveRequest(request, issuer); + } catch (error) { + firstError = firstError || error; + continue; + } - const resolution = exports.resolveRequest(request, issuer); - return resolution !== null ? resolution : request; + return resolution !== null ? resolution : request; + } + + throw firstError; }; const originalFindPath = Module._findPath;