From 7b03715485bf421a4aef1b2010c9a24599deeedd Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 27 Oct 2021 15:24:04 +0200 Subject: [PATCH] feat(resolve): automatically add `node_modules` to search path --- src/resolve.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/resolve.ts b/src/resolve.ts index 3de3ac4..41e5da4 100644 --- a/src/resolve.ts +++ b/src/resolve.ts @@ -47,11 +47,20 @@ function _resolve (id: string, opts: ResolveOptions = {}): string { const conditionsSet = opts.conditions ? new Set(opts.conditions) : DEFAULT_CONDITIONS_SET // Search paths - const urls: URL[] = (Array.isArray(opts.url) ? opts.url : [opts.url]) + const _urls: URL[] = (Array.isArray(opts.url) ? opts.url : [opts.url]) .filter(Boolean) .map(u => new URL(normalizeid(u.toString()))) - if (!urls.length) { - urls.push(DEFAULT_URL) + if (!_urls.length) { + _urls.push(DEFAULT_URL) + } + const urls = [..._urls] + // TODO: Consider pnp + for (const url of _urls) { + if (url.protocol === 'file:' && !url.pathname.includes('node_modules')) { + const newURL = new URL(url) + newURL.pathname += '/node_modules' + urls.push(newURL) + } } let resolved