turbopack_ecmascript_plugins/transform/directives/
client_disallowed.rs1use anyhow::Result;
2use async_trait::async_trait;
3use swc_core::ecma::{ast::Program, transforms::base::resolver, visit::VisitMutWith};
4use turbopack_ecmascript::{CustomTransformer, TransformContext};
5
6use super::{is_client_module, server_to_client_proxy::create_error_proxy_module};
7
8#[derive(Debug)]
9pub struct ClientDisallowedDirectiveTransformer {
10 error_proxy_module: String,
11}
12
13impl ClientDisallowedDirectiveTransformer {
14 pub fn new(error_proxy_module: String) -> Self {
15 Self { error_proxy_module }
16 }
17}
18
19#[async_trait]
20impl CustomTransformer for ClientDisallowedDirectiveTransformer {
21 async fn transform(&self, program: &mut Program, ctx: &TransformContext<'_>) -> Result<()> {
22 if is_client_module(program) {
23 *program = create_error_proxy_module(&self.error_proxy_module);
24 program.visit_mut_with(&mut resolver(
25 ctx.unresolved_mark,
26 ctx.top_level_mark,
27 false,
28 ));
29 }
30
31 Ok(())
32 }
33}