diff --git a/crates/browser/src/module_loader.rs b/crates/browser/src/module_loader.rs index 26dbc84..0797736 100644 --- a/crates/browser/src/module_loader.rs +++ b/crates/browser/src/module_loader.rs @@ -13,7 +13,13 @@ //! Specifier resolution currently handles relative (`./`, `../`, `/`) and //! absolute-URL specifiers. Bare specifiers (which require an import map) are //! left unresolved; their imported bindings evaluate to `undefined`. Import -//! maps and dynamic `import()` are tracked as follow-up sub-steps. +//! maps are tracked as a follow-up sub-step. +//! +//! Statically-discoverable dynamic-import targets — `import("")` calls +//! whose specifier is a string literal — are fetched and executed eagerly as +//! ordinary graph dependencies so the runtime `import()` resolves the module +//! namespace from the populated registry (see [`we_js::dynamic_import`]). +//! Runtime-computed specifiers are not preloaded and reject at call time. use crate::loader::{Resource, ResourceLoader, ResourceRequestType}; use std::collections::HashMap; @@ -136,6 +142,26 @@ fn build_graph( } } + // Eagerly fetch and execute statically-discoverable dynamic-import + // targets so a later `import("")` resolves from the populated + // module registry. These are sequenced as ordinary dependencies (they + // run before the importing module) but, unlike static imports, are not + // added to `resolve`: dynamic import reads the registry at call time + // rather than binding names at link time. + for specifier in module::dynamic_import_specifiers(&program) { + if let Some(dep_url) = resolve_specifier(&specifier, &base) { + let dep_key = dep_url.serialize(); + if !deps.contains(&dep_key) { + deps.push(dep_key.clone()); + } + if !graph.contains_key(&dep_key) { + if let Some(dep_source) = fetch_module_source(loader, &dep_url, origin) { + pending.push((dep_key, dep_url, dep_source)); + } + } + } + } + graph.insert( key, ModuleRecord { diff --git a/crates/e2e/pages/44_dynamic_import.html b/crates/e2e/pages/44_dynamic_import.html new file mode 100644 index 0000000..29a6a75 --- /dev/null +++ b/crates/e2e/pages/44_dynamic_import.html @@ -0,0 +1,28 @@ + + + + Dynamic import() (isu issue 304) + + +
pending
+
sum-pending
+ + + + + diff --git a/crates/e2e/scenarios/dynamic_import.we b/crates/e2e/scenarios/dynamic_import.we new file mode 100644 index 0000000..b2902fb --- /dev/null +++ b/crates/e2e/scenarios/dynamic_import.we @@ -0,0 +1,18 @@ +# Regression scenario for isu issue 304: dynamic `import()`. +# A `