diff --git a/packages/utils/node-resolver-core/src/NodeResolver.js b/packages/utils/node-resolver-core/src/NodeResolver.js index 7b7cd1b1f4c..6e38d809441 100644 --- a/packages/utils/node-resolver-core/src/NodeResolver.js +++ b/packages/utils/node-resolver-core/src/NodeResolver.js @@ -779,7 +779,6 @@ export default class NodeResolver { let res = (await this.loadAsFile({ file: entry.filename, - asPackageEntry: true, extensions, env, pkg, @@ -973,14 +972,12 @@ export default class NodeResolver { extensions, env, pkg, - asPackageEntry, ctx, }: {| file: string, extensions: Array, env: Environment, pkg: InternalPackageJSON | null, - asPackageEntry?: boolean, ctx: ResolverContext, |}): Promise { // Try all supported extensions @@ -1002,9 +999,12 @@ export default class NodeResolver { if (found) { return { path: found, - // If this is an entrypoint in package.json, it's possible pkg is not the + // If this package.json isn't a sibling of found, it's possible pkg is not the // closest package.json to the resolved file. Reload it instead. - pkg: asPackageEntry ? await this.findPackage(found, ctx) : pkg, + pkg: + pkg == null || pkg?.pkgdir !== path.basename(found) + ? await this.findPackage(found, ctx) + : pkg, }; } diff --git a/packages/utils/node-resolver-core/test/resolver.js b/packages/utils/node-resolver-core/test/resolver.js index e38c4d2287b..fbe6074c704 100644 --- a/packages/utils/node-resolver-core/test/resolver.js +++ b/packages/utils/node-resolver-core/test/resolver.js @@ -379,6 +379,10 @@ describe('resolver', function () { fileName: 'package.json', aboveFilePath: path.join(__dirname, '..', 'src', '_empty.js'), }, + { + fileName: 'package.json', + aboveFilePath: path.join(__dirname, '..', 'src', '_empty.js'), + }, ], invalidateOnFileChange: [ path.join(rootDir, 'package.json'), @@ -441,6 +445,10 @@ describe('resolver', function () { fileName: 'node_modules/foo', aboveFilePath: path.join(rootDir, 'foo.js'), }, + { + fileName: 'package.json', + aboveFilePath: path.join(rootDir, 'node_modules/foo/index.js'), + }, ], invalidateOnFileChange: [ path.join(rootDir, 'package.json'), @@ -681,6 +689,15 @@ describe('resolver', function () { 'main.js/package.json', ), }, + { + fileName: 'package.json', + aboveFilePath: path.join( + rootDir, + 'node_modules', + 'package-fallback', + 'index.js', + ), + }, ], invalidateOnFileChange: [ path.join(rootDir, 'package.json'), @@ -749,6 +766,16 @@ describe('resolver', function () { 'package.json', ), }, + { + fileName: 'package.json', + aboveFilePath: path.join( + rootDir, + 'node_modules', + 'package-main-directory', + 'nested', + 'index.js', + ), + }, ], invalidateOnFileChange: [ path.join(rootDir, 'package.json'), @@ -786,6 +813,16 @@ describe('resolver', function () { fileName: 'node_modules/foo', aboveFilePath: path.join(rootDir, 'foo.js'), }, + { + fileName: 'package.json', + aboveFilePath: path.join( + rootDir, + 'node_modules', + 'foo', + 'nested', + 'baz.js', + ), + }, ], invalidateOnFileChange: [ path.join(rootDir, 'package.json'), @@ -818,6 +855,16 @@ describe('resolver', function () { fileName: 'node_modules/@scope/pkg', aboveFilePath: path.join(rootDir, 'foo.js'), }, + { + fileName: 'package.json', + aboveFilePath: path.join( + rootDir, + 'node_modules', + '@scope', + 'pkg', + 'index.js', + ), + }, ], invalidateOnFileChange: [ path.join(rootDir, 'package.json'), @@ -850,6 +897,17 @@ describe('resolver', function () { fileName: 'node_modules/@scope/pkg', aboveFilePath: path.join(rootDir, 'foo.js'), }, + { + fileName: 'package.json', + aboveFilePath: path.join( + rootDir, + 'node_modules', + '@scope', + 'pkg', + 'foo', + 'bar.js', + ), + }, ], invalidateOnFileChange: [ path.join(rootDir, 'package.json'), @@ -886,6 +944,16 @@ describe('resolver', function () { fileName: 'node_modules/side-effects-false', aboveFilePath: path.join(rootDir, 'foo.js'), }, + { + fileName: 'package.json', + aboveFilePath: path.join( + rootDir, + 'node_modules', + 'side-effects-false', + 'src', + 'index.js', + ), + }, ], invalidateOnFileChange: [ path.join(rootDir, 'package.json'), @@ -926,6 +994,16 @@ describe('resolver', function () { fileName: 'node_modules/side-effects-false', aboveFilePath: path.join(rootDir, 'foo.js'), }, + { + fileName: 'package.json', + aboveFilePath: path.join( + rootDir, + 'node_modules', + 'side-effects-false', + 'src', + 'index.js', + ), + }, ], invalidateOnFileChange: [ path.join(rootDir, 'package.json'), @@ -985,6 +1063,16 @@ describe('resolver', function () { ), fileName: 'package.json', }, + { + aboveFilePath: path.join( + rootDir, + 'node_modules', + 'side-effects-false', + 'src', + 'index.js', + ), + fileName: 'package.json', + }, ], invalidateOnFileChange: [ path.join(rootDir, 'package.json'), @@ -1044,6 +1132,16 @@ describe('resolver', function () { ), fileName: 'package.json', }, + { + aboveFilePath: path.join( + rootDir, + 'node_modules', + 'side-effects-false', + 'src', + 'index.js', + ), + fileName: 'package.json', + }, ], invalidateOnFileChange: [ path.join(rootDir, 'package.json'), @@ -1461,6 +1559,15 @@ describe('resolver', function () { fileName: 'node_modules/package-browser-alias', aboveFilePath: path.join(rootDir, 'foo.js'), }, + { + fileName: 'package.json', + aboveFilePath: path.join( + rootDir, + 'node_modules', + 'package-browser-alias', + 'bar.js', + ), + }, ], invalidateOnFileChange: [ path.join(rootDir, 'package.json'), @@ -1518,6 +1625,15 @@ describe('resolver', function () { 'bar', ), }, + { + fileName: 'package.json', + aboveFilePath: path.join( + rootDir, + 'node_modules', + 'package-browser-alias', + 'bar.js', + ), + }, ], invalidateOnFileChange: [ path.join(rootDir, 'package.json'), @@ -1560,6 +1676,14 @@ describe('resolver', function () { fileName: 'node_modules/package-browser-alias', aboveFilePath: path.join(rootDir, 'foo.js'), }, + { + fileName: 'package.json', + aboveFilePath: path.join( + rootDir, + 'node_modules/package-browser-alias', + 'foo.js', + ), + }, ], invalidateOnFileChange: [ path.join(rootDir, 'package.json'), @@ -1617,6 +1741,17 @@ describe('resolver', function () { 'nested', ), }, + { + fileName: 'package.json', + aboveFilePath: path.join( + rootDir, + 'node_modules', + 'package-browser-alias', + 'subfolder1', + 'subfolder2', + 'subfile.js', + ), + }, ], invalidateOnFileChange: [ path.join(rootDir, 'package.json'), @@ -1654,6 +1789,15 @@ describe('resolver', function () { fileName: 'node_modules/package-alias', aboveFilePath: path.join(rootDir, 'foo.js'), }, + { + fileName: 'package.json', + aboveFilePath: path.join( + rootDir, + 'node_modules', + 'package-alias', + 'bar.js', + ), + }, ], invalidateOnFileChange: [ path.join(rootDir, 'package.json'), @@ -1701,6 +1845,15 @@ describe('resolver', function () { 'bar', ), }, + { + fileName: 'package.json', + aboveFilePath: path.join( + rootDir, + 'node_modules', + 'package-alias', + 'bar.js', + ), + }, ], invalidateOnFileChange: [ path.join(rootDir, 'package.json'), @@ -1755,6 +1908,16 @@ describe('resolver', function () { 'test', ), }, + { + fileName: 'package.json', + aboveFilePath: path.join( + rootDir, + 'node_modules', + 'package-alias-glob', + 'src', + 'test.js', + ), + }, ], invalidateOnFileChange: [ path.join(rootDir, 'package.json'), @@ -1792,6 +1955,15 @@ describe('resolver', function () { fileName: 'node_modules/foo', aboveFilePath: path.join(rootDir, 'foo.js'), }, + { + fileName: 'package.json', + aboveFilePath: path.join( + rootDir, + 'node_modules', + 'foo', + 'index.js', + ), + }, ], invalidateOnFileChange: [ path.join(rootDir, 'package.json'), @@ -1834,6 +2006,10 @@ describe('resolver', function () { 'foo.js', ), }, + { + fileName: 'package.json', + aboveFilePath: path.join(rootDir, 'node_modules', 'foo/index.js'), + }, ], invalidateOnFileChange: [ path.join(rootDir, 'package.json'), @@ -1877,6 +2053,10 @@ describe('resolver', function () { 'foo.js', ), }, + { + fileName: 'package.json', + aboveFilePath: path.join(rootDir, 'node_modules', 'foo', 'bar.js'), + }, ], invalidateOnFileChange: [ path.join(rootDir, 'package.json'), @@ -1910,6 +2090,10 @@ describe('resolver', function () { fileName: 'package.json', aboveFilePath: path.join(rootDir, 'bar.js'), }, + { + fileName: 'package.json', + aboveFilePath: path.join(rootDir, 'bar.js'), + }, ], invalidateOnFileChange: [path.join(rootDir, 'package.json')], }); @@ -1944,6 +2128,10 @@ describe('resolver', function () { fileName: 'package.json', aboveFilePath: path.join(rootDir, 'bar.js'), }, + { + fileName: 'package.json', + aboveFilePath: path.join(rootDir, 'bar.js'), + }, ], invalidateOnFileChange: [ path.join(rootDir, 'package.json'), @@ -1976,6 +2164,10 @@ describe('resolver', function () { fileName: 'package.json', aboveFilePath: path.join(rootDir, 'nested', 'test.js'), }, + { + fileName: 'package.json', + aboveFilePath: path.join(rootDir, 'nested', 'test.js'), + }, ], invalidateOnFileChange: [path.join(rootDir, 'package.json')], }); @@ -2014,6 +2206,10 @@ describe('resolver', function () { { filePath: path.join(rootDir, 'nested', 'package.json'), }, + { + fileName: 'package.json', + aboveFilePath: path.join(rootDir, 'nested/index.js'), + }, ], invalidateOnFileChange: [path.join(rootDir, 'package.json')], }); @@ -2043,6 +2239,10 @@ describe('resolver', function () { fileName: 'package.json', aboveFilePath: path.join(rootDir, 'nested', 'test.js'), }, + { + fileName: 'package.json', + aboveFilePath: path.join(rootDir, 'nested', 'test.js'), + }, ], invalidateOnFileChange: [path.join(rootDir, 'package.json')], }); @@ -2081,6 +2281,10 @@ describe('resolver', function () { { filePath: path.join(rootDir, 'nested', 'package.json'), }, + { + fileName: 'package.json', + aboveFilePath: path.join(rootDir, 'nested/index.js'), + }, ], invalidateOnFileChange: [path.join(rootDir, 'package.json')], }); @@ -2110,6 +2314,10 @@ describe('resolver', function () { fileName: 'package.json', aboveFilePath: path.join(rootDir, 'bar.js'), }, + { + fileName: 'package.json', + aboveFilePath: path.join(rootDir, 'bar.js'), + }, ], invalidateOnFileChange: [path.join(rootDir, 'package.json')], }); @@ -2139,6 +2347,10 @@ describe('resolver', function () { fileName: 'package.json', aboveFilePath: path.join(rootDir, 'nested', 'test'), }, + { + fileName: 'package.json', + aboveFilePath: path.join(rootDir, 'nested', 'test.js'), + }, ], invalidateOnFileChange: [path.join(rootDir, 'package.json')], }); @@ -2168,6 +2380,10 @@ describe('resolver', function () { fileName: 'package.json', aboveFilePath: path.join(rootDir, 'nested', 'test.js'), }, + { + fileName: 'package.json', + aboveFilePath: path.join(rootDir, 'nested', 'test.js'), + }, ], invalidateOnFileChange: [path.join(rootDir, 'package.json')], }); @@ -2202,6 +2418,10 @@ describe('resolver', function () { fileName: 'package.json', aboveFilePath: path.join(rootDir, 'nested', 'test.js'), }, + { + fileName: 'package.json', + aboveFilePath: path.join(rootDir, 'nested', 'test.js'), + }, ], invalidateOnFileChange: [ path.join(rootDir, 'package.json'),