From 3b01d23381b48a2d36d0bd9f7026119f078e5206 Mon Sep 17 00:00:00 2001 From: Will Binns-Smith Date: Tue, 5 Apr 2022 19:12:31 -0700 Subject: [PATCH] Add test for using nearest package.json for sideEffects --- .../index.js | 3 ++ .../node_modules/foo/bar/baz/package.json | 3 ++ .../node_modules/foo/bar/baz/real-bar.js | 1 + .../node_modules/foo/bar/package.json | 3 ++ .../node_modules/foo/package.json | 1 + .../side-effects-package-redirect-up/index.js | 3 ++ .../node_modules/foo/bar/package.json | 3 ++ .../node_modules/foo/package.json | 3 ++ .../node_modules/foo/real-bar.js | 1 + .../integration-tests/test/scope-hoisting.js | 34 +++++++++++++++++++ 10 files changed, 55 insertions(+) create mode 100644 packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-package-redirect-down/index.js create mode 100644 packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-package-redirect-down/node_modules/foo/bar/baz/package.json create mode 100644 packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-package-redirect-down/node_modules/foo/bar/baz/real-bar.js create mode 100644 packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-package-redirect-down/node_modules/foo/bar/package.json create mode 100644 packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-package-redirect-down/node_modules/foo/package.json create mode 100644 packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-package-redirect-up/index.js create mode 100644 packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-package-redirect-up/node_modules/foo/bar/package.json create mode 100644 packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-package-redirect-up/node_modules/foo/package.json create mode 100644 packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-package-redirect-up/node_modules/foo/real-bar.js diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-package-redirect-down/index.js b/packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-package-redirect-down/index.js new file mode 100644 index 00000000000..3afbd9e39b2 --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-package-redirect-down/index.js @@ -0,0 +1,3 @@ +import {one} from 'foo/bar' + +output = one; diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-package-redirect-down/node_modules/foo/bar/baz/package.json b/packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-package-redirect-down/node_modules/foo/bar/baz/package.json new file mode 100644 index 00000000000..a43829151e1 --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-package-redirect-down/node_modules/foo/bar/baz/package.json @@ -0,0 +1,3 @@ +{ + "sideEffects": false +} diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-package-redirect-down/node_modules/foo/bar/baz/real-bar.js b/packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-package-redirect-down/node_modules/foo/bar/baz/real-bar.js new file mode 100644 index 00000000000..d8157863065 --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-package-redirect-down/node_modules/foo/bar/baz/real-bar.js @@ -0,0 +1 @@ +export const one = 1; diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-package-redirect-down/node_modules/foo/bar/package.json b/packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-package-redirect-down/node_modules/foo/bar/package.json new file mode 100644 index 00000000000..518cd1ba105 --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-package-redirect-down/node_modules/foo/bar/package.json @@ -0,0 +1,3 @@ +{ + "module": "baz/real-bar.js" +} diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-package-redirect-down/node_modules/foo/package.json b/packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-package-redirect-down/node_modules/foo/package.json new file mode 100644 index 00000000000..0967ef424bc --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-package-redirect-down/node_modules/foo/package.json @@ -0,0 +1 @@ +{} diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-package-redirect-up/index.js b/packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-package-redirect-up/index.js new file mode 100644 index 00000000000..3afbd9e39b2 --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-package-redirect-up/index.js @@ -0,0 +1,3 @@ +import {one} from 'foo/bar' + +output = one; diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-package-redirect-up/node_modules/foo/bar/package.json b/packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-package-redirect-up/node_modules/foo/bar/package.json new file mode 100644 index 00000000000..476f815322c --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-package-redirect-up/node_modules/foo/bar/package.json @@ -0,0 +1,3 @@ +{ + "module": "../real-bar.js" +} diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-package-redirect-up/node_modules/foo/package.json b/packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-package-redirect-up/node_modules/foo/package.json new file mode 100644 index 00000000000..a43829151e1 --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-package-redirect-up/node_modules/foo/package.json @@ -0,0 +1,3 @@ +{ + "sideEffects": false +} diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-package-redirect-up/node_modules/foo/real-bar.js b/packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-package-redirect-up/node_modules/foo/real-bar.js new file mode 100644 index 00000000000..d8157863065 --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-package-redirect-up/node_modules/foo/real-bar.js @@ -0,0 +1 @@ +export const one = 1; diff --git a/packages/core/integration-tests/test/scope-hoisting.js b/packages/core/integration-tests/test/scope-hoisting.js index b1632bfc83e..256262b57b9 100644 --- a/packages/core/integration-tests/test/scope-hoisting.js +++ b/packages/core/integration-tests/test/scope-hoisting.js @@ -2264,6 +2264,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(