Skip to content

Commit 7730f36

Browse files
committed
also try all import specifiers with exports algo
but the first one that supports exports, but had an error, will result in an error
1 parent bbc4a44 commit 7730f36

File tree

1 file changed

+94
-87
lines changed

1 file changed

+94
-87
lines changed

packages/node-resolve/src/resolveImportSpecifiers.js

Lines changed: 94 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-await-in-loop */
12
import fs from 'fs';
23
import { promisify } from 'util';
34
import { fileURLToPath, pathToFileURL } from 'url';
@@ -100,7 +101,7 @@ async function resolveIdClassic({
100101

101102
async function resolveWithExportMap({
102103
importer,
103-
importSpecifier,
104+
importSpecifierList,
104105
exportConditions,
105106
packageInfoCache,
106107
extensions,
@@ -112,103 +113,110 @@ async function resolveWithExportMap({
112113
rootDir,
113114
ignoreSideEffectsForRoot
114115
}) {
115-
if (importSpecifier.startsWith('#')) {
116-
// this is a package internal import, resolve using package imports field
117-
const resolveResult = await resolvePackageImports({
118-
importSpecifier,
119-
importer,
120-
moduleDirs: moduleDirectories,
121-
conditions: exportConditions,
122-
resolveId(id /*, parent*/) {
123-
return resolveIdClassic({
124-
importSpecifier: id,
125-
packageInfoCache,
116+
for (let i = 0; i < importSpecifierList.length; i++) {
117+
const importSpecifier = importSpecifierList[i];
118+
if (importSpecifier.startsWith('#')) {
119+
// this is a package internal import, resolve using package imports field
120+
const resolveResult = await resolvePackageImports({
121+
importSpecifier,
122+
importer,
123+
moduleDirs: moduleDirectories,
124+
conditions: exportConditions,
125+
resolveId(id /*, parent*/) {
126+
return resolveIdClassic({
127+
importSpecifier: id,
128+
packageInfoCache,
129+
extensions,
130+
mainFields,
131+
preserveSymlinks,
132+
useBrowserOverrides,
133+
baseDir,
134+
moduleDirectories
135+
});
136+
}
137+
});
138+
139+
const location = fileURLToPath(resolveResult);
140+
return {
141+
location: preserveSymlinks ? location : await resolveSymlink(location),
142+
hasModuleSideEffects: () => null,
143+
hasPackageEntry: true,
144+
packageBrowserField: false,
145+
// eslint-disable-next-line no-undefined
146+
packageInfo: undefined
147+
};
148+
}
149+
150+
const pkgName = getPackageName(importSpecifier);
151+
if (pkgName) {
152+
// it's a bare import, find the package.json and resolve using package exports if available
153+
let hasModuleSideEffects = () => null;
154+
let hasPackageEntry = true;
155+
let packageBrowserField = false;
156+
let packageInfo;
157+
158+
const filter = (pkg, pkgPath) => {
159+
const info = getPackageInfo({
160+
cache: packageInfoCache,
126161
extensions,
162+
pkg,
163+
pkgPath,
127164
mainFields,
128165
preserveSymlinks,
129166
useBrowserOverrides,
130-
baseDir,
131-
moduleDirectories
167+
rootDir,
168+
ignoreSideEffectsForRoot
132169
});
133-
}
134-
});
135170

136-
const location = fileURLToPath(resolveResult);
137-
return {
138-
location: preserveSymlinks ? location : await resolveSymlink(location),
139-
hasModuleSideEffects: () => null,
140-
hasPackageEntry: true,
141-
packageBrowserField: false,
142-
// eslint-disable-next-line no-undefined
143-
packageInfo: undefined
144-
};
145-
}
171+
({ packageInfo, hasModuleSideEffects, hasPackageEntry, packageBrowserField } = info);
146172

147-
const pkgName = getPackageName(importSpecifier);
148-
if (pkgName) {
149-
// it's a bare import, find the package.json and resolve using package exports if available
150-
let hasModuleSideEffects = () => null;
151-
let hasPackageEntry = true;
152-
let packageBrowserField = false;
153-
let packageInfo;
173+
return info.cachedPkg;
174+
};
154175

155-
const filter = (pkg, pkgPath) => {
156-
const info = getPackageInfo({
157-
cache: packageInfoCache,
176+
const resolveOptions = {
177+
basedir: baseDir,
178+
readFile: readCachedFile,
179+
isFile: isFileCached,
180+
isDirectory: isDirCached,
158181
extensions,
159-
pkg,
160-
pkgPath,
161-
mainFields,
182+
includeCoreModules: false,
183+
moduleDirectory: moduleDirectories,
162184
preserveSymlinks,
163-
useBrowserOverrides,
164-
rootDir,
165-
ignoreSideEffectsForRoot
166-
});
167-
168-
({ packageInfo, hasModuleSideEffects, hasPackageEntry, packageBrowserField } = info);
169-
170-
return info.cachedPkg;
171-
};
172-
173-
const resolveOptions = {
174-
basedir: baseDir,
175-
readFile: readCachedFile,
176-
isFile: isFileCached,
177-
isDirectory: isDirCached,
178-
extensions,
179-
includeCoreModules: false,
180-
moduleDirectory: moduleDirectories,
181-
preserveSymlinks,
182-
packageFilter: filter
183-
};
185+
packageFilter: filter
186+
};
184187

185-
const result = await getPackageJson(importer, pkgName, resolveOptions, moduleDirectories);
188+
const result = await getPackageJson(importer, pkgName, resolveOptions, moduleDirectories);
186189

187-
if (result && result.pkgJson.exports) {
188-
const { pkgJson, pkgJsonPath } = result;
189-
const subpath =
190-
pkgName === importSpecifier ? '.' : `.${importSpecifier.substring(pkgName.length)}`;
191-
const pkgDr = pkgJsonPath.replace('package.json', '');
192-
const pkgURL = pathToFileURL(pkgDr);
190+
if (result && result.pkgJson.exports) {
191+
const { pkgJson, pkgJsonPath } = result;
192+
const subpath =
193+
pkgName === importSpecifier ? '.' : `.${importSpecifier.substring(pkgName.length)}`;
194+
const pkgDr = pkgJsonPath.replace('package.json', '');
195+
const pkgURL = pathToFileURL(pkgDr);
193196

194-
const context = {
195-
importer,
196-
importSpecifier,
197-
moduleDirs: moduleDirectories,
198-
pkgURL,
199-
pkgJsonPath,
200-
conditions: exportConditions
201-
};
202-
const resolvedPackageExport = await resolvePackageExports(context, subpath, pkgJson.exports);
203-
const location = fileURLToPath(resolvedPackageExport);
204-
if (location) {
205-
return {
206-
location: preserveSymlinks ? location : await resolveSymlink(location),
207-
hasModuleSideEffects,
208-
hasPackageEntry,
209-
packageBrowserField,
210-
packageInfo
197+
const context = {
198+
importer,
199+
importSpecifier,
200+
moduleDirs: moduleDirectories,
201+
pkgURL,
202+
pkgJsonPath,
203+
conditions: exportConditions
211204
};
205+
const resolvedPackageExport = await resolvePackageExports(
206+
context,
207+
subpath,
208+
pkgJson.exports
209+
);
210+
const location = fileURLToPath(resolvedPackageExport);
211+
if (location) {
212+
return {
213+
location: preserveSymlinks ? location : await resolveSymlink(location),
214+
hasModuleSideEffects,
215+
hasPackageEntry,
216+
packageBrowserField,
217+
packageInfo
218+
};
219+
}
212220
}
213221
}
214222
}
@@ -232,7 +240,6 @@ async function resolveWithClassic({
232240
ignoreSideEffectsForRoot
233241
}) {
234242
for (let i = 0; i < importSpecifierList.length; i++) {
235-
// eslint-disable-next-line no-await-in-loop
236243
const result = await resolveIdClassic({
237244
importer,
238245
importSpecifier: importSpecifierList[i],
@@ -258,7 +265,7 @@ async function resolveWithClassic({
258265
}
259266

260267
// Resolves to the module if found or `null`.
261-
// The first import specificer will first be attempted with the exports algorithm.
268+
// First all import specifiers will be attempted with the exports algorithm.
262269
// If this is unsuccesful because export maps are not being used, then all of `importSpecifierList`
263270
// will be tried with the classic resolution algorithm
264271
export default async function resolveImportSpecifiers({
@@ -279,7 +286,7 @@ export default async function resolveImportSpecifiers({
279286
try {
280287
const exportMapRes = await resolveWithExportMap({
281288
importer,
282-
importSpecifier: importSpecifierList[0],
289+
importSpecifierList,
283290
exportConditions,
284291
packageInfoCache,
285292
extensions,

0 commit comments

Comments
 (0)