Skip to content

Commit 6285723

Browse files
pass defineEnv to rust directly
1 parent 55a638b commit 6285723

File tree

17 files changed

+495
-384
lines changed

17 files changed

+495
-384
lines changed

packages/next-swc/crates/napi/src/next_api/project.rs

Lines changed: 90 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use napi::{
77
JsFunction, Status,
88
};
99
use next_api::{
10-
project::{Middleware, ProjectContainer, ProjectOptions},
10+
project::{DefineEnv, Middleware, PartialProjectOptions, ProjectContainer, ProjectOptions},
1111
route::{Endpoint, Route},
1212
};
1313
use next_core::tracing_presets::{
@@ -44,6 +44,7 @@ use super::{
4444
use crate::register;
4545

4646
#[napi(object)]
47+
#[derive(Clone, Debug)]
4748
pub struct NapiEnvVar {
4849
pub name: String,
4950
pub value: String,
@@ -74,10 +75,56 @@ pub struct NapiProjectOptions {
7475
/// A map of environment variables to use when compiling code.
7576
pub env: Vec<NapiEnvVar>,
7677

78+
/// A map of environment variables which should get injected at compile
79+
/// time.
80+
pub define_env: NapiDefineEnv,
81+
7782
/// The address of the dev server.
7883
pub server_addr: String,
7984
}
8085

86+
/// [NapiProjectOptions] with all fields optional.
87+
#[napi(object)]
88+
pub struct NapiPartialProjectOptions {
89+
/// A root path from which all files must be nested under. Trying to access
90+
/// a file outside this root will fail. Think of this as a chroot.
91+
pub root_path: Option<String>,
92+
93+
/// A path inside the root_path which contains the app/pages directories.
94+
pub project_path: Option<String>,
95+
96+
/// next.config's distDir. Project initialization occurs eariler than
97+
/// deserializing next.config, so passing it as separate option.
98+
pub dist_dir: Option<Option<String>>,
99+
100+
/// Whether to watch he filesystem for file changes.
101+
pub watch: Option<bool>,
102+
103+
/// The contents of next.config.js, serialized to JSON.
104+
pub next_config: Option<String>,
105+
106+
/// The contents of ts/config read by load-jsconfig, serialized to JSON.
107+
pub js_config: Option<String>,
108+
109+
/// A map of environment variables to use when compiling code.
110+
pub env: Option<Vec<NapiEnvVar>>,
111+
112+
/// A map of environment variables which should get injected at compile
113+
/// time.
114+
pub define_env: Option<NapiDefineEnv>,
115+
116+
/// The address of the dev server.
117+
pub server_addr: Option<String>,
118+
}
119+
120+
#[napi(object)]
121+
#[derive(Clone, Debug)]
122+
pub struct NapiDefineEnv {
123+
pub client: Vec<NapiEnvVar>,
124+
pub edge: Vec<NapiEnvVar>,
125+
pub nodejs: Vec<NapiEnvVar>,
126+
}
127+
81128
#[napi(object)]
82129
pub struct NapiTurboEngineOptions {
83130
/// An upper bound of memory that turbopack will attempt to stay under.
@@ -95,13 +142,53 @@ impl From<NapiProjectOptions> for ProjectOptions {
95142
env: val
96143
.env
97144
.into_iter()
98-
.map(|NapiEnvVar { name, value }| (name, value))
145+
.map(|var| (var.name, var.value))
99146
.collect(),
147+
define_env: val.define_env.into(),
148+
server_addr: val.server_addr,
149+
}
150+
}
151+
}
152+
153+
impl From<NapiPartialProjectOptions> for PartialProjectOptions {
154+
fn from(val: NapiPartialProjectOptions) -> Self {
155+
PartialProjectOptions {
156+
root_path: val.root_path,
157+
project_path: val.project_path,
158+
watch: val.watch,
159+
next_config: val.next_config,
160+
js_config: val.js_config,
161+
env: val
162+
.env
163+
.map(|env| env.into_iter().map(|var| (var.name, var.value)).collect()),
164+
define_env: val.define_env.map(|env| env.into()),
100165
server_addr: val.server_addr,
101166
}
102167
}
103168
}
104169

170+
impl From<NapiDefineEnv> for DefineEnv {
171+
fn from(val: NapiDefineEnv) -> Self {
172+
DefineEnv {
173+
client: val
174+
.client
175+
.into_iter()
176+
.map(|var| (var.name, var.value))
177+
.collect(),
178+
edge: val
179+
.edge
180+
.into_iter()
181+
.map(|var| (var.name, var.value))
182+
.collect(),
183+
nodejs: val
184+
.nodejs
185+
.into_iter()
186+
.map(|var| (var.name, var.value))
187+
.collect(),
188+
}
189+
}
190+
}
191+
105192
pub struct ProjectInstance {
106193
turbo_tasks: Arc<TurboTasks<MemoryBackend>>,
107194
container: Vc<ProjectContainer>,
@@ -190,7 +277,7 @@ pub async fn project_new(
190277
#[napi(ts_return_type = "{ __napiType: \"Project\" }")]
191278
pub async fn project_update(
192279
#[napi(ts_arg_type = "{ __napiType: \"Project\" }")] project: External<ProjectInstance>,
193-
options: NapiProjectOptions,
280+
options: NapiPartialProjectOptions,
194281
) -> napi::Result<()> {
195282
let turbo_tasks = project.turbo_tasks.clone();
196283
let options = options.into();

packages/next-swc/crates/napi/src/turbopack.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ use std::{
66
use anyhow::Context;
77
use napi::bindgen_prelude::*;
88
use next_build::{
9-
build as turbo_next_build, build_options::BuildContext, BuildOptions as NextBuildOptions,
9+
build as turbo_next_build,
10+
build_options::{BuildContext, DefineEnv},
11+
BuildOptions as NextBuildOptions,
1012
};
1113
use next_core::next_config::{Rewrite, Rewrites, RouteHas};
1214

13-
use crate::util::MapErr;
15+
use crate::{next_api::project::NapiDefineEnv, util::MapErr};
1416

1517
#[napi(object, object_to_js = false)]
1618
#[derive(Debug)]
@@ -38,6 +40,7 @@ pub struct NextBuildContext {
3840
// TODO(alexkirsz) These are used to generate route types.
3941
// pub original_rewrites: Option<Rewrites>,
4042
// pub original_redirects: Option<Vec<Redirect>>,
43+
pub define_env: NapiDefineEnv,
4144
}
4245

4346
impl TryFrom<NextBuildContext> for NextBuildOptions {
@@ -62,10 +65,33 @@ impl TryFrom<NextBuildContext> for NextBuildOptions {
6265
.context("NextBuildContext must provide rewrites")?
6366
.into(),
6467
}),
68+
define_env: value.define_env.into(),
6569
})
6670
}
6771
}
6872

73+
impl From<NapiDefineEnv> for DefineEnv {
74+
fn from(val: NapiDefineEnv) -> Self {
75+
DefineEnv {
76+
client: val
77+
.client
78+
.into_iter()
79+
.map(|var| (var.name, var.value))
80+
.collect(),
81+
edge: val
82+
.edge
83+
.into_iter()
84+
.map(|var| (var.name, var.value))
85+
.collect(),
86+
nodejs: val
87+
.nodejs
88+
.into_iter()
89+
.map(|var| (var.name, var.value))
90+
.collect(),
91+
}
92+
}
93+
}
94+
6995
/// Keep in sync with [`next_core::next_config::Rewrites`]
7096
#[napi(object, object_to_js = false)]
7197
#[derive(Debug)]

0 commit comments

Comments
 (0)