Skip to content

Commit

Permalink
Treat re-exports of '*' from empty files as ESM (#9079)
Browse files Browse the repository at this point in the history
  • Loading branch information
lettertwo authored Jun 8, 2023
1 parent 44c5d73 commit 4db35cd
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {bar} from 'lib';

output = `foo ${bar}`;

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions packages/core/integration-tests/test/scope-hoisting.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,28 @@ describe('scope hoisting', function () {
assert.strictEqual(output, '2 4');
});

it('supports re-exporting all from an empty module without side effects', async function () {
let b = await bundle(
path.join(
__dirname,
'/integration/scope-hoisting/es6/re-export-all-empty-no-side-effects/index.js',
),
{
mode: 'production',
},
);

let output = await run(b);
assert.strictEqual(output, 'foo bar');

let contents = await outputFS.readFile(
b.getBundles().find(b => b.getMainEntry().filePath.endsWith('index.js'))
.filePath,
'utf8',
);
assert.match(contents, /output="foo bar"/);
});

it('supports re-exporting all exports from an external module', async function () {
let b = await bundle(
path.join(
Expand Down
4 changes: 3 additions & 1 deletion packages/transformers/js/src/JSTransformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -813,11 +813,13 @@ export default (new Transformer({
});
}

// Add * symbol if there are CJS exports, no imports/exports at all, or the asset is wrapped.
// Add * symbol if there are CJS exports, no imports/exports at all
// (and the asset has side effects), or the asset is wrapped.
// This allows accessing symbols that don't exist without errors in symbol propagation.
if (
hoist_result.has_cjs_exports ||
(!hoist_result.is_esm &&
asset.sideEffects &&
deps.size === 0 &&
Object.keys(hoist_result.exported_symbols).length === 0) ||
(hoist_result.should_wrap && !asset.symbols.hasExportSymbol('*'))
Expand Down

0 comments on commit 4db35cd

Please sign in to comment.