From 2e58db8a949ffecfc53115fcd53b514ceb9a1d4c Mon Sep 17 00:00:00 2001 From: Will Binns-Smith Date: Tue, 10 Aug 2021 14:51:50 -0700 Subject: [PATCH] Don't include the default export in static reexports (#6701) --- .../es6/no-reexport-default/a.js | 5 +++++ .../es6/no-reexport-default/async.js | 5 +++++ .../es6/no-reexport-default/b.js | 1 + .../es6/no-reexport-default/c.js | 1 + .../es6/no-reexport-default/index.js | 3 +++ .../es6/no-reexport-esmodule/a.js | 2 ++ .../es6/no-reexport-esmodule/async.js | 1 + .../es6/no-reexport-esmodule/index.js | 3 +++ .../integration-tests/test/scope-hoisting.js | 22 +++++++++++++++++++ .../packagers/js/src/ScopeHoistingPackager.js | 7 ++++++ 10 files changed, 50 insertions(+) create mode 100644 packages/core/integration-tests/test/integration/scope-hoisting/es6/no-reexport-default/a.js create mode 100644 packages/core/integration-tests/test/integration/scope-hoisting/es6/no-reexport-default/async.js create mode 100644 packages/core/integration-tests/test/integration/scope-hoisting/es6/no-reexport-default/b.js create mode 100644 packages/core/integration-tests/test/integration/scope-hoisting/es6/no-reexport-default/c.js create mode 100644 packages/core/integration-tests/test/integration/scope-hoisting/es6/no-reexport-default/index.js create mode 100644 packages/core/integration-tests/test/integration/scope-hoisting/es6/no-reexport-esmodule/a.js create mode 100644 packages/core/integration-tests/test/integration/scope-hoisting/es6/no-reexport-esmodule/async.js create mode 100644 packages/core/integration-tests/test/integration/scope-hoisting/es6/no-reexport-esmodule/index.js diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/es6/no-reexport-default/a.js b/packages/core/integration-tests/test/integration/scope-hoisting/es6/no-reexport-default/a.js new file mode 100644 index 00000000000..f4ef1a62f0c --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/es6/no-reexport-default/a.js @@ -0,0 +1,5 @@ +export {default} from './b.js'; +export * from './b.js'; + +export {default as other} from './c.js'; +export * from './c.js'; diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/es6/no-reexport-default/async.js b/packages/core/integration-tests/test/integration/scope-hoisting/es6/no-reexport-default/async.js new file mode 100644 index 00000000000..effda9f6898 --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/es6/no-reexport-default/async.js @@ -0,0 +1,5 @@ +import _default, {other} from './a.js'; + +sideEffectNoop(_default, other); + +export default _default; diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/es6/no-reexport-default/b.js b/packages/core/integration-tests/test/integration/scope-hoisting/es6/no-reexport-default/b.js new file mode 100644 index 00000000000..7a4e8a723a4 --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/es6/no-reexport-default/b.js @@ -0,0 +1 @@ +export default 42; diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/es6/no-reexport-default/c.js b/packages/core/integration-tests/test/integration/scope-hoisting/es6/no-reexport-default/c.js new file mode 100644 index 00000000000..4a45ec62226 --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/es6/no-reexport-default/c.js @@ -0,0 +1 @@ +export default 99; diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/es6/no-reexport-default/index.js b/packages/core/integration-tests/test/integration/scope-hoisting/es6/no-reexport-default/index.js new file mode 100644 index 00000000000..4d154549b02 --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/es6/no-reexport-default/index.js @@ -0,0 +1,3 @@ +import _default, {other} from './a.js'; + +output = import('./async').then(mod => mod.default); diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/es6/no-reexport-esmodule/a.js b/packages/core/integration-tests/test/integration/scope-hoisting/es6/no-reexport-esmodule/a.js new file mode 100644 index 00000000000..001aee69b47 --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/es6/no-reexport-esmodule/a.js @@ -0,0 +1,2 @@ +module.exports.foo = 42; +module.exports.__esModule = true; diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/es6/no-reexport-esmodule/async.js b/packages/core/integration-tests/test/integration/scope-hoisting/es6/no-reexport-esmodule/async.js new file mode 100644 index 00000000000..69f6e6a7942 --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/es6/no-reexport-esmodule/async.js @@ -0,0 +1 @@ +export * from './a'; diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/es6/no-reexport-esmodule/index.js b/packages/core/integration-tests/test/integration/scope-hoisting/es6/no-reexport-esmodule/index.js new file mode 100644 index 00000000000..7b266d0d780 --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/es6/no-reexport-esmodule/index.js @@ -0,0 +1,3 @@ +import * as a from './a.js'; + +output = import('./async').then(mod => mod.__esModule); diff --git a/packages/core/integration-tests/test/scope-hoisting.js b/packages/core/integration-tests/test/scope-hoisting.js index 0118744826c..39c215b3ae4 100644 --- a/packages/core/integration-tests/test/scope-hoisting.js +++ b/packages/core/integration-tests/test/scope-hoisting.js @@ -3714,6 +3714,28 @@ describe('scope hoisting', function() { let test = await run(b); assert.equal(test({foo: 2}), 2); }); + + it('should not include default when reexporting * without $parcel$exportWildcard', async () => { + let b = await bundle( + path.join( + __dirname, + 'integration/scope-hoisting/es6/no-reexport-default/index.js', + ), + ); + + assert.equal(await run(b), 42); + }); + + it('should not include __esModule when reexporting * without $parcel$exportWildcard', async () => { + let b = await bundle( + path.join( + __dirname, + 'integration/scope-hoisting/es6/no-reexport-esmodule/index.js', + ), + ); + + assert.equal(await run(b), undefined); + }); }); describe('commonjs', function() { diff --git a/packages/packagers/js/src/ScopeHoistingPackager.js b/packages/packagers/js/src/ScopeHoistingPackager.js index af3329ebff0..2514347b5c5 100644 --- a/packages/packagers/js/src/ScopeHoistingPackager.js +++ b/packages/packagers/js/src/ScopeHoistingPackager.js @@ -983,6 +983,13 @@ ${code} this.usedHelpers.add('$parcel$exportWildcard'); } else { for (let symbol of this.bundleGraph.getUsedSymbols(dep)) { + if ( + symbol === 'default' || // `export * as ...` does not include the default export + symbol === '__esModule' + ) { + continue; + } + let resolvedSymbol = this.getSymbolResolution( asset, resolved,