From 1a96d6db889d7e7f74047aeb79761cb4feb3c9fc Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Tue, 2 Aug 2022 19:36:20 +0200 Subject: [PATCH] Use placeholder expression when replacing unused symbols (#8358) --- .../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 +++++++++++++ packages/packagers/js/src/ScopeHoistingPackager.js | 10 ++++++++-- 7 files changed, 33 insertions(+), 2 deletions(-) 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( diff --git a/packages/packagers/js/src/ScopeHoistingPackager.js b/packages/packagers/js/src/ScopeHoistingPackager.js index cd88c660a82..b990058b634 100644 --- a/packages/packagers/js/src/ScopeHoistingPackager.js +++ b/packages/packagers/js/src/ScopeHoistingPackager.js @@ -763,10 +763,16 @@ ${code} exportSymbol, symbol, } = this.bundleGraph.getSymbolResolution(resolved, imported, this.bundle); - if (resolvedAsset.type !== 'js') { - // Graceful fallback for non-js imports + + 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 '{}'; } + let isWrapped = !this.bundle.hasAsset(resolvedAsset) || (this.wrappedAssets.has(resolvedAsset.id) &&