Skip to content

Commit

Permalink
feat: resolve node builtin module without node: scheme (#295)
Browse files Browse the repository at this point in the history
  • Loading branch information
kt3k authored Sep 28, 2023
1 parent fab232b commit 3d51f4f
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 12 deletions.
1 change: 1 addition & 0 deletions lib/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ pub fn js_parse_module(
content.into(),
maybe_resolver.as_ref().map(|r| r as &dyn Resolver),
None,
None,
) {
Ok(module) => {
let serializer =
Expand Down
79 changes: 67 additions & 12 deletions src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,7 @@ impl GraphImport {
referrer: &ModuleSpecifier,
imports: Vec<String>,
maybe_resolver: Option<&dyn Resolver>,
maybe_npm_resolver: Option<&dyn NpmResolver>,
) -> Self {
let dependencies = imports
.into_iter()
Expand All @@ -831,7 +832,8 @@ impl GraphImport {
start: Position::zeroed(),
end: Position::zeroed(),
};
let maybe_type = resolve(&import, referrer_range, maybe_resolver);
let maybe_type =
resolve(&import, referrer_range, maybe_resolver, maybe_npm_resolver);
(
import,
Dependency {
Expand Down Expand Up @@ -1550,13 +1552,32 @@ fn resolve(
specifier_text: &str,
referrer_range: Range,
maybe_resolver: Option<&dyn Resolver>,
maybe_npm_resolver: Option<&dyn NpmResolver>,
) -> Resolution {
let response = if let Some(resolver) = maybe_resolver {
resolver.resolve(specifier_text, &referrer_range.specifier)
} else {
resolve_import(specifier_text, &referrer_range.specifier)
.map_err(|err| err.into())
};
use ResolveError::*;
use SpecifierError::*;
if let Err(Specifier(ImportPrefixMissing(_, _))) = response.as_ref() {
if let Some(npm_resolver) = maybe_npm_resolver {
if let Ok(specifier) =
ModuleSpecifier::parse(&format!("node:{}", specifier_text))
{
if npm_resolver.resolve_builtin_node_module(&specifier).is_ok() {
npm_resolver.on_resolve_bare_builtin_node_module(specifier_text);
return Resolution::from_resolve_result(
Ok(specifier),
specifier_text,
referrer_range,
);
}
}
}
}
Resolution::from_resolve_result(response, specifier_text, referrer_range)
}

Expand Down Expand Up @@ -1620,6 +1641,7 @@ pub(crate) fn parse_module(
module_analyzer: &dyn ModuleAnalyzer,
is_root: bool,
is_dynamic_branch: bool,
maybe_npm_resolver: Option<&dyn NpmResolver>,
) -> Result<Module, ModuleGraphError> {
let media_type =
MediaType::from_specifier_and_headers(specifier, maybe_headers);
Expand Down Expand Up @@ -1686,6 +1708,7 @@ pub(crate) fn parse_module(
module_info,
content,
maybe_resolver,
maybe_npm_resolver,
)))
}
Err(diagnostic) => Err(ModuleGraphError::ModuleError(
Expand All @@ -1708,6 +1731,7 @@ pub(crate) fn parse_module(
module_info,
content,
maybe_resolver,
maybe_npm_resolver,
)))
}
Err(diagnostic) => Err(ModuleGraphError::ModuleError(
Expand All @@ -1732,6 +1756,7 @@ pub(crate) fn parse_esm_module_from_module_info(
module_info: ModuleInfo,
source: Arc<str>,
maybe_resolver: Option<&dyn Resolver>,
maybe_npm_resolver: Option<&dyn NpmResolver>,
) -> EsmModule {
let mut module = EsmModule::new(specifier.clone(), source);
module.media_type = media_type;
Expand All @@ -1747,8 +1772,12 @@ pub(crate) fn parse_esm_module_from_module_info(
let range =
Range::from_position_range(module.specifier.clone(), specifier.range);
if dep.maybe_type.is_none() {
dep.maybe_type =
resolve(&specifier.text, range.clone(), maybe_resolver);
dep.maybe_type = resolve(
&specifier.text,
range.clone(),
maybe_resolver,
maybe_npm_resolver,
);
}
dep.imports.push(Import {
specifier: specifier.text,
Expand All @@ -1761,8 +1790,12 @@ pub(crate) fn parse_esm_module_from_module_info(
TypeScriptReference::Types(specifier) => {
let range =
Range::from_position_range(module.specifier.clone(), specifier.range);
let dep_resolution =
resolve(&specifier.text, range.clone(), maybe_resolver);
let dep_resolution = resolve(
&specifier.text,
range.clone(),
maybe_resolver,
maybe_npm_resolver,
);
if is_untyped(&module.media_type) {
module.maybe_types_dependency = Some(TypesDependency {
specifier: specifier.text.clone(),
Expand Down Expand Up @@ -1823,8 +1856,12 @@ pub(crate) fn parse_esm_module_from_module_info(
import_source.range,
);
if dep.maybe_code.is_none() {
dep.maybe_code =
resolve(&specifier_text, range.clone(), maybe_resolver);
dep.maybe_code = resolve(
&specifier_text,
range.clone(),
maybe_resolver,
maybe_npm_resolver,
);
}
dep.imports.push(Import {
specifier: specifier_text,
Expand All @@ -1845,7 +1882,12 @@ pub(crate) fn parse_esm_module_from_module_info(
let range =
Range::from_position_range(module.specifier.clone(), specifier.range);
if dep.maybe_type.is_none() {
dep.maybe_type = resolve(&specifier.text, range.clone(), maybe_resolver);
dep.maybe_type = resolve(
&specifier.text,
range.clone(),
maybe_resolver,
maybe_npm_resolver,
);
}
dep.imports.push(Import {
specifier: specifier.text,
Expand All @@ -1867,7 +1909,12 @@ pub(crate) fn parse_esm_module_from_module_info(
};
module.maybe_types_dependency = Some(TypesDependency {
specifier: types_header.to_string(),
dependency: resolve(types_header, range, maybe_resolver),
dependency: resolve(
types_header,
range,
maybe_resolver,
maybe_npm_resolver,
),
});
}
}
Expand Down Expand Up @@ -1929,8 +1976,12 @@ pub(crate) fn parse_esm_module_from_module_info(
module.specifier.clone(),
desc.specifier_range.clone(),
);
let dep_resolution =
resolve(&desc.specifier, range.clone(), maybe_resolver);
let dep_resolution = resolve(
&desc.specifier,
range.clone(),
maybe_resolver,
maybe_npm_resolver,
);
if matches!(
desc.kind,
DependencyKind::ImportType | DependencyKind::ExportType
Expand Down Expand Up @@ -1973,6 +2024,7 @@ pub(crate) fn parse_esm_module_from_module_info(
&pragma.specifier,
Range::from_position_range(specifier, pragma.range),
maybe_resolver,
maybe_npm_resolver,
)
} else {
Resolution::None
Expand Down Expand Up @@ -2349,7 +2401,8 @@ impl<'a, 'graph> Builder<'a, 'graph> {
for referrer_imports in imports {
let referrer = referrer_imports.referrer;
let imports = referrer_imports.imports;
let graph_import = GraphImport::new(&referrer, imports, self.resolver);
let graph_import =
GraphImport::new(&referrer, imports, self.resolver, self.npm_resolver);
for dep in graph_import.dependencies.values() {
if let Resolution::Ok(resolved) = &dep.maybe_type {
self.load(
Expand Down Expand Up @@ -3157,6 +3210,7 @@ impl<'a, 'graph> Builder<'a, 'graph> {
.unwrap_or(self.module_analyzer),
is_root,
self.in_dynamic_branch,
self.npm_resolver,
) {
Ok(module) => ModuleSlot::Module(module),
Err(err) => ModuleSlot::Err(err),
Expand Down Expand Up @@ -3615,6 +3669,7 @@ mod tests {
&module_analyzer,
true,
false,
None,
)
.unwrap();
let module = module.esm().unwrap();
Expand Down
Loading

0 comments on commit 3d51f4f

Please sign in to comment.