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
21 changed files
with
352 additions
and
23 deletions.
There are no files selected for viewing
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
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()) | ||
} | ||
} |
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
17 changes: 17 additions & 0 deletions
17
crates/turbopack-tests/tests/snapshot/dynamic-request/very-dynamic/input/index.js
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,17 @@ | ||
import child_process from "node:child_process"; | ||
import fs, { readFileSync } from "node:fs"; | ||
|
||
const unknown = Math.random(); | ||
|
||
child_process.spawnSync(unknown); | ||
child_process.spawnSync("node", unknown); | ||
child_process.spawnSync("node", [unknown, unknown]); | ||
|
||
require(unknown); | ||
|
||
import(unknown); | ||
|
||
fs.readFileSync(unknown); | ||
readFileSync(unknown); | ||
|
||
new URL(unknown, import.meta.url); |
13 changes: 13 additions & 0 deletions
13
...request/very-dynamic/issues/__l___lint TP1001__ import(FreeVar(Math)[__quo__ra-fcce98.txt
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,13 @@ | ||
warning - [analysis] [project]/crates/turbopack-tests/tests/snapshot/dynamic-request/very-dynamic/input/index.js /crates/turbopack-tests/tests/snapshot/dynamic-request/very-dynamic/input/index.js:12:0 lint TP1001 import(FreeVar(Math)["random"]()) is very dynamic | ||
|
||
8 | child_process.spawnSync("node", [unknown, unknown]); | ||
9 | | ||
10 | require(unknown); | ||
11 | | ||
+ v-------------v | ||
12 + import(unknown); | ||
+ ^-------------^ | ||
13 | | ||
14 | fs.readFileSync(unknown); | ||
15 | readFileSync(unknown); | ||
16 | |
13 changes: 13 additions & 0 deletions
13
...request/very-dynamic/issues/__l___lint TP1002__ require(FreeVar(Math)[__quo__r-fa7bb4.txt
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,13 @@ | ||
warning - [analysis] [project]/crates/turbopack-tests/tests/snapshot/dynamic-request/very-dynamic/input/index.js /crates/turbopack-tests/tests/snapshot/dynamic-request/very-dynamic/input/index.js:10:0 lint TP1002 require(FreeVar(Math)["random"]()) is very dynamic | ||
|
||
6 | child_process.spawnSync(unknown); | ||
7 | child_process.spawnSync("node", unknown); | ||
8 | child_process.spawnSync("node", [unknown, unknown]); | ||
9 | | ||
+ v--------------v | ||
10 + require(unknown); | ||
+ ^--------------^ | ||
11 | | ||
12 | import(unknown); | ||
13 | | ||
14 | fs.readFileSync(unknown); |
12 changes: 12 additions & 0 deletions
12
...request/very-dynamic/issues/__l___lint TP1004__ fs.readFileSync(FreeVar(Math)[-45dc7a.txt
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,12 @@ | ||
warning - [analysis] [project]/crates/turbopack-tests/tests/snapshot/dynamic-request/very-dynamic/input/index.js /crates/turbopack-tests/tests/snapshot/dynamic-request/very-dynamic/input/index.js:15:0 lint TP1004 fs.readFileSync(FreeVar(Math)["random"]()) is very dynamic | ||
|
||
11 | | ||
12 | import(unknown); | ||
13 | | ||
14 | fs.readFileSync(unknown); | ||
+ v-------------------v | ||
15 + readFileSync(unknown); | ||
+ ^-------------------^ | ||
16 | | ||
17 | new URL(unknown, import.meta.url); | ||
18 | |
Oops, something went wrong.