Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3569720

Browse files
fix(node-resolve): resolve bare targets of package "imports" using export maps; avoid fileURLToPath(null) (#1908)
fix(node-resolve): resolve bare targets of package "imports" using export maps; avoid fileURLToPath(null)\n\n- When an imports mapping points to a bare specifier (e.g. "#foo/*": "@scope/pkg/*"), resolve the target with the full export-map-aware algorithm instead of classic only.\n- Guard against null return to prevent TypeError from fileURLToPath(null).\n- Add test: imports pattern -> bare package that uses exports.\n\nFixes #1907 Co-authored-by: CharlieHelps <[email protected]>
1 parent c3dcdc0 commit 3569720

File tree

5 files changed

+55
-3
lines changed

5 files changed

+55
-3
lines changed

packages/node-resolve/src/resolveImportSpecifiers.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,21 +125,41 @@ async function resolveWithExportMap({
125125
importer,
126126
moduleDirs: moduleDirectories,
127127
conditions: exportConditions,
128+
// Resolve targets of "imports" mappings using the same algorithm
129+
// we use for normal specifiers: try export maps first and then
130+
// fall back to classic resolution. This is important for cases
131+
// like "#foo/*": "@scope/pkg/*" where the target package relies
132+
// on "exports" to expose subpaths. Using the classic resolver
133+
// alone would fail to find those subpaths.
128134
resolveId(id /* , parent*/) {
129-
return resolveIdClassic({
130-
importSpecifier: id,
135+
return resolveImportSpecifiers({
136+
importer,
137+
importSpecifierList: [id],
138+
exportConditions,
139+
// pass-through of the rest of the context
131140
packageInfoCache,
132141
extensions,
133142
mainFields,
134143
preserveSymlinks,
135144
useBrowserOverrides,
136145
baseDir,
137146
moduleDirectories,
138-
modulePaths
147+
modulePaths,
148+
rootDir,
149+
ignoreSideEffectsForRoot,
150+
allowExportsFolderMapping
139151
});
140152
}
141153
});
142154

155+
if (resolveResult == null) {
156+
// When the target of an "imports" mapping cannot be resolved,
157+
// surface a proper resolve error instead of throwing from
158+
// fileURLToPath(null).
159+
throw new ResolveError(
160+
`Could not resolve import "${importSpecifier}" in ${importer} using imports.`
161+
);
162+
}
143163
const location = fileURLToPath(resolveResult);
144164
return {
145165
location: preserveSymlinks ? location : await resolveSymlink(location),
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { a, b, c } from 'imports-bare-pattern-exports';
2+
3+
export default { a, b, c };

packages/node-resolve/test/fixtures/node_modules/imports-bare-pattern-exports/index.js

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/node-resolve/test/fixtures/node_modules/imports-bare-pattern-exports/package.json

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/node-resolve/test/package-entry-points.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,23 @@ test('can resolve a package import with a pattern', async (t) => {
349349
});
350350
});
351351

352+
test('can resolve a package import pattern to a bare package that uses exports', async (t) => {
353+
const bundle = await rollup({
354+
input: 'imports-bare-pattern-exports.js',
355+
onwarn: () => {
356+
t.fail('No warnings were expected');
357+
},
358+
plugins: [nodeResolve()]
359+
});
360+
const { module } = await testBundle(t, bundle);
361+
362+
t.deepEqual(module.exports, {
363+
a: 'exported-foo a',
364+
b: 'exported-foo b',
365+
c: 'exported-foo c'
366+
});
367+
});
368+
352369
test('can override a star pattern using null', async (t) => {
353370
const errors = [];
354371
const bundle = await rollup({

0 commit comments

Comments
 (0)