Repository: denoland/deno
Pull request: perf: skip jsx transpile when disabled

Follow-up to https://github.com/denoland/deno/pull/29994

Changed files:
filename: libs/resolver/emit.rs
patch: @@ -125,11 +125,22 @@ impl<TInNpmPackageChecker: InNpmPackageChecker, TSys: EmitterSys>
     media_type: MediaType,
     module_kind: ModuleKind,
     source: &ArcStr,
-  ) -> Result<String, EmitParsedSourceHelperError> {
+  ) -> Result<ArcStr, EmitParsedSourceHelperError> {
+    if !media_type.is_emittable() {
+      return Ok(source.clone());
+    }
     let transpile_and_emit_options = self
       .compiler_options_resolver
       .for_specifier(specifier)
       .transpile_options()?;
+    let transpile_options = &transpile_and_emit_options.transpile;
+    if matches!(media_type, MediaType::Jsx)
+      && !transpile_options.transform_jsx
+      && !transpile_options.precompile_jsx
+    {
+      // jsx disabled, so skip
+      return Ok(source.clone());
+    }
     // Note: keep this in sync with the sync version below
     let helper = EmitParsedSourceHelper(self);
     match helper.pre_emit_parsed_source(
@@ -138,12 +149,11 @@ impl<TInNpmPackageChecker: InNpmPackageChecker, TSys: EmitterSys>
       transpile_and_emit_options,
       source,
     ) {
-      PreEmitResult::Cached(emitted_text) => Ok(emitted_text),
+      PreEmitResult::Cached(emitted_text) => Ok(emitted_text.into()),
       PreEmitResult::NotCached { source_hash } => {
-        let parsed_source_cache = self.parsed_source_cache.clone();
-        let transpile_and_emit_options = transpile_and_emit_options.clone();
-        #[cfg(feature = "sync")]
-        let transpiled_source = crate::rt::spawn_blocking({
+        let emit = {
+          let parsed_source_cache = self.parsed_source_cache.clone();
+          let transpile_and_emit_options = transpile_and_emit_options.clone();
           let specifier = specifier.clone();
           let source = source.clone();
           move || {
@@ -158,26 +168,18 @@ impl<TInNpmPackageChecker: InNpmPackageChecker, TSys: EmitterSys>
             )
             .map(|r| r.text)
           }
-        })
-        .await
-        .unwrap()?;
+        };
+        #[cfg(feature = "sync")]
+        let transpiled_source =
+          crate::rt::spawn_blocking(emit).await.unwrap()?;
         #[cfg(not(feature = "sync"))]
-        let transpiled_source = transpile(
-          &parsed_source_cache,
-          &specifier,
-          media_type,
-          module_kind,
-          source.clone(),
-          &transpile_and_emit_options.transpile,
-          &transpile_and_emit_options.emit,
-        )?
-        .text;
+        let transpiled_source = emit()?;
         helper.post_emit_parsed_source(
           specifier,
           &transpiled_source,
           source_hash,
         );
-        Ok(transpiled_source)
+        Ok(transpiled_source.into())
       }
     }
   }

filename: libs/resolver/loader/prepared.rs
patch: @@ -185,7 +185,7 @@ impl<TInNpmPackageChecker: InNpmPackageChecker, TSys: PreparedModuleLoaderSys>
 
         Ok(Some(PreparedModuleOrAsset::Module(PreparedModule {
           // note: it's faster to provide a string to v8 if we know it's a string
-          source: PreparedModuleSource::ArcStr(transpile_result.into()),
+          source: PreparedModuleSource::ArcStr(transpile_result),
           specifier,
           media_type,
         })))
@@ -413,31 +413,29 @@ impl<TInNpmPackageChecker: InNpmPackageChecker, TSys: PreparedModuleLoaderSys>
     original_source: &ArcStr,
   ) -> Result<ArcStr, LoadMaybeCjsError> {
     let js_source = if media_type.is_emittable() {
-      Cow::Owned(
-        self
-          .emitter
-          .emit_parsed_source(
-            specifier,
-            media_type,
-            ModuleKind::Cjs,
-            original_source,
-          )
-          .await?,
-      )
+      self
+        .emitter
+        .emit_parsed_source(
+          specifier,
+          media_type,
+          ModuleKind::Cjs,
+          original_source,
+        )
+        .await?
     } else {
-      Cow::Borrowed(original_source.as_ref())
+      original_source.clone()
     };
     let text = self
       .node_code_translator
-      .translate_cjs_to_esm(specifier, Some(js_source))
+      .translate_cjs_to_esm(specifier, Some(Cow::Borrowed(js_source.as_ref())))
       .await?;
     // at this point, we no longer need the parsed source in memory, so free it
     self.parsed_source_cache.free(specifier);
     Ok(match text {
       // perf: if the text is borrowed, that means it didn't make any changes
       // to the original source, so we can just provide that instead of cloning
       // the borrowed text
-      Cow::Borrowed(_) => original_source.clone(),
+      Cow::Borrowed(_) => js_source.clone(),
       Cow::Owned(text) => text.into(),
     })
   }