Skip to content

Commit

Permalink
Fix unused internalized async imports (#5652)
Browse files Browse the repository at this point in the history
* Add test

* Fix

Co-authored-by: Will Binns-Smith <[email protected]>
  • Loading branch information
mischnic and Will Binns-Smith authored Jan 14, 2021
1 parent 7cb56b3 commit 49ac217
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import c from "./c.js";
import b from "./b.js";

output = b + c;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import("./c.js");

export default "b";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default "c";
12 changes: 12 additions & 0 deletions packages/core/integration-tests/test/scope-hoisting.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,18 @@ describe('scope hoisting', function() {
assert.deepEqual(output, [123, 123]);
});

it('supports async import of internalized asset with unused return value', async function() {
let b = await bundle(
path.join(
__dirname,
'/integration/scope-hoisting/es6/async-internalize-unused/a.js',
),
);

let output = await run(b);
assert.strictEqual(output, 'bc');
});

it('supports importing a namespace of exported values', async function() {
let b = await bundle(
path.join(
Expand Down
16 changes: 10 additions & 6 deletions packages/shared/scope-hoisting/src/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import type {
import type {ExternalModule, ExternalBundle} from './types';
import type {
Expression,
ExpressionStatement,
File,
FunctionDeclaration,
Identifier,
Statement,
StringLiteral,
VariableDeclaration,
} from '@babel/types';
Expand Down Expand Up @@ -48,9 +48,10 @@ import {
} from './utils';
import OutputFormats from './formats/index.js';

const THROW_TEMPLATE = template.statement<{|MODULE: StringLiteral|}, Statement>(
'$parcel$missingModule(MODULE);',
);
const THROW_TEMPLATE = template.statement<
{|MODULE: StringLiteral|},
ExpressionStatement,
>('$parcel$missingModule(MODULE);');
const REQUIRE_RESOLVE_CALL_TEMPLATE = template.expression<
{|ID: StringLiteral|},
Expression,
Expand Down Expand Up @@ -678,13 +679,16 @@ export function link({
}

// async dependency that was internalized
if (asyncResolution?.type === 'asset') {
if (
newNode &&
asyncResolution?.type === 'asset' &&
!isExpressionStatement(newNode)
) {
newNode = t.callExpression(
t.memberExpression(
t.identifier('Promise'),
t.identifier('resolve'),
),
// $FlowFixMe[incompatible-call]
[newNode],
);
}
Expand Down

0 comments on commit 49ac217

Please sign in to comment.