Repository: vercel/next.js
File: turbopack/crates/turbopack-ecmascript/src/lib.rs (Rust)

Code before:

        Ok(Vc::cell(false))
    }

    #[turbo_tasks::function]
    async fn merge(
        self: Vc<Self>,
        modules: Vc<MergeableModulesExposed>,
        entry_points: Vc<MergeableModules>,
    ) -> Result<Vc<Box<dyn ChunkableModule>>> {
        Ok(Vc::upcast(
            *MergedEcmascriptModule::new(
                modules,
                entry_points,
                self.options().to_resolved().await?,
            )
            .await?,
        ))
    }
}

#[turbo_tasks::value_impl]
impl EvaluatableAsset for EcmascriptModuleAsset {}

#[turbo_tasks::value_impl]
impl ResolveOrigin for EcmascriptModuleAsset {
    #[turbo_tasks::function]
    fn origin_path(&self) -> Vc<FileSystemPath> {
        self.source.ident().path()
    }

    #[turbo_tasks::function]
    fn asset_context(&self) -> Vc<Box<dyn AssetContext>> {
        *self.asset_context
    }

    #[turbo_tasks::function]
    async fn get_inner_asset(&self, request: Vc<Request>) -> Result<Vc<OptionModule>> {
        Ok(Vc::cell(if let Some(inner_assets) = &self.inner_assets {
            if let Some(request) = request.await?.request() {
                inner_assets.await?.get(&request).copied()
            } else {
                None
            }
        } else {
            None
        }))
    }
}

#[turbo_tasks::value]

Review comment (refactor):
I think you can also split this method into `get_local_if_split()` and `get_facade_if_split()`. Currently most variants of `ModulePart` are unused and return something weird here: e.g. calling it with `ModulePart::RenamedExport` will return the facade. We never call it that way, so it doesn't break anything, but I think it could be cleaner. We always call this method with a const ModulePart anyway.

Code after:
        let should_split = *self.get_exports().split_locals_and_reexports().await?;
        Ok(if should_split {
            Vc::upcast(EcmascriptModuleLocalsModule::new(self))
        } else {
            Vc::upcast(self)
        })
    }

    /// Returns the facade module if the module should be split (has re-exports), otherwise self.
    ///
    /// The facade module re-exports all exports from the original module, including
    /// both local bindings (via the locals module) and re-exports from other modules.
    #[turbo_tasks::function]
    pub async fn get_facade_if_split(
        self: Vc<Self>,
    ) -> Result<Vc<Box<dyn EcmascriptChunkPlaceable>>> {
        let should_split = *self.get_exports().split_locals_and_reexports().await?;
        Ok(if should_split {
            Vc::upcast(EcmascriptModuleFacadeModule::new(
                Vc::upcast(self),
                ModulePart::Facade,
            ))
        } else {
            Vc::upcast(self)
        })
    }
}

#[turbo_tasks::value_impl]
impl MergeableModule for EcmascriptModuleAsset {
    #[turbo_tasks::function]
    async fn is_mergeable(self: ResolvedVc<Self>) -> Result<Vc<bool>> {
        if matches!(
            &*self.get_exports().await?,
            EcmascriptExports::EsmExports(_)
        ) {
            return Ok(Vc::cell(true));
        }

        Ok(Vc::cell(false))
    }

    #[turbo_tasks::function]
    async fn merge(
        self: Vc<Self>,
        modules: Vc<MergeableModulesExposed>,
        entry_points: Vc<MergeableModules>,
    ) -> Result<Vc<Box<dyn ChunkableModule>>> {
        Ok(Vc::upcast(
            *MergedEcmascriptModule::new(
                modules,