Skip to content

Commit

Permalink
Fix named imports of CommonJS assets with namespace exports via names…
Browse files Browse the repository at this point in the history
…pace reexports (#4759)
  • Loading branch information
mischnic committed Jun 23, 2020
1 parent 6e1abea commit d19bee1
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 8 deletions.
14 changes: 6 additions & 8 deletions packages/core/core/src/BundleGraph.js
Expand Up @@ -803,13 +803,10 @@ export default class BundleGraph {
if (!resolved) continue;
let result = this.resolveSymbol(resolved, symbol, boundary);

// Either result.symbol is a string (found) or null with a wildcard (found)
if (
result.symbol != undefined ||
(result.symbol === null && result.exportSymbol === '*')
) {
// We found the symbol
if (result.symbol != undefined) {
if (assetOutside) {
// We found the symbol, but `asset` is outside, return `asset` and the original symbol
// ..., but `asset` is outside, return `asset` and the original symbol
bailout = true;
break;
}
Expand All @@ -821,7 +818,7 @@ export default class BundleGraph {
loc: resolved.symbols?.get(symbol)?.loc,
};
}
if (!result.asset.symbols) {
if (!result.asset.symbols || result.symbol === null) {
// We didn't find it in this dependency, but it might still be there: bailout.
// Continue searching though, with the assumption that there are no conficting reexports
// and there might be a another (re)export (where we might statically find the symbol).
Expand All @@ -845,7 +842,8 @@ export default class BundleGraph {
return {
asset,
exportSymbol: symbol,
symbol: identifier ?? (bailout ? null : undefined),
symbol:
identifier ?? (bailout || asset.symbols?.has('*') ? null : undefined),
loc: asset.symbols?.get(symbol)?.loc,
};
}
Expand Down
@@ -0,0 +1,3 @@
import { createElement } from "./b.js";

output = createElement();
@@ -0,0 +1 @@
export * from './c.js';
@@ -0,0 +1,3 @@
import { createElement } from './d.js';

export { createElement };
@@ -0,0 +1,5 @@
module.exports = {
createElement: function () {
return 'foo';
},
};
12 changes: 12 additions & 0 deletions packages/core/integration-tests/test/scope-hoisting.js
Expand Up @@ -995,6 +995,18 @@ describe('scope hoisting', function() {
assert.deepEqual(output, 'foo');
});

it('support chained namespace reexports of CommonJS', async function() {
let b = await bundle(
path.join(
__dirname,
'/integration/scope-hoisting/es6/re-export-commonjs-wildcard/a.js',
),
);

let output = await run(b);
assert.deepEqual(output, 'foo');
});

it('should support named imports on wrapped modules', async function() {
let b = await bundle(
path.join(
Expand Down
1 change: 1 addition & 0 deletions packages/shared/scope-hoisting/src/link.js
Expand Up @@ -231,6 +231,7 @@ export function link({
}

// Look for an exports object if we bailed out.
// TODO remove the first part of the condition once bundleGraph.resolveSymbol().identifier === null covers this
if ((node === undefined && mod.meta.isCommonJS) || node === null) {
node = findSymbol(path, assertString(mod.meta.exportsIdentifier));
if (!node) {
Expand Down

0 comments on commit d19bee1

Please sign in to comment.