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: improved support for cjs and cts modules #26558

Merged
merged 56 commits into from
Nov 1, 2024
Merged
Changes from 1 commit
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
c11079d
refactor: prefer parsing as `Program` over `Module`
dsherret Oct 22, 2024
904c316
more updates
dsherret Oct 22, 2024
4335aa0
Starting on this, but going to revert.
dsherret Oct 22, 2024
29876a5
Merge branch 'main' into refactor_handle_program_more
dsherret Oct 25, 2024
7410544
compiling
dsherret Oct 25, 2024
34b1a88
working
dsherret Oct 25, 2024
873da6b
Start on translating code
dsherret Oct 25, 2024
342c81c
perf: pass transpiled modules to deno_core as a string
dsherret Oct 25, 2024
3cd7c94
update
dsherret Oct 25, 2024
eedcdca
Merge branch 'main' into perf_pass_transpiled_modules_to_deno_core_as…
dsherret Oct 25, 2024
d1d7b36
Merge branch 'main' into refactor_handle_program_more
dsherret Oct 25, 2024
8540344
update
dsherret Oct 25, 2024
3a83933
Merge branch 'perf_pass_transpiled_modules_to_deno_core_as_string' in…
dsherret Oct 25, 2024
af06589
better cts support
dsherret Oct 25, 2024
21b5d06
Merge branch 'main' into refactor_handle_program_more
dsherret Oct 26, 2024
eb9f9ff
fix(check): expose more globals from @types/node
dsherret Oct 28, 2024
eaa4ea7
Merge branch 'fix_expose_types_node_globals' into refactor_handle_pro…
dsherret Oct 28, 2024
941eefd
Merge branch 'main' into refactor_handle_program_more
dsherret Oct 28, 2024
9e7ad6f
support in deno compile
dsherret Oct 28, 2024
39e928f
working test
dsherret Oct 28, 2024
bd824a6
updates to pass around the module kind
dsherret Oct 29, 2024
01c773f
Create PackageJsonResolver
dsherret Oct 29, 2024
63acd1a
Use fs more
dsherret Oct 29, 2024
84f1f70
better cjs/esm tracking
dsherret Oct 29, 2024
ebaa01f
compiling large refactor
dsherret Oct 29, 2024
cf9fd71
fix bug
dsherret Oct 29, 2024
e0b0fb5
passing tests
dsherret Oct 29, 2024
c8361e8
Start working on removing `NodeResolution`
dsherret Oct 29, 2024
e513f6a
commit and going to revert to simplify
dsherret Oct 29, 2024
e7e1299
More passing tests
dsherret Oct 29, 2024
fd26e78
going to revert
dsherret Oct 29, 2024
d23a91a
Add LspCjsTracker
dsherret Oct 29, 2024
d56c766
remove resolving media type in lsp based on node resolution
dsherret Oct 29, 2024
9e7d896
fix json imports
dsherret Oct 29, 2024
4d3097a
Remove url_to_node_resolution
dsherret Oct 30, 2024
157dfd5
update test
dsherret Oct 30, 2024
eb2a381
Merge branch 'main' into refactor_handle_program_more
dsherret Oct 30, 2024
41b7742
lint
dsherret Oct 30, 2024
5582eda
bump deno_ast
dsherret Oct 30, 2024
fc4ff68
Fix node compat tests
dsherret Oct 30, 2024
220e782
do not require --allow-read if requiring modules found in the module …
dsherret Oct 30, 2024
9aaa4d7
more tests
dsherret Oct 30, 2024
1e28b03
Merge branch 'main' into refactor_handle_program_more
dsherret Oct 30, 2024
5049c3c
fix small issues and add jsx tests
dsherret Oct 30, 2024
ecde7bd
Merge branch 'main' into refactor_handle_program_more
dsherret Oct 30, 2024
adcf6d3
Bump ci
dsherret Oct 30, 2024
2636535
mark main CJS module
bartlomieju Oct 31, 2024
d47411b
add a test for main cjs module
bartlomieju Oct 31, 2024
0693427
Merge branch 'main' into refactor_handle_program_more
dsherret Oct 31, 2024
66c139d
Update
dsherret Oct 31, 2024
b2500dd
Support TsImportEquals
dsherret Oct 31, 2024
5e86e00
Merge branch 'refactor_handle_program_more' of https://github.com/dsh…
dsherret Oct 31, 2024
ef85fc3
Merge branch 'main' into refactor_handle_program_more
dsherret Oct 31, 2024
c21c283
proper support for ts import equals and export equals
dsherret Oct 31, 2024
0411a73
Add some more import equals tests
dsherret Oct 31, 2024
a471da0
Merge branch 'main' into refactor_handle_program_more
dsherret Nov 1, 2024
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
Prev Previous commit
Next Next commit
better cjs/esm tracking
dsherret committed Oct 29, 2024
commit 84f1f7083d726ec300306dfcd9ce71bbfa1a181d
3 changes: 1 addition & 2 deletions cli/factory.rs
Original file line number Diff line number Diff line change
@@ -845,9 +845,8 @@ impl CliFactory {
cli_npm_resolver.clone(),
NpmModuleLoader::new(
self.cjs_tracker()?.clone(),
node_code_translator.clone(),
fs.clone(),
cli_node_resolver.clone(),
node_code_translator.clone(),
),
self.parsed_source_cache().clone(),
self.resolver().await?.clone(),
9 changes: 6 additions & 3 deletions cli/module_loader.rs
Original file line number Diff line number Diff line change
@@ -416,7 +416,7 @@ impl<TGraphContainer: ModuleGraphContainer>
if let Some(code_source) = self.load_prepared_module(specifier).await? {
return Ok(code_source);
}
if self.shared.npm_module_loader.if_in_npm_package(specifier) {
if self.shared.node_resolver.in_npm_package(specifier) {
return self
.shared
.npm_module_loader
@@ -583,7 +583,10 @@ impl<TGraphContainer: ModuleGraphContainer>
specifier,
media_type,
source,
}) => self.load_cjs(specifier, media_type, source).await.map(Some),
}) => self
.load_maybe_cjs(specifier, media_type, source)
.await
.map(Some),
None => Ok(None),
}
}
@@ -713,7 +716,7 @@ impl<TGraphContainer: ModuleGraphContainer>
}
}

