From 79c7e32bf37c06f692ccce37bf44d7b46542b992 Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Mon, 1 Aug 2022 14:38:02 +0200 Subject: [PATCH 1/3] Add test --- .../index.js | 1 + .../library/a.js | 1 + .../library/b.js | 3 +++ .../library/index.js | 4 ++++ .../library/package.json | 3 +++ packages/core/integration-tests/test/javascript.js | 13 +++++++++++++ 6 files changed, 25 insertions(+) create mode 100644 packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-re-exports-rename-var-unused/index.js create mode 100644 packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-re-exports-rename-var-unused/library/a.js create mode 100644 packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-re-exports-rename-var-unused/library/b.js create mode 100644 packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-re-exports-rename-var-unused/library/index.js create mode 100644 packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-re-exports-rename-var-unused/library/package.json diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-re-exports-rename-var-unused/index.js b/packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-re-exports-rename-var-unused/index.js new file mode 100644 index 00000000000..cf4fbe2af7c --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-re-exports-rename-var-unused/index.js @@ -0,0 +1 @@ +output = import("./library").then(({ a }) => a); diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-re-exports-rename-var-unused/library/a.js b/packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-re-exports-rename-var-unused/library/a.js new file mode 100644 index 00000000000..6df90691862 --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-re-exports-rename-var-unused/library/a.js @@ -0,0 +1 @@ +export var foo = "foo"; diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-re-exports-rename-var-unused/library/b.js b/packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-re-exports-rename-var-unused/library/b.js new file mode 100644 index 00000000000..24cb012005b --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-re-exports-rename-var-unused/library/b.js @@ -0,0 +1,3 @@ +class b {} + +export { b as default }; diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-re-exports-rename-var-unused/library/index.js b/packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-re-exports-rename-var-unused/library/index.js new file mode 100644 index 00000000000..1ef49c49122 --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-re-exports-rename-var-unused/library/index.js @@ -0,0 +1,4 @@ +import * as a from "./a"; +import b from "./b"; +export { a, b }; +export var b2 = b; diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-re-exports-rename-var-unused/library/package.json b/packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-re-exports-rename-var-unused/library/package.json new file mode 100644 index 00000000000..a43829151e1 --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/es6/side-effects-re-exports-rename-var-unused/library/package.json @@ -0,0 +1,3 @@ +{ + "sideEffects": false +} diff --git a/packages/core/integration-tests/test/javascript.js b/packages/core/integration-tests/test/javascript.js index 4de6429736a..8cedde288af 100644 --- a/packages/core/integration-tests/test/javascript.js +++ b/packages/core/integration-tests/test/javascript.js @@ -7025,6 +7025,19 @@ describe('javascript', function () { assert.deepEqual(res.output, 'bar'); }); + it('supports reexports via variable declaration (unused)', async function () { + let b = await bundle( + path.join( + __dirname, + '/integration/scope-hoisting/es6/side-effects-re-exports-rename-var-unused/index.js', + ), + options, + ); + + let res = await run(b, {}, {require: false}); + assert.deepEqual((await res.output).foo, 'foo'); + }); + it('supports named and renamed reexports of the same asset (named used)', async function () { let b = await bundle( path.join( From 72007639b76f87e9cff27e1325691dc0b4490871 Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Mon, 1 Aug 2022 14:42:43 +0200 Subject: [PATCH 2/3] Fix --- packages/packagers/js/src/ScopeHoistingPackager.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/packagers/js/src/ScopeHoistingPackager.js b/packages/packagers/js/src/ScopeHoistingPackager.js index cd88c660a82..ae25d4bf170 100644 --- a/packages/packagers/js/src/ScopeHoistingPackager.js +++ b/packages/packagers/js/src/ScopeHoistingPackager.js @@ -763,10 +763,13 @@ ${code} exportSymbol, symbol, } = this.bundleGraph.getSymbolResolution(resolved, imported, this.bundle); - if (resolvedAsset.type !== 'js') { - // Graceful fallback for non-js imports + + if (resolvedAsset.type !== 'js' || this.shouldSkipAsset(resolvedAsset)) { + // Graceful fallback for non-js imports or when trying to resolve a symbol + // that is actually unused but we still need a placeholder value. return '{}'; } + let isWrapped = !this.bundle.hasAsset(resolvedAsset) || (this.wrappedAssets.has(resolvedAsset.id) && From 760bda567e52a8766ea2fbf127623fe0640a60f4 Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Tue, 2 Aug 2022 09:10:00 +0200 Subject: [PATCH 3/3] Different fix --- packages/packagers/js/src/ScopeHoistingPackager.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/packagers/js/src/ScopeHoistingPackager.js b/packages/packagers/js/src/ScopeHoistingPackager.js index ae25d4bf170..b990058b634 100644 --- a/packages/packagers/js/src/ScopeHoistingPackager.js +++ b/packages/packagers/js/src/ScopeHoistingPackager.js @@ -764,7 +764,10 @@ ${code} symbol, } = this.bundleGraph.getSymbolResolution(resolved, imported, this.bundle); - if (resolvedAsset.type !== 'js' || this.shouldSkipAsset(resolvedAsset)) { + if ( + resolvedAsset.type !== 'js' || + (dep && this.bundleGraph.isDependencySkipped(dep)) + ) { // Graceful fallback for non-js imports or when trying to resolve a symbol // that is actually unused but we still need a placeholder value. return '{}';