Repository: denoland/deno
File: libs/resolver/loader/module_loader.rs (Rust)

Code before:

          // at this point, we no longer need the parsed source in memory, so free it
          self.parsed_source_cache.free(specifier);

          Ok(Some(CodeOrDeferredEmit::Source(LoadedModule {
            source: LoadedModuleSource::ArcStr(code),
            specifier: Cow::Borrowed(specifier),
            media_type: *media_type,
          })))
        }
      },
      Some(deno_graph::Module::Wasm(WasmModule {
        source, specifier, ..
      })) => Ok(Some(CodeOrDeferredEmit::Source(LoadedModule {
        source: LoadedModuleSource::ArcBytes(source.clone()),
        specifier: Cow::Borrowed(specifier),
        media_type: MediaType::Wasm,
      }))),
      Some(deno_graph::Module::External(module))
        if matches!(
          requested_module_type,
          RequestedModuleType::Bytes | RequestedModuleType::Text
        ) =>
      {
        Ok(Some(CodeOrDeferredEmit::ExternalAsset {
          specifier: &module.specifier,
        }))
      }
      Some(
        deno_graph::Module::External(_)
        | deno_graph::Module::Node(_)
        | deno_graph::Module::Npm(_),
      )
      | None => Ok(None),
    }
  }

  async fn load_maybe_cjs(
    &self,
    specifier: &Url,
    media_type: MediaType,
    original_source: &ArcStr,
  ) -> Result<ArcStr, LoadMaybeCjsError> {
    let js_source = self
      .emitter
      .maybe_emit_source(
        specifier,
        media_type,
        ModuleKind::Cjs,
        original_source,
      )

Review comment (suggestion):
This is iffy, but this code needs to return `Ok(None)` for byonm files and pass through raw and source-only imports.

Code after:

          // at this point, we no longer need the parsed source in memory, so free it
          self.parsed_source_cache.free(specifier);

          Ok(Some(CodeOrDeferredEmit::Source(LoadedModule {
            source: LoadedModuleSource::ArcStr(code),
            specifier: Cow::Borrowed(specifier),
            media_type: *media_type,
          })))
        }
      },
      Some(deno_graph::Module::Wasm(WasmModule {
        source, specifier, ..
      })) => Ok(Some(CodeOrDeferredEmit::Source(LoadedModule {
        source: LoadedModuleSource::ArcBytes(source.clone()),
        specifier: Cow::Borrowed(specifier),
        media_type: MediaType::Wasm,
      }))),
      Some(deno_graph::Module::External(module)) => {
        if module.specifier.as_str().contains("/node_modules/") {
          return Ok(None);
        }
        Ok(Some(CodeOrDeferredEmit::ExternalAsset {
          specifier: &module.specifier,
        }))
      }
      Some(deno_graph::Module::Node(_) | deno_graph::Module::Npm(_)) | None => {
        Ok(None)
      }
    }
  }

  async fn load_maybe_cjs(
    &self,
    specifier: &Url,
    media_type: MediaType,
    original_source: &ArcStr,
  ) -> Result<ArcStr, LoadMaybeCjsError> {
    let js_source = self
      .emitter
      .maybe_emit_source(
        specifier,
        media_type,
        ModuleKind::Cjs,
        original_source,
      )
      .await?;
    let text = self
      .node_code_translator
      .translate_cjs_to_esm(specifier, Some(Cow::Borrowed(js_source.as_ref())))
      .await?;