Skip to content

Commit

Permalink
NodeResolver: Reload the closest package.json to an asset if it's a p…
Browse files Browse the repository at this point in the history
…ackage entry (#7909)
  • Loading branch information
Will Binns-Smith committed May 23, 2022
1 parent 2c18016 commit 460e786
Show file tree
Hide file tree
Showing 21 changed files with 385 additions and 1 deletion.
@@ -0,0 +1,3 @@
import {one} from 'foo/bar'

output = one;

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

@@ -0,0 +1,3 @@
import {one} from 'foo/bar'

output = one;

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions packages/core/integration-tests/test/scope-hoisting.js
Expand Up @@ -2348,6 +2348,40 @@ describe('scope hoisting', function () {
assert.equal(await output, 42);
});

describe("considers an asset's closest package.json for sideEffects, not the package through which it found the asset", () => {
it('handles redirects up the tree', async () => {
let b = await bundle(
path.join(
__dirname,
'/integration/scope-hoisting/es6/side-effects-package-redirect-up/index.js',
),
);

let result = await run(b);
assert.strictEqual(result, 1);

let bar = findAsset(b, 'real-bar.js');
assert(bar);
assert.strictEqual(bar.sideEffects, false);
});

it('handles redirects down the tree', async () => {
let b = await bundle(
path.join(
__dirname,
'/integration/scope-hoisting/es6/side-effects-package-redirect-down/index.js',
),
);

let result = await run(b);
assert.strictEqual(result, 1);

let bar = findAsset(b, 'real-bar.js');
assert(bar);
assert.strictEqual(bar.sideEffects, false);
});
});

describe('correctly updates used symbols on changes', () => {
it('dependency symbols change', async function () {
let testDir = path.join(
Expand Down
10 changes: 9 additions & 1 deletion packages/utils/node-resolver-core/src/NodeResolver.js
Expand Up @@ -997,7 +997,15 @@ export default class NodeResolver {
}

if (found) {
return {path: found, pkg};
return {
path: found,
// 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:
pkg == null || pkg?.pkgdir !== path.dirname(found)
? await this.findPackage(found, ctx)
: pkg,
};
}

return null;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 460e786

Please sign in to comment.