Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hoist exports to allow circular dependencies #9024

Merged
merged 8 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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