async fn load_cjs(
async fn load_maybe_cjs(
&self,
specifier: &ModuleSpecifier,
media_type: MediaType,
11 changes: 9 additions & 2 deletions cli/node.rs
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ use std::borrow::Cow;
use std::sync::Arc;

use deno_ast::MediaType;
use deno_ast::ModuleKind;
use deno_ast::ModuleSpecifier;
use deno_core::error::AnyError;
use deno_graph::ParsedSourceStore;
@@ -99,7 +100,8 @@ impl CliCjsCodeAnalyzer {
});
}

let treat_as_cjs = self.cjs_tracker.treat_as_cjs(&specifier)?;
let cjs_tracker = self.cjs_tracker.clone();
let treat_as_cjs = cjs_tracker.treat_as_cjs(&specifier)?;
let analysis = if treat_as_cjs {
let maybe_parsed_source = self
.parsed_source_cache
@@ -121,7 +123,12 @@ impl CliCjsCodeAnalyzer {
maybe_syntax: None,
})
})?;
if parsed_source.is_script() && treat_as_cjs {
let is_cjs = parsed_source.is_script() && treat_as_cjs;
cjs_tracker.mark_kind(
parsed_source.specifier().clone(),
ModuleKind::from_is_cjs(is_cjs),
);
if is_cjs {
let analysis = parsed_source.analyze_cjs();
Ok(CliCjsAnalysis::Cjs {
exports: analysis.exports,
44 changes: 22 additions & 22 deletions cli/resolver.rs
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ use async_trait::async_trait;
use dashmap::DashMap;
use dashmap::DashSet;
use deno_ast::MediaType;
use deno_ast::ModuleKind;
use deno_config::workspace::MappedResolution;
use deno_config::workspace::MappedResolutionDiagnostic;
use deno_config::workspace::MappedResolutionError;
@@ -329,46 +330,45 @@ impl CliNodeResolver {
&self,
resolution: NodeResolution,
) -> NodeResolution {
if let NodeResolution::CommonJs(specifier) = &resolution {
// remember that this was a common js resolution
self.mark_cjs_resolution(specifier.clone());
match &resolution {
NodeResolution::CommonJs(specifier) => {
// remember that this was a common js resolution
self
.cjs_tracker
.mark_kind(specifier.clone(), ModuleKind::Cjs);
}
NodeResolution::Esm(specifier) => {
self
.cjs_tracker
.mark_kind(specifier.clone(), ModuleKind::Esm);
}
NodeResolution::BuiltIn(_) => {}
}
resolution
}

pub fn mark_cjs_resolution(&self, specifier: ModuleSpecifier) {
self.cjs_tracker.insert(specifier);
}
}

// todo(dsherret): move to module_loader.rs
#[derive(Clone)]
pub struct NpmModuleLoader {
cjs_tracker: Arc<CjsTracker>,
node_code_translator: Arc<CliNodeCodeTranslator>,
fs: Arc<dyn deno_fs::FileSystem>,
node_resolver: Arc<CliNodeResolver>,
node_code_translator: Arc<CliNodeCodeTranslator>,
}

impl NpmModuleLoader {
pub fn new(
cjs_tracker: Arc<CjsTracker>,
node_code_translator: Arc<CliNodeCodeTranslator>,
fs: Arc<dyn deno_fs::FileSystem>,
node_resolver: Arc<CliNodeResolver>,
node_code_translator: Arc<CliNodeCodeTranslator>,
) -> Self {
Self {
cjs_tracker,
node_code_translator,
fs,
node_resolver,
}
}

pub fn if_in_npm_package(&self, specifier: &ModuleSpecifier) -> bool {
self.node_resolver.in_npm_package(specifier)
}

pub async fn load(
&self,
specifier: &ModuleSpecifier,
@@ -442,7 +442,7 @@ pub struct CjsTrackerOptions {
#[derive(Debug)]
pub struct CjsTracker {
unstable_detect_cjs: bool,
set: DashSet<ModuleSpecifier>,
known: DashMap<ModuleSpecifier, ModuleKind>,
pkg_json_resolver: Arc<PackageJsonResolver>,
}

@@ -453,7 +453,7 @@ impl CjsTracker {
) -> Self {
Self {
unstable_detect_cjs: options.unstable_detect_cjs,
set: DashSet::new(),
known: Default::default(),
pkg_json_resolver,
}
}
@@ -482,8 +482,8 @@ impl CjsTracker {
| MediaType::Wasm
| MediaType::SourceMap
| MediaType::Unknown => {
if self.set.contains(specifier) {
return Ok(true);
if let Some(value) = self.known.get(specifier) {
return Ok(value.is_cjs());
}

if self.unstable_detect_cjs {
@@ -499,8 +499,8 @@ impl CjsTracker {
}
}

pub fn insert(&self, specifier: ModuleSpecifier) {
self.set.insert(specifier);
pub fn mark_kind(&self, specifier: ModuleSpecifier, kind: ModuleKind) {
self.known.insert(specifier, kind);
}
}

3 changes: 1 addition & 2 deletions cli/standalone/mod.rs
Original file line number Diff line number Diff line change
@@ -671,9 +671,8 @@ pub async fn run(data: StandaloneData) -> Result<i32, AnyError> {
node_resolver: cli_node_resolver.clone(),
npm_module_loader: Arc::new(NpmModuleLoader::new(
cjs_tracker,
node_code_translator,
fs.clone(),
cli_node_resolver,
node_code_translator,
)),
npm_resolver: npm_resolver.clone(),
workspace_resolver,