Skip to content

Commit fd1165c

Browse files
authored
fix: dynamic imports error the graph (#55)
1 parent 97d59ca commit fd1165c

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

src/testdata/emit/dynamic.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const { default: data } = await import("./data.json", {
2+
assert: {
3+
type: "json"
4+
}
5+
});

src/testdata/source/dynamic.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const { default: data } = await import("./data.json", {
2+
assert: { type: "json" },
3+
});

src/v2.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,9 @@ impl EszipV2 {
549549

550550
ordered_modules.push(specifier.to_string());
551551
for dep in module.dependencies.values() {
552+
if dep.is_dynamic {
553+
continue;
554+
}
552555
if let Some(specifier) = dep.get_code() {
553556
visit_module(
554557
graph,
@@ -698,7 +701,9 @@ mod tests {
698701
specifier: &ModuleSpecifier,
699702
is_dynamic: bool,
700703
) -> deno_graph::source::LoadFuture {
701-
assert!(!is_dynamic);
704+
if is_dynamic {
705+
return Box::pin(async { Ok(None) });
706+
}
702707
assert_eq!(specifier.scheme(), "file");
703708
let path = format!("./src/testdata/source{}", specifier.path());
704709
Box::pin(async move {
@@ -804,6 +809,36 @@ mod tests {
804809
assert_eq!(module.kind, ModuleKind::Json);
805810
}
806811

812+
#[tokio::test]
813+
async fn from_graph_dynamic() {
814+
let roots = vec![(
815+
ModuleSpecifier::parse("file:///dynamic.ts").unwrap(),
816+
deno_graph::ModuleKind::Esm,
817+
)];
818+
let graph = deno_graph::create_graph(
819+
roots,
820+
false,
821+
None,
822+
&mut FileLoader,
823+
None,
824+
None,
825+
None,
826+
None,
827+
)
828+
.await;
829+
graph.valid().unwrap();
830+
let eszip =
831+
super::EszipV2::from_graph(graph, EmitOptions::default()).unwrap();
832+
let module = eszip.get_module("file:///dynamic.ts").unwrap();
833+
assert_eq!(module.specifier, "file:///dynamic.ts");
834+
let source = module.source().await;
835+
assert_eq!(&*source, include_bytes!("./testdata/emit/dynamic.ts"));
836+
let _source_map = module.source_map().await.unwrap();
837+
assert_eq!(module.kind, ModuleKind::JavaScript);
838+
let module = eszip.get_module("file:///data.json");
839+
assert!(module.is_none());
840+
}
841+
807842
#[tokio::test]
808843
async fn file_format_parse_redirect() {
809844
let file = tokio::fs::File::open("./src/testdata/redirect.eszip2")

0 commit comments

Comments
 (0)