diff --git a/packages/core/core/src/BundleGraph.js b/packages/core/core/src/BundleGraph.js index 2a39a98c531..6348d666269 100644 --- a/packages/core/core/src/BundleGraph.js +++ b/packages/core/core/src/BundleGraph.js @@ -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; } @@ -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). @@ -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, }; } diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/es6/re-export-commonjs-wildcard/a.js b/packages/core/integration-tests/test/integration/scope-hoisting/es6/re-export-commonjs-wildcard/a.js new file mode 100644 index 00000000000..22d58184413 --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/es6/re-export-commonjs-wildcard/a.js @@ -0,0 +1,3 @@ +import { createElement } from "./b.js"; + +output = createElement(); diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/es6/re-export-commonjs-wildcard/b.js b/packages/core/integration-tests/test/integration/scope-hoisting/es6/re-export-commonjs-wildcard/b.js new file mode 100644 index 00000000000..a7ce11c1ce6 --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/es6/re-export-commonjs-wildcard/b.js @@ -0,0 +1 @@ +export * from './c.js'; diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/es6/re-export-commonjs-wildcard/c.js b/packages/core/integration-tests/test/integration/scope-hoisting/es6/re-export-commonjs-wildcard/c.js new file mode 100644 index 00000000000..5b85182aae7 --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/es6/re-export-commonjs-wildcard/c.js @@ -0,0 +1,3 @@ +import { createElement } from './d.js'; + +export { createElement }; diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/es6/re-export-commonjs-wildcard/d.js b/packages/core/integration-tests/test/integration/scope-hoisting/es6/re-export-commonjs-wildcard/d.js new file mode 100644 index 00000000000..dbd76e900a9 --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/es6/re-export-commonjs-wildcard/d.js @@ -0,0 +1,5 @@ +module.exports = { + createElement: function () { + return 'foo'; + }, +}; diff --git a/packages/core/integration-tests/test/scope-hoisting.js b/packages/core/integration-tests/test/scope-hoisting.js index e741dd0e4b5..a0f81fee812 100644 --- a/packages/core/integration-tests/test/scope-hoisting.js +++ b/packages/core/integration-tests/test/scope-hoisting.js @@ -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( diff --git a/packages/shared/scope-hoisting/src/link.js b/packages/shared/scope-hoisting/src/link.js index 321d343fb76..07948e17494 100644 --- a/packages/shared/scope-hoisting/src/link.js +++ b/packages/shared/scope-hoisting/src/link.js @@ -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) {