turbopack_ecmascript_plugins/transform/directives/
client.rs1use anyhow::Result;
2use async_trait::async_trait;
3use swc_core::ecma::{ast::Program, transforms::base::resolver, visit::VisitMutWith};
4use turbo_rcstr::RcStr;
5use turbo_tasks::ResolvedVc;
6use turbopack_ecmascript::{CustomTransformer, TransformContext};
7
8use super::{is_client_module, server_to_client_proxy::create_proxy_module};
9
10#[derive(Debug)]
11pub struct ClientDirectiveTransformer {
12 transition_name: ResolvedVc<RcStr>,
13}
14
15impl ClientDirectiveTransformer {
16 pub fn new(transition_name: ResolvedVc<RcStr>) -> Self {
17 Self { transition_name }
18 }
19}
20
21#[async_trait]
22impl CustomTransformer for ClientDirectiveTransformer {
23 #[tracing::instrument(level = tracing::Level::TRACE, name = "client_directive", skip_all)]
24 async fn transform(&self, program: &mut Program, ctx: &TransformContext<'_>) -> Result<()> {
25 if is_client_module(program) {
26 let transition_name = &*self.transition_name.await?;
27 *program = create_proxy_module(transition_name, &format!("./{}", ctx.file_name_str));
28 program.visit_mut_with(&mut resolver(
29 ctx.unresolved_mark,
30 ctx.top_level_mark,
31 false,
32 ));
33 }
34
35 Ok(())
36 }
37}