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

fix(compat): cjs/esm interop for dynamic imports #13792

Merged
merged 4 commits into from
Mar 11, 2022

Conversation

bartlomieju
Copy link
Member

@bartlomieju bartlomieju commented Feb 28, 2022

This commit fixes CJS/ESM interop in compat mode for dynamically
imported modules.

"ProcState::prepare_module_load" was changed to accept a list
of "graph roots" without associated "module kind". That module kind
was always hardcoded to "ESM" which is not true for CJS/ESM interop -
a CommonJs module might be imported using "import()" function. In
such case the root of the graph should have "CommonJs" module kind
instead of "ESM".

Fixes #13790

@bartlomieju
Copy link
Member Author

Culprit is here:

deno/cli/module_loader.rs

Lines 109 to 120 in a41d399

async move {
ps.prepare_module_load(
vec![(specifier, deno_graph::ModuleKind::Esm)],
is_dynamic,
lib,
root_permissions,
dynamic_permissions,
false,
)
.await
}
.boxed_local()

Root of the graph is always marked as ESM, which in this instance is not correct.

@bartlomieju
Copy link
Member Author

@kitsonk could you take a look? I feel we should be using resolver inside ModuleLoader::prepare_load and pass resolved specifier and module kind based on the ResolveResponse instead of hard coding module kind to Esm.

@bartlomieju bartlomieju changed the title test(compat): add test for dynamic import cjs/esm interop fix(compat): cjs/esm interop for dynamic imports Mar 5, 2022
@bartlomieju bartlomieju requested a review from kitsonk March 5, 2022 01:41
@bartlomieju
Copy link
Member Author

ping @kitsonk

@kitsonk
Copy link
Contributor

kitsonk commented Mar 10, 2022

Ah sorry, missed this. I will look tomorrow my time, apologies!

Copy link
Contributor

@kitsonk kitsonk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

if let Some(resolver) = &maybe_resolver {
let response =
resolver.resolve(r.as_str(), &Url::parse("unused:").unwrap());
// TODO(bartlomieju): this should be implemented in `deno_graph`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for opening this!

@bartlomieju bartlomieju merged commit 808f797 into denoland:main Mar 11, 2022
@bartlomieju bartlomieju deleted the fix_cjs_esm_translation branch March 11, 2022 01:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants