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 turbopack_ecmascript::{CustomTransformer, TransformContext};
6
7use super::{is_client_module, server_to_client_proxy::create_proxy_module};
8
9#[derive(Debug)]
10pub struct ClientDirectiveTransformer {
11 transition_name: RcStr,
12}
13
14impl ClientDirectiveTransformer {
15 pub fn new(transition_name: RcStr) -> Self {
16 Self { transition_name }
17 }
18}
19
20#[async_trait]
21impl CustomTransformer for ClientDirectiveTransformer {
22 #[tracing::instrument(level = tracing::Level::TRACE, name = "client_directive", skip_all)]
23 async fn transform(&self, program: &mut Program, ctx: &TransformContext<'_>) -> Result<()> {
24 if is_client_module(program) {
25 *program = create_proxy_module(
26 self.transition_name.as_str(),
27 &format!("./{}", ctx.file_name_str),
28 );
29 program.visit_mut_with(&mut resolver(
30 ctx.unresolved_mark,
31 ctx.top_level_mark,
32 false,
33 ));
34 }
35
36 Ok(())
37 }
38}