Skip to content

Commit

Permalink
very dynamic requests will only lead to a warning
Browse files Browse the repository at this point in the history
they no longer lead to an additional module not found error
  • Loading branch information
sokra committed Mar 6, 2024
1 parent ce591fd commit 1702a06
Show file tree
Hide file tree
Showing 17 changed files with 409 additions and 113 deletions.
71 changes: 71 additions & 0 deletions crates/turbopack-ecmascript/src/references/dynamic_expression.rs
@@ -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())
}
}

0 comments on commit 1702a06

Please sign in to comment.