Skip to content

Commit

Permalink
Use placeholder expression when replacing unused symbols (#8358)
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic committed Aug 2, 2022
1 parent a22164c commit 1a96d6d
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 2 deletions.
@@ -0,0 +1 @@
output = import("./library").then(({ a }) => a);
@@ -0,0 +1 @@
export var foo = "foo";
@@ -0,0 +1,3 @@
class b {}

export { b as default };
@@ -0,0 +1,4 @@
import * as a from "./a";
import b from "./b";
export { a, b };
export var b2 = b;
@@ -0,0 +1,3 @@
{
"sideEffects": false
}
13 changes: 13 additions & 0 deletions packages/core/integration-tests/test/javascript.js
Expand Up @@ -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(
Expand Down
10 changes: 8 additions & 2 deletions packages/packagers/js/src/ScopeHoistingPackager.js
Expand Up @@ -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) &&
Expand Down

0 comments on commit 1a96d6d

Please sign in to comment.