/
context.rs
154 lines (142 loc) · 5.04 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use anyhow::Result;
use turbo_tasks::{Value, Vc};
use turbopack_binding::{
turbo::tasks_fs::FileSystemPath,
turbopack::{
core::{
compile_time_defines,
compile_time_info::{
CompileTimeDefines, CompileTimeInfo, FreeVarReference, FreeVarReferences,
},
environment::{EdgeWorkerEnvironment, Environment, ExecutionEnvironment, ServerAddr},
free_var_references,
},
dev::DevChunkingContext,
ecmascript::chunk::EcmascriptChunkingContext,
node::{debug::should_debug, execution_context::ExecutionContext},
turbopack::resolve_options_context::ResolveOptionsContext,
},
};
use crate::{
mode::NextMode,
next_client::context::get_client_assets_path,
next_config::NextConfig,
next_import_map::get_next_edge_import_map,
next_server::context::ServerContextType,
next_shared::resolve::{
ModuleFeatureReportResolvePlugin, NextSharedRuntimeResolvePlugin,
UnsupportedModulesResolvePlugin,
},
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() -> Vc<CompileTimeDefines> {
defines().cell()
}
#[turbo_tasks::function]
fn next_edge_free_vars(project_path: Vc<FileSystemPath>) -> Vc<FreeVarReferences> {
free_var_references!(
..defines().into_iter(),
Buffer = FreeVarReference::EcmaScriptModule {
request: "next/dist/compiled/buffer".to_string(),
lookup_path: Some(project_path),
export: Some("Buffer".to_string()),
},
process = FreeVarReference::EcmaScriptModule {
request: "next/dist/build/polyfills/process".to_string(),
lookup_path: Some(project_path),
export: Some("default".to_string()),
},
)
.cell()
}
#[turbo_tasks::function]
pub fn get_edge_compile_time_info(
project_path: Vc<FileSystemPath>,
server_addr: Vc<ServerAddr>,
) -> Vc<CompileTimeInfo> {
CompileTimeInfo::builder(Environment::new(Value::new(
ExecutionEnvironment::EdgeWorker(EdgeWorkerEnvironment { server_addr }.into()),
)))
.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: Vc<FileSystemPath>,
ty: Value<ServerContextType>,
mode: NextMode,
next_config: Vc<NextConfig>,
execution_context: Vc<ExecutionContext>,
) -> Result<Vc<ResolveOptionsContext>> {
let next_edge_import_map =
get_next_edge_import_map(project_path, ty, mode, next_config, execution_context);
let ty = ty.into_value();
// https://github.com/vercel/next.js/blob/bf52c254973d99fed9d71507a2e818af80b8ade7/packages/next/src/build/webpack-config.ts#L96-L102
let mut custom_conditions = vec![
mode.node_env().to_string(),
"edge-light".to_string(),
"worker".to_string(),
];
match ty {
ServerContextType::AppRSC { .. } => custom_conditions.push("react-server".to_string()),
ServerContextType::AppRoute { .. }
| ServerContextType::Pages { .. }
| ServerContextType::PagesData { .. }
| ServerContextType::AppSSR { .. }
| ServerContextType::Middleware { .. } => {}
};
let resolve_options_context = ResolveOptionsContext {
enable_node_modules: Some(project_path.root().resolve().await?),
custom_conditions,
import_map: Some(next_edge_import_map),
module: true,
browser: true,
plugins: vec![
Vc::upcast(ModuleFeatureReportResolvePlugin::new(project_path)),
Vc::upcast(UnsupportedModulesResolvePlugin::new(project_path)),
Vc::upcast(NextSharedRuntimeResolvePlugin::new(project_path)),
],
..Default::default()
};
Ok(ResolveOptionsContext {
enable_typescript: true,
enable_react: true,
rules: vec![(
foreign_code_context_condition(next_config, project_path).await?,
resolve_options_context.clone().cell(),
)],
..resolve_options_context
}
.cell())
}
#[turbo_tasks::function]
pub fn get_edge_chunking_context(
project_path: Vc<FileSystemPath>,
node_root: Vc<FileSystemPath>,
client_root: Vc<FileSystemPath>,
environment: Vc<Environment>,
) -> Vc<Box<dyn EcmascriptChunkingContext>> {
Vc::upcast(
DevChunkingContext::builder(
project_path,
node_root.join("server/edge".to_string()),
node_root.join("server/edge/chunks".to_string()),
get_client_assets_path(client_root),
environment,
)
.reference_chunk_source_maps(should_debug("edge"))
.build(),
)
}