-
Notifications
You must be signed in to change notification settings - Fork 26.1k
/
context.rs
115 lines (107 loc) · 3.79 KB
/
context.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use anyhow::Result;
use turbo_tasks::Value;
use turbopack_binding::{
turbo::tasks_fs::FileSystemPathVc,
turbopack::{
core::{
compile_time_defines,
compile_time_info::{
CompileTimeDefines, CompileTimeDefinesVc, CompileTimeInfo, CompileTimeInfoVc,
FreeVarReference, FreeVarReferencesVc,
},
environment::{
EdgeWorkerEnvironment, EnvironmentIntention, EnvironmentVc, ExecutionEnvironment,
ServerAddrVc,
},
free_var_references,
},
node::execution_context::ExecutionContextVc,
turbopack::resolve_options_context::{ResolveOptionsContext, ResolveOptionsContextVc},
},
};
use crate::{
next_config::NextConfigVc, next_import_map::get_next_edge_import_map,
next_server::context::ServerContextType,
next_shared::resolve::UnsupportedModulesResolvePluginVc, util::foreign_code_context_condition,
};
fn defines() -> CompileTimeDefines {
compile_time_defines!(
process.turbopack = true,
process.env.NODE_ENV = "development",
process.env.__NEXT_CLIENT_ROUTER_FILTER_ENABLED = false,
process.env.NEXT_RUNTIME = "edge"
)
// TODO(WEB-937) there are more defines needed, see
// packages/next/src/build/webpack-config.ts
}
#[turbo_tasks::function]
fn next_edge_defines() -> CompileTimeDefinesVc {
defines().cell()
}
#[turbo_tasks::function]
fn next_edge_free_vars(project_path: FileSystemPathVc) -> FreeVarReferencesVc {
free_var_references!(
..defines().into_iter(),
Buffer = FreeVarReference::EcmaScriptModule {
request: "next/dist/compiled/buffer".to_string(),
context: Some(project_path),
export: Some("Buffer".to_string()),
},
process = FreeVarReference::EcmaScriptModule {
request: "next/dist/build/polyfills/process".to_string(),
context: Some(project_path),
export: Some("default".to_string()),
},
)
.cell()
}
#[turbo_tasks::function]
pub fn get_edge_compile_time_info(
project_path: FileSystemPathVc,
server_addr: ServerAddrVc,
intention: Value<EnvironmentIntention>,
) -> CompileTimeInfoVc {
CompileTimeInfo::builder(EnvironmentVc::new(
Value::new(ExecutionEnvironment::EdgeWorker(
EdgeWorkerEnvironment { server_addr }.into(),
)),
intention,
))
.defines(next_edge_defines())
.free_var_references(next_edge_free_vars(project_path))
.cell()
}
#[turbo_tasks::function]
pub async fn get_edge_resolve_options_context(
project_path: FileSystemPathVc,
ty: Value<ServerContextType>,
next_config: NextConfigVc,
execution_context: ExecutionContextVc,
) -> Result<ResolveOptionsContextVc> {
let next_edge_import_map =
get_next_edge_import_map(project_path, ty, next_config, execution_context);
let resolve_options_context = ResolveOptionsContext {
enable_node_modules: Some(project_path.root().resolve().await?),
// https://github.com/vercel/next.js/blob/bf52c254973d99fed9d71507a2e818af80b8ade7/packages/next/src/build/webpack-config.ts#L96-L102
custom_conditions: vec![
"edge-light".to_string(),
"worker".to_string(),
"development".to_string(),
],
import_map: Some(next_edge_import_map),
module: true,
browser: true,
plugins: vec![UnsupportedModulesResolvePluginVc::new(project_path).into()],
..Default::default()
};
Ok(ResolveOptionsContext {
enable_typescript: true,
enable_react: true,
rules: vec![(
foreign_code_context_condition(next_config).await?,
resolve_options_context.clone().cell(),
)],
..resolve_options_context
}
.cell())
}