/
env.rs
169 lines (149 loc) · 4.96 KB
/
env.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use anyhow::Result;
use indexmap::indexmap;
use turbo_tasks::Vc;
use turbopack_binding::{
turbo::tasks_env::{CustomProcessEnv, EnvMap, FilterProcessEnv, ProcessEnv},
turbopack::env::EmbeddableProcessEnv,
};
use crate::next_config::NextConfig;
/// Creates a Vc<Box<dyn ProcessEnv>> safe to use in JS, by stringifying and
/// encoding as regular JS strings. Setting `client` to true will additionally
/// filter the env to just the keys that are acceptable for the client to
/// access.
///
/// For now, it also injects overridden values as if they were real JS code, eg
/// an Object and not a String.
#[turbo_tasks::function]
pub async fn env_for_js(
env: Vc<Box<dyn ProcessEnv>>,
client: bool,
next_config: Vc<NextConfig>,
) -> Result<Vc<Box<dyn ProcessEnv>>> {
let test_mode = env.read("__NEXT_TEST_MODE".to_string()).await?;
let test_mode = test_mode.as_deref().unwrap_or("");
let env = if client {
Vc::upcast(FilterProcessEnv::new(
env,
vec![
"NEXT_PUBLIC_".to_string(),
"NODE_ENV".to_string(),
"PORT".to_string(),
],
))
} else {
// Server doesn't need to have env vars injected since it will have them in the
// real process.env.
Vc::upcast(EnvMap::empty())
};
let env = Vc::upcast(EmbeddableProcessEnv::new(Vc::upcast(
CustomProcessEnv::new(env, next_config.env()),
)));
let image_config = next_config.image_config().await?;
let mut map = indexmap! {
// We need to overload the __NEXT_IMAGE_OPTS to override the default remotePatterns field.
// This allows us to support loading from remote hostnames until we properly support reading
// the next.config.js file.
"__NEXT_IMAGE_OPTS".to_string() => serde_json::to_string(&image_config)?,
};
let next_config = next_config.await?;
if next_config.react_strict_mode.unwrap_or(false) {
map.insert("__NEXT_STRICT_MODE".to_string(), "true".to_string());
}
if next_config.react_strict_mode.unwrap_or(true) {
map.insert("__NEXT_STRICT_MODE_APP".to_string(), "true".to_string());
}
map.insert(
"__NEXT_STRICT_NEXT_HEAD".to_string(),
if next_config.experimental.strict_next_head.unwrap_or(false) {
"true".to_string()
} else {
"false".to_string()
},
);
map.insert(
"__NEXT_TRAILING_SLASH".to_string(),
if next_config.trailing_slash.unwrap_or(false) {
"true".to_string()
} else {
"false".to_string()
},
);
map.insert(
"__NEXT_ROUTER_BASEPATH".to_string(),
// Don't stringify undefined
if let Some(base_path) = next_config.base_path.as_ref() {
serde_json::to_string(base_path)?
} else {
"undefined".to_string()
},
);
map.insert(
"__NEXT_ASSET_PREFIX".to_string(),
// Don't stringify undefined
if let Some(asset_prefix) = next_config.asset_prefix.as_ref() {
serde_json::to_string(asset_prefix)?
} else {
"undefined".to_string()
},
);
map.insert(
"__NEXT_MANUAL_TRAILING_SLASH".to_string(),
if next_config.skip_trailing_slash_redirect.unwrap_or(false) {
"true".to_string()
} else {
"false".to_string()
},
);
map.insert(
"__NEXT_NO_MIDDLEWARE_URL_NORMALIZE".to_string(),
if next_config.skip_middleware_url_normalize.unwrap_or(false) {
"true".to_string()
} else {
"false".to_string()
},
);
map.insert(
"__NEXT_EXTERNAL_MIDDLEWARE_REWRITE_RESOLVE".to_string(),
if next_config
.experimental
.external_middleware_rewrites_resolve
.unwrap_or(false)
{
"true".to_string()
} else {
"false".to_string()
},
);
map.insert(
"__NEXT_SCROLL_RESTORATION".to_string(),
if next_config.experimental.scroll_restoration.unwrap_or(false) {
"true".to_string()
} else {
"false".to_string()
},
);
map.insert(
"__NEXT_I18N_SUPPORT".to_string(),
// How do I check if i18n exists in the config?
if next_config.i18n.is_none() {
"false".to_string()
} else {
"true".to_string()
},
);
map.insert(
"__NEXT_I18N_DOMAINS".to_string(),
// Don't stringify undefined
match next_config.i18n.as_ref() {
Some(i18n) => match i18n.domains.as_ref() {
Some(domains) => serde_json::to_string(domains)?,
None => "undefined".to_string(),
},
None => "undefined".to_string(),
},
);
if !test_mode.is_empty() {
map.insert("__NEXT_TEST_MODE".to_string(), "true".to_string());
}
Ok(Vc::upcast(CustomProcessEnv::new(env, Vc::cell(map))))
}