Skip to content

Commit

Permalink
Hoist exports to allow circular dependencies (#9024)
Browse files Browse the repository at this point in the history
  • Loading branch information
AGawrys committed May 19, 2023
1 parent e46be4d commit 3554866
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "./b.mjs";
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { c } from "./c.mjs"; // imports and calls b1 (circular)

export default function () {
return "b1";
}

let str = c + "str";
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import b1 from "./b.mjs";

export const c = b1();

result(c);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "./b.mjs";
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { c } from "./c.mjs"; // imports and calls b1 (circular)

export default function b1() {
return "b1";
}

let str = c + "str";
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import b1 from "./b.mjs";

export const c = b1();

result(c);
32 changes: 32 additions & 0 deletions packages/core/integration-tests/test/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
10 changes: 3 additions & 7 deletions packages/transformers/js/core/src/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
));
);
}
}
_ => {
Expand Down

0 comments on commit 3554866

Please sign in to comment.