-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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: improved support for cjs and cts modules #26558
fix: improved support for cjs and cts modules #26558
Conversation
…to refactor_handle_program_more
pub struct ModuleInfoCache { | ||
conn: CacheDB, | ||
parsed_source_cache: Arc<ParsedSourceCache>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shifted from being pased along horizontally to being a ctor dependency.
} | ||
} | ||
|
||
pub fn analyze_sync( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This sync version is for the require loader.
// this will conditionally parse because it's using a CapturingModuleParser | ||
parser.parse_module(ParseOptions { | ||
// this will conditionally parse because it's using a CapturingEsParser | ||
parser.parse_program(ParseOptions { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Part of this PR is shifting everything over to using parse_program
so that we can quickly tell if a module is definitely an ES module.
@@ -150,42 +147,3 @@ impl deno_graph::ParsedSourceStore for ParsedSourceCache { | |||
} | |||
} | |||
} | |||
|
|||
pub struct EsmOrCjsChecker { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was removed and replaced with CjsTracker.
) -> MediaType { | ||
if resolver.in_node_modules(specifier) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't change the media type from stuff like JS to TS when providing it to typescript anymore because this isn't something we can determine at resolution (only when loading). This is fine because we tell typescript about the "node module kind" (if ESM or CJS)
Ok(Cow::Borrowed(path)) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was moved into the NodeRequireLoader
|
||
pub fn url_to_node_resolution( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
url_to_node_resolution is gone since we no longer determine if something is esm or cjs at resolution time, but rather after loading when we can see if it has imports/exports/tla/etc.
Self { env } | ||
} | ||
|
||
pub fn get_closest_package_json( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All this code was extracted out of other places.
@@ -73,16 +66,14 @@ impl NodeResolutionMode { | |||
|
|||
#[derive(Debug)] | |||
pub enum NodeResolution { | |||
Esm(Url), | |||
CommonJs(Url), | |||
Module(Url), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We no longer know this at resolution time.
Cargo.toml
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sorry, this PR is hard to split up into multiple smaller ones. There's so much stuff that's intertwined.
The main themes are using "program" more and determining if a js
, jsx
, ts
, or tsx
file is cjs or esm always only happens after loading it now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @arnauorriols on these changes and this PR in general. There's now a NodeRequireLoader
trait for loading files with require(...)
and PackageJsonResolver
struct that searches for package.jsons. Let me know if you'd like a walkthrough of the changes.
…erret/deno into refactor_handle_program_more
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First pass, ended at cli/resolver.rs
because the rest of files doesn't load in the browser :(
// todo(dsherret): support in the lsp by stabilizing the feature | ||
// so that we don't have to pipe the config in here |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe link it in an issue so it's not forgotten?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or the PR that stabilizes it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The tracking issue has it: #26225 (just updated it to be more clear)
@@ -53,16 +53,6 @@ pub async fn compile( | |||
); | |||
} | |||
|
|||
if cli_options.unstable_detect_cjs() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this on purpose? Shouldn't we land #26439 first?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. It works in deno compile now so I got rid of the warning saying it doesn't work.
}) | ||
.unwrap_or(false); | ||
// todo(dsherret): how to avoid cloning here? | ||
Ok(code.to_string()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can make OpLoadResponse
accept Arc<str>
and then the op_load
would return a v8::Array
that would manually serialize to v8::String
|
||
let source_code = format!( | ||
r#"(function loadCjsModule(moduleName, isMain, inspectBrk) {{ | ||
Deno[Deno.internal].node.loadCjsModule(moduleName, isMain, inspectBrk); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
loadCjsModule
can probably be removed now. I can do that in a follow up
* cts support * better cjs/cts type checking * deno compile cjs/cts support * More efficient detect cjs (going towards stabilization) * Determination of whether .js, .ts, .jsx, or .tsx is cjs or esm is only done after loading * Support `import x = require(...);` Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
Program
instead ofModule
more deno_ast#281Closes #26651
Closes #25498