-
Notifications
You must be signed in to change notification settings - Fork 27.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add client disallowed transform (vercel/turborepo#8336)
### Description needed for next.js to disallow "use client" on middleware and instrumentation ### Testing Instructions <!-- Give a quick description of steps to test your changes. -->
- Loading branch information
Showing
3 changed files
with
61 additions
and
2 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
crates/turbopack-ecmascript-plugins/src/transform/directives/client_disallowed.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use anyhow::Result; | ||
use async_trait::async_trait; | ||
use swc_core::ecma::{ast::Program, transforms::base::resolver, visit::VisitMutWith}; | ||
use turbopack_ecmascript::{CustomTransformer, TransformContext}; | ||
|
||
use super::{is_client_module, server_to_client_proxy::create_error_proxy_module}; | ||
|
||
#[derive(Debug)] | ||
pub struct ClientDisallowedDirectiveTransformer { | ||
error_proxy_module: String, | ||
} | ||
|
||
impl ClientDisallowedDirectiveTransformer { | ||
pub fn new(error_proxy_module: String) -> Self { | ||
Self { error_proxy_module } | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl CustomTransformer for ClientDisallowedDirectiveTransformer { | ||
async fn transform(&self, program: &mut Program, ctx: &TransformContext<'_>) -> Result<()> { | ||
if is_client_module(program) { | ||
*program = create_error_proxy_module(&self.error_proxy_module); | ||
program.visit_mut_with(&mut resolver( | ||
ctx.unresolved_mark, | ||
ctx.top_level_mark, | ||
false, | ||
)); | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
crates/turbopack-ecmascript-plugins/src/transform/directives/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters