diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2021-12-15 19:22:36 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-15 19:22:36 +0100 |
commit | a1f0796fccfafee19b2fe06155efe746da2e9654 (patch) | |
tree | 01942b1bc202352418c88dbf8a1c447e72f6f976 /cli/proc_state.rs | |
parent | ec7d90666f68ae0bf6036a6915296622adc9b65c (diff) |
feat: Add support for import assertions and JSON modules (#12866)
This commit adds proper support for import assertions and JSON modules.
Implementation of "core/modules.rs" was changed to account for multiple possible
module types, instead of always assuming that the code is an "ES module". In
effect "ModuleMap" now has knowledge about each modules' type (stored via
"ModuleType" enum). Module loading pipeline now stores information about
expected module type for each request and validates that expected type matches
discovered module type based on file's "MediaType".
Relevant tests were added to "core/modules.rs" and integration tests,
additionally multiple WPT tests were enabled.
There are still some rough edges in the implementation and not all WPT were
enabled, due to:
a) unclear BOM handling in source code by "FileFetcher"
b) design limitation of Deno's "FileFetcher" that doesn't download the same
module multiple times in a single run
Co-authored-by: Kitson Kelly <me@kitsonkelly.com>
Diffstat (limited to 'cli/proc_state.rs')
-rw-r--r-- | cli/proc_state.rs | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/cli/proc_state.rs b/cli/proc_state.rs index 1a0f61e71..d82f8d017 100644 --- a/cli/proc_state.rs +++ b/cli/proc_state.rs @@ -31,6 +31,7 @@ use deno_core::url::Url; use deno_core::CompiledWasmModuleStore; use deno_core::ModuleSource; use deno_core::ModuleSpecifier; +use deno_core::ModuleType; use deno_core::SharedArrayBufferStore; use deno_graph::create_graph; use deno_graph::Dependency; @@ -68,6 +69,7 @@ pub struct ProcState(Arc<Inner>); enum ModuleEntry { Module { code: String, + media_type: MediaType, dependencies: BTreeMap<String, Dependency>, }, Error(ModuleGraphError), @@ -584,6 +586,7 @@ impl ProcState { | MediaType::Unknown | MediaType::Cjs | MediaType::Mjs + | MediaType::Json ) { module.maybe_source().unwrap_or("").to_string() // The emit may also be missing when a declaration file is in the @@ -602,7 +605,11 @@ impl ProcState { module.maybe_dependencies().cloned().unwrap_or_default(); graph_data.modules.insert( specifier.clone(), - ModuleEntry::Module { code, dependencies }, + ModuleEntry::Module { + code, + dependencies, + media_type: *media_type, + }, ); if let Some(dependencies) = module.maybe_dependencies() { for dep in dependencies.values() { @@ -724,10 +731,16 @@ impl ProcState { _ => &specifier, }; match graph_data.modules.get(found_specifier) { - Some(ModuleEntry::Module { code, .. }) => Ok(ModuleSource { + Some(ModuleEntry::Module { + code, media_type, .. + }) => Ok(ModuleSource { code: code.clone(), module_url_specified: specifier.to_string(), module_url_found: found_specifier.to_string(), + module_type: match media_type { + MediaType::Json => ModuleType::Json, + _ => ModuleType::JavaScript, + }, }), _ => Err(anyhow!( "Loading unprepared module: {}", |