Skip to content

Commit

Permalink
Use dirname comparison instead
Browse files Browse the repository at this point in the history
  • Loading branch information
Will Binns-Smith committed Apr 8, 2022
1 parent d8986f5 commit 9b09b63
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 5 deletions.
10 changes: 5 additions & 5 deletions packages/utils/node-resolver-core/src/NodeResolver.js
Expand Up @@ -779,7 +779,6 @@ export default class NodeResolver {
let res =
(await this.loadAsFile({
file: entry.filename,
asPackageEntry: true,
extensions,
env,
pkg,
Expand Down Expand Up @@ -973,14 +972,12 @@ export default class NodeResolver {
extensions,
env,
pkg,
asPackageEntry,
ctx,
}: {|
file: string,
extensions: Array<string>,
env: Environment,
pkg: InternalPackageJSON | null,
asPackageEntry?: boolean,
ctx: ResolverContext,
|}): Promise<?ResolvedFile> {
// Try all supported extensions
Expand All @@ -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.dirname(found)
? await this.findPackage(found, ctx)
: pkg,
};
}

Expand Down
124 changes: 124 additions & 0 deletions packages/utils/node-resolver-core/test/resolver.js
Expand Up @@ -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'),
Expand Down Expand Up @@ -721,6 +725,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'),
Expand Down Expand Up @@ -758,6 +772,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'),
Expand Down Expand Up @@ -822,6 +846,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'),
Expand Down Expand Up @@ -858,6 +893,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'),
Expand Down Expand Up @@ -898,6 +943,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'),
Expand Down Expand Up @@ -957,6 +1012,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'),
Expand Down Expand Up @@ -1016,6 +1081,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'),
Expand Down Expand Up @@ -1582,6 +1657,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'),
Expand Down Expand Up @@ -1720,6 +1806,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'),
Expand Down Expand Up @@ -1941,6 +2037,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')],
});
Expand Down Expand Up @@ -1979,6 +2079,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')],
});
Expand Down Expand Up @@ -2008,6 +2112,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')],
});
Expand Down Expand Up @@ -2046,6 +2154,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')],
});
Expand Down Expand Up @@ -2104,6 +2216,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')],
});
Expand Down Expand Up @@ -2133,6 +2249,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')],
});
Expand Down Expand Up @@ -2167,6 +2287,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'),
Expand Down

0 comments on commit 9b09b63

Please sign in to comment.