diff --git a/packages/core/integration-tests/test/integration/js-export-default-fn-circular-anonymous/a.mjs b/packages/core/integration-tests/test/integration/js-export-default-fn-circular-anonymous/a.mjs new file mode 100644 index 00000000000..513ed2a03dc --- /dev/null +++ b/packages/core/integration-tests/test/integration/js-export-default-fn-circular-anonymous/a.mjs @@ -0,0 +1 @@ +import "./b.mjs"; diff --git a/packages/core/integration-tests/test/integration/js-export-default-fn-circular-anonymous/b.mjs b/packages/core/integration-tests/test/integration/js-export-default-fn-circular-anonymous/b.mjs new file mode 100644 index 00000000000..7cc245868de --- /dev/null +++ b/packages/core/integration-tests/test/integration/js-export-default-fn-circular-anonymous/b.mjs @@ -0,0 +1,7 @@ +import { c } from "./c.mjs"; // imports and calls b1 (circular) + +export default function () { + return "b1"; +} + +let str = c + "str"; diff --git a/packages/core/integration-tests/test/integration/js-export-default-fn-circular-anonymous/c.mjs b/packages/core/integration-tests/test/integration/js-export-default-fn-circular-anonymous/c.mjs new file mode 100644 index 00000000000..70d0cf189ce --- /dev/null +++ b/packages/core/integration-tests/test/integration/js-export-default-fn-circular-anonymous/c.mjs @@ -0,0 +1,5 @@ +import b1 from "./b.mjs"; + +export const c = b1(); + +result(c); diff --git a/packages/core/integration-tests/test/integration/js-export-default-fn-circular-named/a.mjs b/packages/core/integration-tests/test/integration/js-export-default-fn-circular-named/a.mjs new file mode 100644 index 00000000000..513ed2a03dc --- /dev/null +++ b/packages/core/integration-tests/test/integration/js-export-default-fn-circular-named/a.mjs @@ -0,0 +1 @@ +import "./b.mjs"; diff --git a/packages/core/integration-tests/test/integration/js-export-default-fn-circular-named/b.mjs b/packages/core/integration-tests/test/integration/js-export-default-fn-circular-named/b.mjs new file mode 100644 index 00000000000..92a1e6f08f9 --- /dev/null +++ b/packages/core/integration-tests/test/integration/js-export-default-fn-circular-named/b.mjs @@ -0,0 +1,7 @@ +import { c } from "./c.mjs"; // imports and calls b1 (circular) + +export default function b1() { + return "b1"; +} + +let str = c + "str"; diff --git a/packages/core/integration-tests/test/integration/js-export-default-fn-circular-named/c.mjs b/packages/core/integration-tests/test/integration/js-export-default-fn-circular-named/c.mjs new file mode 100644 index 00000000000..70d0cf189ce --- /dev/null +++ b/packages/core/integration-tests/test/integration/js-export-default-fn-circular-named/c.mjs @@ -0,0 +1,5 @@ +import b1 from "./b.mjs"; + +export const c = b1(); + +result(c); diff --git a/packages/core/integration-tests/test/javascript.js b/packages/core/integration-tests/test/javascript.js index 760cb45c07c..8a3db3ec077 100644 --- a/packages/core/integration-tests/test/javascript.js +++ b/packages/core/integration-tests/test/javascript.js @@ -5235,6 +5235,38 @@ describe('javascript', function () { assert.deepEqual(res, {other: 1}); }); + it('should hoist function default exports to allow circular imports', async function () { + let b = await bundle( + path.join( + __dirname, + '/integration/js-export-default-fn-circular-named/a.mjs', + ), + ); + + let output; + function result(v) { + output = v; + } + await run(b, {result}); + assert.deepEqual(output, 'b1'); + }); + + it('should hoist anonymous function default exports to allow circular imports', async function () { + let b = await bundle( + path.join( + __dirname, + '/integration/js-export-default-fn-circular-anonymous/a.mjs', + ), + ); + + let output; + function result(v) { + output = v; + } + await run(b, {result}); + assert.deepEqual(output, 'b1'); + }); + it('should work with many different types of exports', async function () { let b = await bundle( path.join(__dirname, 'integration/js-export-many/index.js'), diff --git a/packages/transformers/js/core/src/modules.rs b/packages/transformers/js/core/src/modules.rs index 217d4c08c06..88ae24be541 100644 --- a/packages/transformers/js/core/src/modules.rs +++ b/packages/transformers/js/core/src/modules.rs @@ -460,20 +460,16 @@ impl Fold for ESMFold { declare: false, function: func.function.clone(), })))); - items.push(self.create_exports_assign( - "default".into(), - Expr::Ident(ident.clone()), - DUMMY_SP, - )); + self.create_export("default".into(), Expr::Ident(ident.clone()), DUMMY_SP); } else { - items.push(self.create_exports_assign( + self.create_export( "default".into(), Expr::Fn(FnExpr { ident: None, function: func.function.clone(), }), export.span, - )); + ); } } _ => {