From 33c4d45328166d103e99a8c243727eead626080c Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Wed, 24 Aug 2022 19:36:05 +0200 Subject: fix: resolve `jsxImportSource` relative to module (#15561) Previously `jsxImportSource` was resolved relative to the config file during graph building, and relative to the emitted module during runtime. This is now fixed so that the JSX import source is resolved relative to the module both during graph building and at runtime. --- cli/args/config_file.rs | 31 +++++++++++++++---------------- cli/args/mod.rs | 9 ++++++--- 2 files changed, 21 insertions(+), 19 deletions(-) (limited to 'cli/args') diff --git a/cli/args/config_file.rs b/cli/args/config_file.rs index 1739309ff..ab6e57cb1 100644 --- a/cli/args/config_file.rs +++ b/cli/args/config_file.rs @@ -11,7 +11,6 @@ use crate::fs_util::specifier_to_file_path; use deno_core::anyhow::anyhow; use deno_core::anyhow::bail; use deno_core::anyhow::Context; -use deno_core::error::custom_error; use deno_core::error::AnyError; use deno_core::serde::Deserialize; use deno_core::serde::Serialize; @@ -30,6 +29,11 @@ use std::path::PathBuf; pub type MaybeImportsResult = Result)>>, AnyError>; +pub struct JsxImportSourceConfig { + pub default_specifier: Option, + pub module: String, +} + /// The transpile options that are significant out of a user provided tsconfig /// file, that we want to deserialize out of the final config for a transpile. #[derive(Debug, Deserialize)] @@ -680,17 +684,6 @@ impl ConfigFile { if let Some(types) = compiler_options.types { imports.extend(types); } - if compiler_options.jsx == Some("react-jsx".to_string()) { - imports.push(format!( - "{}/jsx-runtime", - compiler_options.jsx_import_source.ok_or_else(|| custom_error("TypeError", "Compiler option 'jsx' set to 'react-jsx', but no 'jsxImportSource' defined."))? - )); - } else if compiler_options.jsx == Some("react-jsxdev".to_string()) { - imports.push(format!( - "{}/jsx-dev-runtime", - compiler_options.jsx_import_source.ok_or_else(|| custom_error("TypeError", "Compiler option 'jsx' set to 'react-jsxdev', but no 'jsxImportSource' defined."))? - )); - } if !imports.is_empty() { let referrer = self.specifier.clone(); Ok(Some(vec![(referrer, imports)])) @@ -700,16 +693,22 @@ impl ConfigFile { } /// Based on the compiler options in the configuration file, return the - /// implied JSX import source module. - pub fn to_maybe_jsx_import_source_module(&self) -> Option { + /// JSX import source configuration. + pub fn to_maybe_jsx_import_source_config( + &self, + ) -> Option { let compiler_options_value = self.json.compiler_options.as_ref()?; let compiler_options: CompilerOptions = serde_json::from_value(compiler_options_value.clone()).ok()?; - match compiler_options.jsx.as_deref() { + let module = match compiler_options.jsx.as_deref() { Some("react-jsx") => Some("jsx-runtime".to_string()), Some("react-jsxdev") => Some("jsx-dev-runtime".to_string()), _ => None, - } + }; + module.map(|module| JsxImportSourceConfig { + default_specifier: compiler_options.jsx_import_source, + module, + }) } pub fn to_fmt_config(&self) -> Result, AnyError> { diff --git a/cli/args/mod.rs b/cli/args/mod.rs index 85e44aca2..90fa5220b 100644 --- a/cli/args/mod.rs +++ b/cli/args/mod.rs @@ -34,6 +34,7 @@ use std::env; use std::net::SocketAddr; use std::path::PathBuf; +use crate::args::config_file::JsxImportSourceConfig; use crate::compat; use crate::deno_dir::DenoDir; use crate::emit::get_ts_config_for_emit; @@ -210,12 +211,14 @@ impl CliOptions { } } - /// Return the implied JSX import source module. - pub fn to_maybe_jsx_import_source_module(&self) -> Option { + /// Return the JSX import source configuration. + pub fn to_maybe_jsx_import_source_config( + &self, + ) -> Option { self .maybe_config_file .as_ref() - .and_then(|c| c.to_maybe_jsx_import_source_module()) + .and_then(|c| c.to_maybe_jsx_import_source_config()) } /// Return any imports that should be brought into the scope of the module -- cgit v1.2.3