Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
very dynamic requests will only lead to a warning
they no longer lead to an additional module not found error
- Loading branch information
Showing
17 changed files
with
409 additions
and
113 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
crates/turbopack-ecmascript/src/references/dynamic_expression.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,71 @@ | ||
use anyhow::Result; | ||
use serde::{Deserialize, Serialize}; | ||
use swc_core::quote; | ||
use turbo_tasks::{debug::ValueDebugFormat, trace::TraceRawVcs, Vc}; | ||
|
||
use super::AstPath; | ||
use crate::{ | ||
chunk::EcmascriptChunkingContext, | ||
code_gen::{CodeGenerateable, CodeGeneration}, | ||
create_visitor, | ||
}; | ||
|
||
#[derive(PartialEq, Eq, TraceRawVcs, Serialize, Deserialize, ValueDebugFormat)] | ||
enum DynamicExpressionType { | ||
Promise, | ||
Normal, | ||
} | ||
|
||
#[turbo_tasks::value] | ||
pub struct DynamicExpression { | ||
path: Vc<AstPath>, | ||
ty: DynamicExpressionType, | ||
} | ||
|
||
#[turbo_tasks::value_impl] | ||
impl DynamicExpression { | ||
#[turbo_tasks::function] | ||
pub fn new(path: Vc<AstPath>) -> Vc<Self> { | ||
Self::cell(DynamicExpression { | ||
path, | ||
ty: DynamicExpressionType::Normal, | ||
}) | ||
} | ||
|
||
#[turbo_tasks::function] | ||
pub fn new_promise(path: Vc<AstPath>) -> Vc<Self> { | ||
Self::cell(DynamicExpression { | ||
path, | ||
ty: DynamicExpressionType::Promise, | ||
}) | ||
} | ||
} | ||
|
||
#[turbo_tasks::value_impl] | ||
impl CodeGenerateable for DynamicExpression { | ||
#[turbo_tasks::function] | ||
async fn code_generation( | ||
&self, | ||
_context: Vc<Box<dyn EcmascriptChunkingContext>>, | ||
) -> Result<Vc<CodeGeneration>> { | ||
let path = &self.path.await?; | ||
|
||
let visitor = match self.ty { | ||
DynamicExpressionType::Normal => { | ||
create_visitor!(path, visit_mut_expr(expr: &mut Expr) { | ||
*expr = quote!("(() => { const e = new Error(\"Cannot find module as expression is too dynamic\"); e.code = 'MODULE_NOT_FOUND'; throw e; })()" as Expr); | ||
}) | ||
} | ||
DynamicExpressionType::Promise => { | ||
create_visitor!(path, visit_mut_expr(expr: &mut Expr) { | ||
*expr = quote!("Promise.resolve().then(() => { const e = new Error(\"Cannot find module as expression is too dynamic\"); e.code = 'MODULE_NOT_FOUND'; throw e; })" as Expr); | ||
}) | ||
} | ||
}; | ||
|
||
Ok(CodeGeneration { | ||
visitors: vec![visitor], | ||
} | ||
.cell()) | ||
} | ||
} |
Oops, something went wrong.