summaryrefslogtreecommitdiff
path: root/cli/emit.rs
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2021-12-15 19:22:36 +0100
committerGitHub <noreply@github.com>2021-12-15 19:22:36 +0100
commita1f0796fccfafee19b2fe06155efe746da2e9654 (patch)
tree01942b1bc202352418c88dbf8a1c447e72f6f976 /cli/emit.rs
parentec7d90666f68ae0bf6036a6915296622adc9b65c (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/emit.rs')
-rw-r--r--cli/emit.rs24
1 files changed, 15 insertions, 9 deletions
diff --git a/cli/emit.rs b/cli/emit.rs
index c9a90abb7..3aa2c4794 100644
--- a/cli/emit.rs
+++ b/cli/emit.rs
@@ -153,6 +153,7 @@ pub(crate) fn get_ts_config(
"isolatedModules": true,
"lib": lib,
"module": "esnext",
+ "resolveJsonModule": true,
"strict": true,
"target": "esnext",
"tsBuildInfoFile": "deno:///.tsbuildinfo",
@@ -186,6 +187,7 @@ pub(crate) fn get_ts_config(
"jsx": "react",
"jsxFactory": "React.createElement",
"jsxFragmentFactory": "React.Fragment",
+ "resolveJsonModule": true,
})),
ConfigType::RuntimeEmit { tsc_emit } => {
let mut ts_config = TsConfig::new(json!({
@@ -403,14 +405,15 @@ pub(crate) fn check_and_maybe_emit(
log::debug!("module missing, skipping emit for {}", specifier);
continue;
};
- // Sometimes if `tsc` sees a CommonJS file it will _helpfully_ output it
- // to ESM, which we don't really want to do unless someone has enabled
- // check_js.
- if !check_js
- && matches!(
- media_type,
- MediaType::JavaScript | MediaType::Cjs | MediaType::Mjs
- )
+ // Sometimes if `tsc` sees a CommonJS file or a JSON module, it will
+ // _helpfully_ output it, which we don't really want to do unless
+ // someone has enabled check_js.
+ if matches!(media_type, MediaType::Json)
+ || (!check_js
+ && matches!(
+ media_type,
+ MediaType::JavaScript | MediaType::Cjs | MediaType::Mjs
+ ))
{
log::debug!("skipping emit for {}", specifier);
continue;
@@ -429,7 +432,10 @@ pub(crate) fn check_and_maybe_emit(
MediaType::Dts | MediaType::Dcts | MediaType::Dmts => {
cache.set(CacheType::Declaration, &specifier, emit.data)?;
}
- _ => unreachable!(),
+ _ => unreachable!(
+ "unexpected media_type {} {}",
+ emit.media_type, specifier
+ ),
}
}
}