Unverified Commit 515784c2 authored by Leah's avatar Leah Committed by GitHub

chore: pass defineEnv from next.js to rust directly (#56216)

### What?

Deduplicates our env var injection between the JS and rust side

Closes WEB-937

---------
Co-authored-by: 's avatarTim Neutkens <tim@timneutkens.nl>
Co-authored-by: 's avatarZack Tanner <zacktanner@gmail.com>
parent f77f222d
...@@ -3378,7 +3378,6 @@ dependencies = [ ...@@ -3378,7 +3378,6 @@ dependencies = [
"anyhow", "anyhow",
"async-recursion", "async-recursion",
"base64 0.21.4", "base64 0.21.4",
"clap 4.4.2",
"console-subscriber", "console-subscriber",
"dunce", "dunce",
"indexmap 1.9.3", "indexmap 1.9.3",
......
...@@ -7,7 +7,7 @@ use napi::{ ...@@ -7,7 +7,7 @@ use napi::{
JsFunction, Status, JsFunction, Status,
}; };
use next_api::{ use next_api::{
project::{Middleware, ProjectContainer, ProjectOptions}, project::{DefineEnv, Middleware, PartialProjectOptions, ProjectContainer, ProjectOptions},
route::{Endpoint, Route}, route::{Endpoint, Route},
}; };
use next_core::tracing_presets::{ use next_core::tracing_presets::{
...@@ -44,6 +44,7 @@ use super::{ ...@@ -44,6 +44,7 @@ use super::{
use crate::register; use crate::register;
#[napi(object)] #[napi(object)]
#[derive(Clone, Debug)]
pub struct NapiEnvVar { pub struct NapiEnvVar {
pub name: String, pub name: String,
pub value: String, pub value: String,
...@@ -74,10 +75,56 @@ pub struct NapiProjectOptions { ...@@ -74,10 +75,56 @@ pub struct NapiProjectOptions {
/// A map of environment variables to use when compiling code. /// A map of environment variables to use when compiling code.
pub env: Vec<NapiEnvVar>, pub env: Vec<NapiEnvVar>,
/// A map of environment variables which should get injected at compile
/// time.
pub define_env: NapiDefineEnv,
/// The address of the dev server. /// The address of the dev server.
pub server_addr: String, pub server_addr: String,
} }
/// [NapiProjectOptions] with all fields optional.
#[napi(object)]
pub struct NapiPartialProjectOptions {
/// A root path from which all files must be nested under. Trying to access
/// a file outside this root will fail. Think of this as a chroot.
pub root_path: Option<String>,
/// A path inside the root_path which contains the app/pages directories.
pub project_path: Option<String>,
/// next.config's distDir. Project initialization occurs eariler than
/// deserializing next.config, so passing it as separate option.
pub dist_dir: Option<Option<String>>,
/// Whether to watch he filesystem for file changes.
pub watch: Option<bool>,
/// The contents of next.config.js, serialized to JSON.
pub next_config: Option<String>,
/// The contents of ts/config read by load-jsconfig, serialized to JSON.
pub js_config: Option<String>,
/// A map of environment variables to use when compiling code.
pub env: Option<Vec<NapiEnvVar>>,
/// A map of environment variables which should get injected at compile
/// time.
pub define_env: Option<NapiDefineEnv>,
/// The address of the dev server.
pub server_addr: Option<String>,
}
#[napi(object)]
#[derive(Clone, Debug)]
pub struct NapiDefineEnv {
pub client: Vec<NapiEnvVar>,
pub edge: Vec<NapiEnvVar>,
pub nodejs: Vec<NapiEnvVar>,
}
#[napi(object)] #[napi(object)]
pub struct NapiTurboEngineOptions { pub struct NapiTurboEngineOptions {
/// An upper bound of memory that turbopack will attempt to stay under. /// An upper bound of memory that turbopack will attempt to stay under.
...@@ -95,13 +142,53 @@ impl From<NapiProjectOptions> for ProjectOptions { ...@@ -95,13 +142,53 @@ impl From<NapiProjectOptions> for ProjectOptions {
env: val env: val
.env .env
.into_iter() .into_iter()
.map(|NapiEnvVar { name, value }| (name, value)) .map(|var| (var.name, var.value))
.collect(), .collect(),
define_env: val.define_env.into(),
server_addr: val.server_addr,
}
}
}
impl From<NapiPartialProjectOptions> for PartialProjectOptions {
fn from(val: NapiPartialProjectOptions) -> Self {
PartialProjectOptions {
root_path: val.root_path,
project_path: val.project_path,
watch: val.watch,
next_config: val.next_config,
js_config: val.js_config,
env: val
.env
.map(|env| env.into_iter().map(|var| (var.name, var.value)).collect()),
define_env: val.define_env.map(|env| env.into()),
server_addr: val.server_addr, server_addr: val.server_addr,
} }
} }
} }
impl From<NapiDefineEnv> for DefineEnv {
fn from(val: NapiDefineEnv) -> Self {
DefineEnv {
client: val
.client
.into_iter()
.map(|var| (var.name, var.value))
.collect(),
edge: val
.edge
.into_iter()
.map(|var| (var.name, var.value))
.collect(),
nodejs: val
.nodejs
.into_iter()
.map(|var| (var.name, var.value))
.collect(),
}
}
}
pub struct ProjectInstance { pub struct ProjectInstance {
turbo_tasks: Arc<TurboTasks<MemoryBackend>>, turbo_tasks: Arc<TurboTasks<MemoryBackend>>,
container: Vc<ProjectContainer>, container: Vc<ProjectContainer>,
...@@ -190,7 +277,7 @@ pub async fn project_new( ...@@ -190,7 +277,7 @@ pub async fn project_new(
#[napi(ts_return_type = "{ __napiType: \"Project\" }")] #[napi(ts_return_type = "{ __napiType: \"Project\" }")]
pub async fn project_update( pub async fn project_update(
#[napi(ts_arg_type = "{ __napiType: \"Project\" }")] project: External<ProjectInstance>, #[napi(ts_arg_type = "{ __napiType: \"Project\" }")] project: External<ProjectInstance>,
options: NapiProjectOptions, options: NapiPartialProjectOptions,
) -> napi::Result<()> { ) -> napi::Result<()> {
let turbo_tasks = project.turbo_tasks.clone(); let turbo_tasks = project.turbo_tasks.clone();
let options = options.into(); let options = options.into();
......
...@@ -6,11 +6,13 @@ use std::{ ...@@ -6,11 +6,13 @@ use std::{
use anyhow::Context; use anyhow::Context;
use napi::bindgen_prelude::*; use napi::bindgen_prelude::*;
use next_build::{ use next_build::{
build as turbo_next_build, build_options::BuildContext, BuildOptions as NextBuildOptions, build as turbo_next_build,
build_options::{BuildContext, DefineEnv},
BuildOptions as NextBuildOptions,
}; };
use next_core::next_config::{Rewrite, Rewrites, RouteHas}; use next_core::next_config::{Rewrite, Rewrites, RouteHas};
use crate::util::MapErr; use crate::{next_api::project::NapiDefineEnv, util::MapErr};
#[napi(object, object_to_js = false)] #[napi(object, object_to_js = false)]
#[derive(Debug)] #[derive(Debug)]
...@@ -38,6 +40,7 @@ pub struct NextBuildContext { ...@@ -38,6 +40,7 @@ pub struct NextBuildContext {
// TODO(alexkirsz) These are used to generate route types. // TODO(alexkirsz) These are used to generate route types.
// pub original_rewrites: Option<Rewrites>, // pub original_rewrites: Option<Rewrites>,
// pub original_redirects: Option<Vec<Redirect>>, // pub original_redirects: Option<Vec<Redirect>>,
pub define_env: NapiDefineEnv,
} }
impl TryFrom<NextBuildContext> for NextBuildOptions { impl TryFrom<NextBuildContext> for NextBuildOptions {
...@@ -62,10 +65,33 @@ impl TryFrom<NextBuildContext> for NextBuildOptions { ...@@ -62,10 +65,33 @@ impl TryFrom<NextBuildContext> for NextBuildOptions {
.context("NextBuildContext must provide rewrites")? .context("NextBuildContext must provide rewrites")?
.into(), .into(),
}), }),
define_env: value.define_env.into(),
}) })
} }
} }
impl From<NapiDefineEnv> for DefineEnv {
fn from(val: NapiDefineEnv) -> Self {
DefineEnv {
client: val
.client
.into_iter()
.map(|var| (var.name, var.value))
.collect(),
edge: val
.edge
.into_iter()
.map(|var| (var.name, var.value))
.collect(),
nodejs: val
.nodejs
.into_iter()
.map(|var| (var.name, var.value))
.collect(),
}
}
}
/// Keep in sync with [`next_core::next_config::Rewrites`] /// Keep in sync with [`next_core::next_config::Rewrites`]
#[napi(object, object_to_js = false)] #[napi(object, object_to_js = false)]
#[derive(Debug)] #[derive(Debug)]
......
...@@ -295,13 +295,8 @@ impl AppProject { ...@@ -295,13 +295,8 @@ impl AppProject {
async fn runtime_entries(self: Vc<Self>) -> Result<Vc<RuntimeEntries>> { async fn runtime_entries(self: Vc<Self>) -> Result<Vc<RuntimeEntries>> {
let this = self.await?; let this = self.await?;
Ok(get_server_runtime_entries( Ok(get_server_runtime_entries(
self.project().project_path(),
// TODO(alexkirsz) Should we pass env here or EnvMap::empty, as is done in
// app_source?
self.project().env(),
Value::new(self.rsc_ty()), Value::new(self.rsc_ty()),
this.mode, this.mode,
self.project().next_config(),
)) ))
} }
...@@ -330,7 +325,6 @@ impl AppProject { ...@@ -330,7 +325,6 @@ impl AppProject {
let this = self.await?; let this = self.await?;
Ok(get_client_runtime_entries( Ok(get_client_runtime_entries(
self.project().project_path(), self.project().project_path(),
self.client_env(),
Value::new(self.client_ty()), Value::new(self.client_ty()),
this.mode, this.mode,
self.project().next_config(), self.project().next_config(),
......
...@@ -68,11 +68,8 @@ impl MiddlewareEndpoint { ...@@ -68,11 +68,8 @@ impl MiddlewareEndpoint {
); );
let mut evaluatable_assets = get_server_runtime_entries( let mut evaluatable_assets = get_server_runtime_entries(
self.project.project_path(),
self.project.env(),
Value::new(ServerContextType::Middleware), Value::new(ServerContextType::Middleware),
NextMode::Development, NextMode::Development,
self.project.next_config(),
) )
.resolve_entries(self.context) .resolve_entries(self.context)
.await? .await?
......
...@@ -406,7 +406,6 @@ impl PagesProject { ...@@ -406,7 +406,6 @@ impl PagesProject {
let this = self.await?; let this = self.await?;
let client_runtime_entries = get_client_runtime_entries( let client_runtime_entries = get_client_runtime_entries(
self.project().project_path(), self.project().project_path(),
self.project().env(),
Value::new(ClientContextType::Pages { Value::new(ClientContextType::Pages {
pages_dir: self.pages_dir(), pages_dir: self.pages_dir(),
}), }),
...@@ -421,13 +420,10 @@ impl PagesProject { ...@@ -421,13 +420,10 @@ impl PagesProject {
async fn runtime_entries(self: Vc<Self>) -> Result<Vc<RuntimeEntries>> { async fn runtime_entries(self: Vc<Self>) -> Result<Vc<RuntimeEntries>> {
let this = self.await?; let this = self.await?;
Ok(get_server_runtime_entries( Ok(get_server_runtime_entries(
self.project().project_path(),
self.project().env(),
Value::new(ServerContextType::Pages { Value::new(ServerContextType::Pages {
pages_dir: self.pages_dir(), pages_dir: self.pages_dir(),
}), }),
this.mode, this.mode,
self.project().next_config(),
)) ))
} }
...@@ -435,13 +431,10 @@ impl PagesProject { ...@@ -435,13 +431,10 @@ impl PagesProject {
async fn data_runtime_entries(self: Vc<Self>) -> Result<Vc<RuntimeEntries>> { async fn data_runtime_entries(self: Vc<Self>) -> Result<Vc<RuntimeEntries>> {
let this = self.await?; let this = self.await?;
Ok(get_server_runtime_entries( Ok(get_server_runtime_entries(
self.project().project_path(),
self.project().env(),
Value::new(ServerContextType::PagesData { Value::new(ServerContextType::PagesData {
pages_dir: self.pages_dir(), pages_dir: self.pages_dir(),
}), }),
this.mode, this.mode,
self.project().next_config(),
)) ))
} }
......
...@@ -83,6 +83,10 @@ pub struct ProjectOptions { ...@@ -83,6 +83,10 @@ pub struct ProjectOptions {
/// A map of environment variables to use when compiling code. /// A map of environment variables to use when compiling code.
pub env: Vec<(String, String)>, pub env: Vec<(String, String)>,
/// A map of environment variables which should get injected at compile
/// time.
pub define_env: DefineEnv,
/// Whether to watch the filesystem for file changes. /// Whether to watch the filesystem for file changes.
pub watch: bool, pub watch: bool,
...@@ -90,6 +94,44 @@ pub struct ProjectOptions { ...@@ -90,6 +94,44 @@ pub struct ProjectOptions {
pub server_addr: String, pub server_addr: String,
} }
#[derive(Debug, Serialize, Deserialize, Clone, TaskInput, PartialEq, Eq, TraceRawVcs)]
#[serde(rename_all = "camelCase")]
pub struct PartialProjectOptions {
/// A root path from which all files must be nested under. Trying to access
/// a file outside this root will fail. Think of this as a chroot.
pub root_path: Option<String>,
/// A path inside the root_path which contains the app/pages directories.
pub project_path: Option<String>,
/// The contents of next.config.js, serialized to JSON.
pub next_config: Option<String>,
/// The contents of ts/config read by load-jsconfig, serialized to JSON.
pub js_config: Option<String>,
/// A map of environment variables to use when compiling code.
pub env: Option<Vec<(String, String)>>,
/// A map of environment variables which should get injected at compile
/// time.
pub define_env: Option<DefineEnv>,
/// Whether to watch the filesystem for file changes.
pub watch: Option<bool>,
/// The address of the dev server.
pub server_addr: Option<String>,
}
#[derive(Debug, Serialize, Deserialize, Clone, TaskInput, PartialEq, Eq, TraceRawVcs)]
#[serde(rename_all = "camelCase")]
pub struct DefineEnv {
pub client: Vec<(String, String)>,
pub edge: Vec<(String, String)>,
pub nodejs: Vec<(String, String)>,
}
#[derive(Serialize, Deserialize, TraceRawVcs, PartialEq, Eq, ValueDebugFormat)] #[derive(Serialize, Deserialize, TraceRawVcs, PartialEq, Eq, ValueDebugFormat)]
pub struct Middleware { pub struct Middleware {
pub endpoint: Vc<Box<dyn Endpoint>>, pub endpoint: Vc<Box<dyn Endpoint>>,
...@@ -113,18 +155,52 @@ impl ProjectContainer { ...@@ -113,18 +155,52 @@ impl ProjectContainer {
} }
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn update(self: Vc<Self>, options: ProjectOptions) -> Result<Vc<()>> { pub fn update(&self, options: PartialProjectOptions) -> Vc<()> {
self.await?.options_state.set(options); let mut new_options = self.options_state.get().clone();
Ok(Default::default())
if let Some(root_path) = options.root_path {
new_options.root_path = root_path;
}
if let Some(project_path) = options.project_path {
new_options.project_path = project_path;
}
if let Some(next_config) = options.next_config {
new_options.next_config = next_config;
}
if let Some(js_config) = options.js_config {
new_options.js_config = js_config;
}
if let Some(env) = options.env {
new_options.env = env;
}
if let Some(define_env) = options.define_env {
new_options.define_env = define_env;
}
if let Some(watch) = options.watch {
new_options.watch = watch;
}
if let Some(server_addr) = options.server_addr {
new_options.server_addr = server_addr;
}
self.options_state.set(new_options);
Default::default()
} }
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn project(self: Vc<Self>) -> Result<Vc<Project>> { pub async fn project(self: Vc<Self>) -> Result<Vc<Project>> {
let this = self.await?; let this = self.await?;
let (env, next_config, js_config, root_path, project_path, watch, server_addr) = { let (env, define_env, next_config, js_config, root_path, project_path, watch, server_addr) = {
let options = this.options_state.get(); let options = this.options_state.get();
let env: Vc<EnvMap> = Vc::cell(options.env.iter().cloned().collect()); let env: Vc<EnvMap> = Vc::cell(options.env.iter().cloned().collect());
let define_env: Vc<ProjectDefineEnv> = ProjectDefineEnv {
client: Vc::cell(options.define_env.client.iter().cloned().collect()),
edge: Vc::cell(options.define_env.edge.iter().cloned().collect()),
nodejs: Vc::cell(options.define_env.nodejs.iter().cloned().collect()),
}
.cell();
let next_config = NextConfig::from_string(Vc::cell(options.next_config.clone())); let next_config = NextConfig::from_string(Vc::cell(options.next_config.clone()));
let js_config = JsConfig::from_string(Vc::cell(options.js_config.clone())); let js_config = JsConfig::from_string(Vc::cell(options.js_config.clone()));
let root_path = options.root_path.clone(); let root_path = options.root_path.clone();
...@@ -133,6 +209,7 @@ impl ProjectContainer { ...@@ -133,6 +209,7 @@ impl ProjectContainer {
let server_addr = options.server_addr.parse()?; let server_addr = options.server_addr.parse()?;
( (
env, env,
define_env,
next_config, next_config,
js_config, js_config,
root_path, root_path,
...@@ -157,6 +234,7 @@ impl ProjectContainer { ...@@ -157,6 +234,7 @@ impl ProjectContainer {
js_config, js_config,
dist_dir, dist_dir,
env: Vc::upcast(env), env: Vc::upcast(env),
define_env,
browserslist_query: "last 1 Chrome versions, last 1 Firefox versions, last 1 Safari \ browserslist_query: "last 1 Chrome versions, last 1 Firefox versions, last 1 Safari \
versions, last 1 Edge versions" versions, last 1 Edge versions"
.to_string(), .to_string(),
...@@ -207,6 +285,10 @@ pub struct Project { ...@@ -207,6 +285,10 @@ pub struct Project {
/// A map of environment variables to use when compiling code. /// A map of environment variables to use when compiling code.
env: Vc<Box<dyn ProcessEnv>>, env: Vc<Box<dyn ProcessEnv>>,
/// A map of environment variables which should get injected at compile
/// time.
define_env: Vc<ProjectDefineEnv>,
browserslist_query: String, browserslist_query: String,
mode: NextMode, mode: NextMode,
...@@ -214,6 +296,31 @@ pub struct Project { ...@@ -214,6 +296,31 @@ pub struct Project {
versioned_content_map: Vc<VersionedContentMap>, versioned_content_map: Vc<VersionedContentMap>,
} }
#[turbo_tasks::value]
pub struct ProjectDefineEnv {
client: Vc<EnvMap>,
edge: Vc<EnvMap>,
nodejs: Vc<EnvMap>,
}
#[turbo_tasks::value_impl]
impl ProjectDefineEnv {
#[turbo_tasks::function]
pub fn client(&self) -> Vc<EnvMap> {
self.client
}
#[turbo_tasks::function]
pub fn edge(&self) -> Vc<EnvMap> {
self.edge
}
#[turbo_tasks::function]
pub fn nodejs(&self) -> Vc<EnvMap> {
self.nodejs
}
}
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
impl Project { impl Project {
#[turbo_tasks::function] #[turbo_tasks::function]
...@@ -353,15 +460,11 @@ impl Project { ...@@ -353,15 +460,11 @@ impl Project {
} }
#[turbo_tasks::function] #[turbo_tasks::function]
pub(super) async fn client_compile_time_info(self: Vc<Self>) -> Result<Vc<CompileTimeInfo>> { pub(super) async fn client_compile_time_info(&self) -> Result<Vc<CompileTimeInfo>> {
let this = self.await?;
Ok(get_client_compile_time_info( Ok(get_client_compile_time_info(
this.mode, self.mode,
this.browserslist_query.clone(), self.browserslist_query.clone(),
self.dist_root_string(), self.define_env.client(),
this.next_config,
find_app_dir(self.project_path()),
)) ))
} }
...@@ -372,16 +475,19 @@ impl Project { ...@@ -372,16 +475,19 @@ impl Project {
this.mode, this.mode,
self.env(), self.env(),
self.server_addr(), self.server_addr(),
this.define_env.nodejs(),
)) ))
} }
#[turbo_tasks::function] #[turbo_tasks::function]
pub(super) fn edge_compile_time_info(self: Vc<Self>) -> Vc<CompileTimeInfo> { pub(super) async fn edge_compile_time_info(self: Vc<Self>) -> Result<Vc<CompileTimeInfo>> {
get_edge_compile_time_info( let this = self.await?;
Ok(get_edge_compile_time_info(
this.mode,
self.project_path(), self.project_path(),
self.server_addr(), self.server_addr(),
self.dist_root_string(), this.define_env.nodejs(),
) ))
} }
#[turbo_tasks::function] #[turbo_tasks::function]
......
...@@ -6,12 +6,6 @@ license = "MPL-2.0" ...@@ -6,12 +6,6 @@ license = "MPL-2.0"
edition = "2021" edition = "2021"
autobenches = false autobenches = false
[[bin]]
name = "next-build"
path = "src/main.rs"
bench = false
required-features = ["cli"]
[lib] [lib]
bench = false bench = false
...@@ -21,8 +15,7 @@ bench = false ...@@ -21,8 +15,7 @@ bench = false
# `cargo xxx` without explicitly specifying features, not that we want to # `cargo xxx` without explicitly specifying features, not that we want to
# promote this as default backend. Actual configuration is done when building next-swc, # promote this as default backend. Actual configuration is done when building next-swc,
# and also turbopack standalone when we have it. # and also turbopack standalone when we have it.
default = ["cli", "custom_allocator", "native-tls"] default = ["custom_allocator", "native-tls"]
cli = ["clap"]
tokio_console = [ tokio_console = [
"dep:console-subscriber", "dep:console-subscriber",
"tokio/tracing", "tokio/tracing",
...@@ -40,7 +33,6 @@ profile = [] ...@@ -40,7 +33,6 @@ profile = []
[dependencies] [dependencies]
anyhow = { workspace = true } anyhow = { workspace = true }
async-recursion = { workspace = true } async-recursion = { workspace = true }
clap = { workspace = true, features = ["derive", "env"], optional = true }
console-subscriber = { workspace = true, optional = true } console-subscriber = { workspace = true, optional = true }
dunce = { workspace = true } dunce = { workspace = true }
next-core = { workspace = true } next-core = { workspace = true }
......
...@@ -30,6 +30,8 @@ pub struct BuildOptions { ...@@ -30,6 +30,8 @@ pub struct BuildOptions {
/// The Next.js build context. /// The Next.js build context.
pub build_context: Option<BuildContext>, pub build_context: Option<BuildContext>,
pub define_env: DefineEnv,
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
...@@ -40,3 +42,10 @@ pub struct BuildContext { ...@@ -40,3 +42,10 @@ pub struct BuildContext {
/// Next.js config rewrites. /// Next.js config rewrites.
pub rewrites: Rewrites, pub rewrites: Rewrites,
} }
#[derive(Debug, Clone)]
pub struct DefineEnv {
pub client: Vec<(String, String)>,
pub edge: Vec<(String, String)>,
pub nodejs: Vec<(String, String)>,
}
#![feature(arbitrary_self_types)]
#![feature(async_fn_in_trait)]
use std::path::PathBuf;
use anyhow::Result;
use clap::Parser;
use next_build::BuildOptions;
use turbopack_binding::turbopack::cli_utils::issue::IssueSeverityCliOption;
#[global_allocator]
static ALLOC: turbopack_binding::turbo::malloc::TurboMalloc =
turbopack_binding::turbo::malloc::TurboMalloc;
#[derive(Debug, Parser)]
#[clap(author, version, about, long_about = None)]
pub struct BuildCliArgs {
/// The directory of the Next.js application.
/// If no directory is provided, the current directory will be used.
#[clap(value_parser)]
pub dir: Option<PathBuf>,
/// The root directory of the project. Nothing outside of this directory can
/// be accessed. e. g. the monorepo root.
/// If no directory is provided, `dir` will be used.
#[clap(long, value_parser)]
pub root: Option<PathBuf>,
#[clap(long, value_parser)]
pub dist_dir: Option<String>,
/// Display version of the binary. Noop if used in library mode.
#[clap(long)]
pub display_version: bool,
/// Filter by issue severity.
#[clap(short, long)]
pub log_level: Option<IssueSeverityCliOption>,
/// Show all log messages without limit.
#[clap(long)]
pub show_all: bool,
/// Expand the log details.
#[clap(long)]
pub log_detail: bool,
/// Whether to enable full task stats recording in Turbo Engine.
#[clap(long)]
pub full_stats: bool,
/// Enable experimental garbage collection with the provided memory limit in
/// MB.
#[clap(long)]
pub memory_limit: Option<usize>,
}
fn main() {
use turbopack_binding::turbo::malloc::TurboMalloc;
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.on_thread_stop(|| {
TurboMalloc::thread_stop();
})
.build()
.unwrap()
.block_on(main_inner())
.unwrap()
}
async fn main_inner() -> Result<()> {
let args = BuildCliArgs::parse();
if args.display_version {
// Note: enabling git causes trouble with aarch64 linux builds with libz-sys
println!(
"Build Timestamp\t\t{:#?}",
option_env!("VERGEN_BUILD_TIMESTAMP").unwrap_or_else(|| "N/A")
);
println!(
"Build Version\t\t{:#?}",
option_env!("VERGEN_BUILD_SEMVER").unwrap_or_else(|| "N/A")
);
println!(
"Cargo Target Triple\t{:#?}",
option_env!("VERGEN_CARGO_TARGET_TRIPLE").unwrap_or_else(|| "N/A")
);
println!(
"Cargo Profile\t\t{:#?}",
option_env!("VERGEN_CARGO_PROFILE").unwrap_or_else(|| "N/A")
);
return Ok(());
}
next_build::build(BuildOptions {
dir: args.dir,
root: args.root,
memory_limit: args.memory_limit,
log_level: args.log_level.map(|l| l.0),
show_all: args.show_all,
log_detail: args.log_detail,
full_stats: args.full_stats,
build_context: None,
dist_dir: args.dist_dir,
})
.await
}
...@@ -23,10 +23,7 @@ use next_core::{ ...@@ -23,10 +23,7 @@ use next_core::{
}; };
use turbo_tasks::{TryJoinIterExt, Value, Vc}; use turbo_tasks::{TryJoinIterExt, Value, Vc};
use turbopack_binding::{ use turbopack_binding::{
turbo::{ turbo::tasks_fs::FileSystemPath,
tasks_env::{CustomProcessEnv, ProcessEnv},
tasks_fs::FileSystemPath,
},
turbopack::{ turbopack::{
build::BuildChunkingContext, build::BuildChunkingContext,
core::{ core::{
...@@ -56,7 +53,6 @@ pub struct AppEntries { ...@@ -56,7 +53,6 @@ pub struct AppEntries {
pub async fn get_app_entries( pub async fn get_app_entries(
project_root: Vc<FileSystemPath>, project_root: Vc<FileSystemPath>,
execution_context: Vc<ExecutionContext>, execution_context: Vc<ExecutionContext>,
env: Vc<Box<dyn ProcessEnv>>,
client_compile_time_info: Vc<CompileTimeInfo>, client_compile_time_info: Vc<CompileTimeInfo>,
server_compile_time_info: Vc<CompileTimeInfo>, server_compile_time_info: Vc<CompileTimeInfo>,
next_config: Vc<NextConfig>, next_config: Vc<NextConfig>,
...@@ -85,9 +81,7 @@ pub async fn get_app_entries( ...@@ -85,9 +81,7 @@ pub async fn get_app_entries(
// TODO(alexkirsz) Should we pass env here or EnvMap::empty, as is done in // TODO(alexkirsz) Should we pass env here or EnvMap::empty, as is done in
// app_source? // app_source?
let runtime_entries = get_server_runtime_entries(project_root, env, rsc_ty, mode, next_config); let runtime_entries = get_server_runtime_entries(rsc_ty, mode);
let env = Vc::upcast(CustomProcessEnv::new(env, next_config.env()));
let ssr_ty: Value<ServerContextType> = Value::new(ServerContextType::AppSSR { app_dir }); let ssr_ty: Value<ServerContextType> = Value::new(ServerContextType::AppSSR { app_dir });
...@@ -228,7 +222,6 @@ pub async fn get_app_entries( ...@@ -228,7 +222,6 @@ pub async fn get_app_entries(
let client_runtime_entries = get_client_runtime_entries( let client_runtime_entries = get_client_runtime_entries(
project_root, project_root,
env,
client_ty, client_ty,
mode, mode,
next_config, next_config,
......
...@@ -7,7 +7,6 @@ use std::{ ...@@ -7,7 +7,6 @@ use std::{
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use dunce::canonicalize; use dunce::canonicalize;
use next_core::{ use next_core::{
app_structure::find_app_dir_if_enabled,
mode::NextMode, mode::NextMode,
next_app::get_app_client_references_chunks, next_app::get_app_client_references_chunks,
next_client::{get_client_chunking_context, get_client_compile_time_info}, next_client::{get_client_chunking_context, get_client_compile_time_info},
...@@ -26,7 +25,7 @@ use next_core::{ ...@@ -26,7 +25,7 @@ use next_core::{
use serde::Serialize; use serde::Serialize;
use turbo_tasks::{ use turbo_tasks::{
graph::{AdjacencyMap, GraphTraversal}, graph::{AdjacencyMap, GraphTraversal},
Completion, Completions, TransientInstance, TryJoinIterExt, ValueToString, Vc, Completion, Completions, TransientInstance, TryJoinIterExt, Vc,
}; };
use turbopack_binding::{ use turbopack_binding::{
turbo::tasks_fs::{rebase, DiskFileSystem, FileContent, FileSystem, FileSystemPath}, turbo::tasks_fs::{rebase, DiskFileSystem, FileContent, FileSystem, FileSystemPath},
...@@ -133,15 +132,14 @@ pub(crate) async fn next_build(options: TransientInstance<BuildOptions>) -> Resu ...@@ -133,15 +132,14 @@ pub(crate) async fn next_build(options: TransientInstance<BuildOptions>) -> Resu
let next_config = load_next_config(execution_context.with_layer("next_config".to_string())); let next_config = load_next_config(execution_context.with_layer("next_config".to_string()));
let mode = NextMode::Build; let mode = NextMode::Build;
let app_dir = find_app_dir_if_enabled(project_root);
let client_compile_time_info = get_client_compile_time_info( let client_define_env = Vc::cell(options.define_env.client.iter().cloned().collect());
mode, let client_compile_time_info =
browserslist_query, get_client_compile_time_info(mode, browserslist_query, client_define_env);
node_root.to_string(),
next_config, let server_define_env = Vc::cell(options.define_env.nodejs.iter().cloned().collect());
app_dir, let server_compile_time_info =
); get_server_compile_time_info(mode, env, ServerAddr::empty(), server_define_env);
let server_compile_time_info = get_server_compile_time_info(mode, env, ServerAddr::empty());
// TODO(alexkirsz) Pages should build their own routes, outside of a FS. // TODO(alexkirsz) Pages should build their own routes, outside of a FS.
let next_router_fs = Vc::upcast::<Box<dyn FileSystem>>(VirtualFileSystem::new()); let next_router_fs = Vc::upcast::<Box<dyn FileSystem>>(VirtualFileSystem::new());
...@@ -150,7 +148,6 @@ pub(crate) async fn next_build(options: TransientInstance<BuildOptions>) -> Resu ...@@ -150,7 +148,6 @@ pub(crate) async fn next_build(options: TransientInstance<BuildOptions>) -> Resu
next_router_root, next_router_root,
project_root, project_root,
execution_context, execution_context,
env,
client_compile_time_info, client_compile_time_info,
server_compile_time_info, server_compile_time_info,
next_config, next_config,
...@@ -159,7 +156,6 @@ pub(crate) async fn next_build(options: TransientInstance<BuildOptions>) -> Resu ...@@ -159,7 +156,6 @@ pub(crate) async fn next_build(options: TransientInstance<BuildOptions>) -> Resu
let app_entries = get_app_entries( let app_entries = get_app_entries(
project_root, project_root,
execution_context, execution_context,
env,
client_compile_time_info, client_compile_time_info,
server_compile_time_info, server_compile_time_info,
next_config, next_config,
......
...@@ -23,7 +23,7 @@ use next_core::{ ...@@ -23,7 +23,7 @@ use next_core::{
}; };
use turbo_tasks::Vc; use turbo_tasks::Vc;
use turbopack_binding::{ use turbopack_binding::{
turbo::{tasks::Value, tasks_env::ProcessEnv, tasks_fs::FileSystemPath}, turbo::{tasks::Value, tasks_fs::FileSystemPath},
turbopack::{ turbopack::{
build::BuildChunkingContext, build::BuildChunkingContext,
core::{ core::{
...@@ -57,7 +57,6 @@ pub async fn get_page_entries( ...@@ -57,7 +57,6 @@ pub async fn get_page_entries(
next_router_root: Vc<FileSystemPath>, next_router_root: Vc<FileSystemPath>,
project_root: Vc<FileSystemPath>, project_root: Vc<FileSystemPath>,
execution_context: Vc<ExecutionContext>, execution_context: Vc<ExecutionContext>,
env: Vc<Box<dyn ProcessEnv>>,
client_compile_time_info: Vc<CompileTimeInfo>, client_compile_time_info: Vc<CompileTimeInfo>,
server_compile_time_info: Vc<CompileTimeInfo>, server_compile_time_info: Vc<CompileTimeInfo>,
next_config: Vc<NextConfig>, next_config: Vc<NextConfig>,
...@@ -120,7 +119,6 @@ pub async fn get_page_entries( ...@@ -120,7 +119,6 @@ pub async fn get_page_entries(
let client_runtime_entries = get_client_runtime_entries( let client_runtime_entries = get_client_runtime_entries(
project_root, project_root,
env,
client_ty, client_ty,
mode, mode,
next_config, next_config,
...@@ -150,8 +148,7 @@ pub async fn get_page_entries( ...@@ -150,8 +148,7 @@ pub async fn get_page_entries(
ssr_resolve_options_context, ssr_resolve_options_context,
)); ));
let ssr_runtime_entries = let ssr_runtime_entries = get_server_runtime_entries(ssr_ty, mode);
get_server_runtime_entries(project_root, env, ssr_ty, mode, next_config);
let ssr_runtime_entries = ssr_runtime_entries.resolve_entries(ssr_module_context); let ssr_runtime_entries = ssr_runtime_entries.resolve_entries(ssr_module_context);
let entries = get_page_entries_for_root_directory( let entries = get_page_entries_for_root_directory(
......
...@@ -8,8 +8,8 @@ use indexmap::{ ...@@ -8,8 +8,8 @@ use indexmap::{
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use turbo_tasks::{ use turbo_tasks::{
debug::ValueDebugFormat, trace::TraceRawVcs, Completion, Completions, TaskInput, ValueDefault, debug::ValueDebugFormat, trace::TraceRawVcs, Completion, Completions, TaskInput, ValueToString,
ValueToString, Vc, Vc,
}; };
use turbopack_binding::{ use turbopack_binding::{
turbo::tasks_fs::{DirectoryContent, DirectoryEntry, FileSystemEntryType, FileSystemPath}, turbo::tasks_fs::{DirectoryContent, DirectoryEntry, FileSystemEntryType, FileSystemPath},
...@@ -475,26 +475,6 @@ pub enum Entrypoint { ...@@ -475,26 +475,6 @@ pub enum Entrypoint {
#[turbo_tasks::value(transparent)] #[turbo_tasks::value(transparent)]
pub struct Entrypoints(IndexMap<AppPath, Entrypoint>); pub struct Entrypoints(IndexMap<AppPath, Entrypoint>);
#[turbo_tasks::value_impl]
impl Entrypoints {
#[turbo_tasks::function]
pub fn paths(&self) -> Vc<EntrypointPaths> {
Vc::cell(self.0.keys().cloned().collect())
}
}
#[turbo_tasks::value(transparent)]
#[derive(Default)]
pub struct EntrypointPaths(Vec<AppPath>);
#[turbo_tasks::value_impl]
impl ValueDefault for EntrypointPaths {
#[turbo_tasks::function]
fn value_default() -> Vc<Self> {
Self::default().cell()
}
}
fn is_parallel_route(name: &str) -> bool { fn is_parallel_route(name: &str) -> bool {
name.starts_with('@') name.starts_with('@')
} }
......
This diff is collapsed.
...@@ -13,7 +13,6 @@ mod babel; ...@@ -13,7 +13,6 @@ mod babel;
mod bootstrap; mod bootstrap;
mod embed_js; mod embed_js;
mod emit; mod emit;
mod env;
mod loader_tree; mod loader_tree;
pub mod middleware; pub mod middleware;
pub mod mode; pub mod mode;
......
use std::{collections::HashSet, f64::consts::LN_2};
use serde::{Deserialize, Serialize};
use crate::{
next_app::{AppPage, AppPath, PageSegment, PathSegment},
next_config::Redirect,
};
pub struct ClientRouterFilter {
pub static_filter: BloomFilter,
pub dynamic_filter: BloomFilter,
}
pub fn create_client_router_filter(
paths: &[AppPath],
redirects: &[Redirect],
allowed_error_rate: Option<f64>,
) -> ClientRouterFilter {
let mut static_paths = HashSet::new();
let mut dynamic_paths = HashSet::new();
for path in paths {
if path.is_dynamic() {
let mut sub_path = AppPath::default();
for segment in path.iter() {
if !matches!(segment, PathSegment::Static(_)) {
break;
}
sub_path.0.push(segment.clone());
}
if !sub_path.is_empty() {
dynamic_paths.insert(sub_path.to_string());
}
} else {
static_paths.insert(path.to_string());
}
}
for redirect in redirects {
let app_page = AppPage::parse(&redirect.source).unwrap_or_default();
if app_page
.iter()
.all(|token| matches!(token, PageSegment::Static(_)))
{
static_paths.insert(app_page.to_string());
}
}
let static_filter = BloomFilter::from(static_paths.iter(), allowed_error_rate.unwrap_or(0.01));
let dynamic_filter =
BloomFilter::from(dynamic_paths.iter(), allowed_error_rate.unwrap_or(0.01));
ClientRouterFilter {
static_filter,
dynamic_filter,
}
}
// minimal implementation MurmurHash2 hash function
fn murmurhash2(s: &str) -> u32 {
const M: u32 = 0x5bd1e995;
let mut h: u32 = 0;
for &b in s.as_bytes() {
h = (h ^ b as u32).wrapping_mul(M);
h ^= h >> 13;
h = h.wrapping_mul(M);
}
h
}
#[cfg(test)]
mod test {
use crate::next_app::{
bloom_filter::{create_client_router_filter, murmurhash2, BloomFilter},
AppPath, PathSegment,
};
// testing that we get the same output as the javascript implementation.
#[test]
fn test_murmurhash2() {
assert_eq!(murmurhash2("/"), 4097004964);
assert_eq!(murmurhash2("/test"), 3006934538);
assert_eq!(murmurhash2("/test/route/123/long/as/heck"), 3187325762);
assert_eq!(
murmurhash2("/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
2001750934
);
}
// testing that we get the same output as the javascript implementation.
#[test]
fn test_create_client_router_filter() {
let app_paths = &[
AppPath(vec![]),
AppPath(vec![PathSegment::Static("favicon.ico".to_string())]),
AppPath(vec![PathSegment::Static("_not-found".to_string())]),
AppPath(vec![PathSegment::Static("app".to_string())]),
];
assert_eq!(
create_client_router_filter(app_paths, &[], None).static_filter,
BloomFilter {
num_items: 4,
error_rate: 0.01,
num_bits: 39,
num_hashes: 7,
bit_array: vec![
0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0,
0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1
]
}
)
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct BloomFilter {
num_items: usize,
error_rate: f64,
num_bits: usize,
num_hashes: usize,
bit_array: Vec<u8>,
}
impl BloomFilter {
pub fn new(num_items: usize, error_rate: f64) -> Self {
let num_bits = (-(num_items as f64 * error_rate.ln()) / LN_2.powi(2)).ceil() as usize;
let num_hashes = ((num_bits as f64 / num_items as f64) * LN_2).ceil() as usize;
let bit_array = vec![0; num_bits];
BloomFilter {
num_items,
error_rate,
num_bits,
num_hashes,
bit_array,
}
}
pub fn from<'a>(items: impl IntoIterator<Item = &'a String>, error_rate: f64) -> Self {
let items = items.into_iter().collect::<Vec<_>>();
let mut filter = Self::new(items.len(), error_rate);
for item in items {
filter.add(item)
}
filter
}
pub fn add(&mut self, item: &str) {
let hash_values = self.get_hash_values(item);
hash_values.iter().for_each(|&hash| {
self.bit_array[hash] = 1;
});
}
pub fn contains(&self, item: &str) -> bool {
let hash_values = self.get_hash_values(item);
hash_values.iter().all(|&hash| self.bit_array[hash] == 1)
}
fn get_hash_values(&self, item: &str) -> Vec<usize> {
let mut hash_values = Vec::new();
for i in 1..self.num_hashes + 1 {
let hash = murmurhash2(&format!("{item}{i}")) as usize % self.num_bits;
hash_values.push(hash);
}
hash_values
}
}
...@@ -3,7 +3,6 @@ pub mod app_client_shared_chunks; ...@@ -3,7 +3,6 @@ pub mod app_client_shared_chunks;
pub mod app_entry; pub mod app_entry;
pub mod app_page_entry; pub mod app_page_entry;
pub mod app_route_entry; pub mod app_route_entry;
pub mod bloom_filter;
pub mod metadata; pub mod metadata;
use std::{ use std::{
......
use core::result::Result::Ok; use core::result::Result::Ok;
use anyhow::Result; use anyhow::Result;
use indexmap::IndexMap;
use turbo_tasks::{Value, Vc}; use turbo_tasks::{Value, Vc};
use turbo_tasks_fs::FileSystem; use turbo_tasks_fs::FileSystem;
use turbopack_binding::{ use turbopack_binding::{
turbo::{tasks_env::ProcessEnv, tasks_fs::FileSystemPath}, turbo::{tasks_env::EnvMap, tasks_fs::FileSystemPath},
turbopack::{ turbopack::{
core::{ core::{
compile_time_defines, compile_time_defines,
compile_time_info::{ compile_time_info::{
CompileTimeDefines, CompileTimeInfo, FreeVarReference, FreeVarReferences, CompileTimeDefineValue, CompileTimeDefines, CompileTimeInfo, FreeVarReference,
FreeVarReferences,
}, },
environment::{BrowserEnvironment, Environment, ExecutionEnvironment}, environment::{BrowserEnvironment, Environment, ExecutionEnvironment},
free_var_references, free_var_references,
...@@ -18,7 +20,6 @@ use turbopack_binding::{ ...@@ -18,7 +20,6 @@ use turbopack_binding::{
dev::{react_refresh::assert_can_resolve_react_refresh, DevChunkingContext}, dev::{react_refresh::assert_can_resolve_react_refresh, DevChunkingContext},
ecmascript::chunk::EcmascriptChunkingContext, ecmascript::chunk::EcmascriptChunkingContext,
ecmascript_plugin::transform::directives::server::ServerDirectiveTransformer, ecmascript_plugin::transform::directives::server::ServerDirectiveTransformer,
env::ProcessEnvAsset,
node::execution_context::ExecutionContext, node::execution_context::ExecutionContext,
turbopack::{ turbopack::{
condition::ContextCondition, condition::ContextCondition,
...@@ -34,12 +35,9 @@ use turbopack_binding::{ ...@@ -34,12 +35,9 @@ use turbopack_binding::{
use super::transforms::get_next_client_transforms_rules; use super::transforms::get_next_client_transforms_rules;
use crate::{ use crate::{
app_structure::{get_entrypoints, EntrypointPaths, OptionAppDir},
babel::maybe_add_babel_loader, babel::maybe_add_babel_loader,
embed_js::next_js_fs, embed_js::next_js_fs,
env::env_for_js,
mode::NextMode, mode::NextMode,
next_app::{bloom_filter::create_client_router_filter, AppPath},
next_build::{get_external_next_compiled_package_mapping, get_postcss_package_mapping}, next_build::{get_external_next_compiled_package_mapping, get_postcss_package_mapping},
next_client::runtime_entry::{RuntimeEntries, RuntimeEntry}, next_client::runtime_entry::{RuntimeEntries, RuntimeEntry},
next_config::NextConfig, next_config::NextConfig,
...@@ -67,82 +65,40 @@ use crate::{ ...@@ -67,82 +65,40 @@ use crate::{
util::foreign_code_context_condition, util::foreign_code_context_condition,
}; };
fn defines( fn defines(mode: NextMode, define_env: &IndexMap<String, String>) -> CompileTimeDefines {
mode: NextMode, // Need the empty NEXT_RUNTIME here for compile time evaluation.
dist_root_path: &str, let mut defines = compile_time_defines!(
next_config: &NextConfig,
app_paths: &[AppPath],
) -> Result<CompileTimeDefines> {
let filter = create_client_router_filter(
app_paths,
if next_config
.experimental
.client_router_filter_redirects
.unwrap_or_default()
{
next_config
.original_redirects
.as_deref()
.unwrap_or_default()
} else {
&[]
},
next_config.experimental.client_router_filter_allowed_rate,
);
// TODO: macro may need to allow dynamically expand from some iterable values
// TODO(WEB-937): there are more defines needed, see
// packages/next/src/build/webpack/plugins/define-env-plugin.ts
Ok(compile_time_defines!(
process.browser = true,
process.turbopack = true, process.turbopack = true,
process.env.TURBOPACK = true, process.env.NEXT_RUNTIME = "",
process.env.NODE_ENV = mode.node_env(), process.env.NODE_ENV = mode.node_env(),
process.env.NEXT_RUNTIME = "".to_string(), process.env.TURBOPACK = true,
process.env.__NEXT_CLIENT_ROUTER_FILTER_ENABLED = next_config );
.experimental
.client_router_filter for (k, v) in define_env {
.unwrap_or_default(), defines
process.env.__NEXT_CLIENT_ROUTER_S_FILTER = serde_json::to_value(&filter.static_filter)?, .0
process.env.__NEXT_CLIENT_ROUTER_D_FILTER = serde_json::to_value(&filter.dynamic_filter)?, .entry(k.split('.').map(|s| s.to_string()).collect())
process.env.__NEXT_DIST_DIR = dist_root_path.to_string(), .or_insert_with(|| CompileTimeDefineValue::JSON(v.clone()));
process.env.__NEXT_HAS_REWRITES = true, }
))
defines
} }
#[turbo_tasks::function] #[turbo_tasks::function]
async fn next_client_defines( async fn next_client_defines(
mode: NextMode, mode: NextMode,
dist_root_path: Vc<String>, define_env: Vc<EnvMap>,
next_config: Vc<NextConfig>,
app_paths: Vc<EntrypointPaths>,
) -> Result<Vc<CompileTimeDefines>> { ) -> Result<Vc<CompileTimeDefines>> {
let dist_root_path = &*dist_root_path.await?; Ok(defines(mode, &*define_env.await?).cell())
Ok(defines(
mode,
dist_root_path.as_str(),
&*next_config.await?,
&app_paths.await?,
)?
.cell())
} }
#[turbo_tasks::function] #[turbo_tasks::function]
async fn next_client_free_vars( async fn next_client_free_vars(
mode: NextMode, mode: NextMode,
dist_root_path: Vc<String>, define_env: Vc<EnvMap>,
next_config: Vc<NextConfig>,
app_paths: Vc<EntrypointPaths>,
) -> Result<Vc<FreeVarReferences>> { ) -> Result<Vc<FreeVarReferences>> {
let dist_root_path = &*dist_root_path.await?;
Ok(free_var_references!( Ok(free_var_references!(
..defines( ..defines(mode, &*define_env.await?).into_iter(),
mode,
dist_root_path.as_str(),
&*next_config.await?,
&app_paths.await?
)?
.into_iter(),
Buffer = FreeVarReference::EcmaScriptModule { Buffer = FreeVarReference::EcmaScriptModule {
request: "node:buffer".to_string(), request: "node:buffer".to_string(),
lookup_path: None, lookup_path: None,
...@@ -157,28 +113,12 @@ async fn next_client_free_vars( ...@@ -157,28 +113,12 @@ async fn next_client_free_vars(
.cell()) .cell())
} }
#[turbo_tasks::function]
async fn get_app_paths(
app_dir: Vc<OptionAppDir>,
next_config: Vc<NextConfig>,
) -> Result<Vc<EntrypointPaths>> {
Ok(if let Some(app_dir) = *app_dir.await? {
get_entrypoints(app_dir, next_config.page_extensions()).paths()
} else {
Default::default()
})
}
#[turbo_tasks::function] #[turbo_tasks::function]
pub fn get_client_compile_time_info( pub fn get_client_compile_time_info(
mode: NextMode, mode: NextMode,
browserslist_query: String, browserslist_query: String,
dist_root_path: Vc<String>, define_env: Vc<EnvMap>,
next_config: Vc<NextConfig>,
app_dir: Vc<OptionAppDir>,
) -> Vc<CompileTimeInfo> { ) -> Vc<CompileTimeInfo> {
let app_paths = get_app_paths(app_dir, next_config);
CompileTimeInfo::builder(Environment::new(Value::new(ExecutionEnvironment::Browser( CompileTimeInfo::builder(Environment::new(Value::new(ExecutionEnvironment::Browser(
BrowserEnvironment { BrowserEnvironment {
dom: true, dom: true,
...@@ -188,18 +128,8 @@ pub fn get_client_compile_time_info( ...@@ -188,18 +128,8 @@ pub fn get_client_compile_time_info(
} }
.into(), .into(),
)))) ))))
.defines(next_client_defines( .defines(next_client_defines(mode, define_env))
mode, .free_var_references(next_client_free_vars(mode, define_env))
dist_root_path,
next_config,
app_paths,
))
.free_var_references(next_client_free_vars(
mode,
dist_root_path,
next_config,
app_paths,
))
.cell() .cell()
} }
...@@ -401,7 +331,6 @@ pub fn get_client_assets_path(client_root: Vc<FileSystemPath>) -> Vc<FileSystemP ...@@ -401,7 +331,6 @@ pub fn get_client_assets_path(client_root: Vc<FileSystemPath>) -> Vc<FileSystemP
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn get_client_runtime_entries( pub async fn get_client_runtime_entries(
project_root: Vc<FileSystemPath>, project_root: Vc<FileSystemPath>,
env: Vc<Box<dyn ProcessEnv>>,
ty: Value<ClientContextType>, ty: Value<ClientContextType>,
mode: NextMode, mode: NextMode,
next_config: Vc<NextConfig>, next_config: Vc<NextConfig>,
...@@ -409,19 +338,6 @@ pub async fn get_client_runtime_entries( ...@@ -409,19 +338,6 @@ pub async fn get_client_runtime_entries(
) -> Result<Vc<RuntimeEntries>> { ) -> Result<Vc<RuntimeEntries>> {
let mut runtime_entries = vec![]; let mut runtime_entries = vec![];
if matches!(
*ty,
ClientContextType::App { .. } | ClientContextType::Pages { .. },
) {
runtime_entries.push(
RuntimeEntry::Source(Vc::upcast(ProcessEnvAsset::new(
project_root,
env_for_js(env, true, next_config),
)))
.cell(),
);
}
match mode { match mode {
NextMode::Development => { NextMode::Development => {
let resolve_options_context = get_client_resolve_options_context( let resolve_options_context = get_client_resolve_options_context(
......
use anyhow::Result; use anyhow::Result;
use indexmap::IndexMap;
use turbo_tasks::{Value, Vc}; use turbo_tasks::{Value, Vc};
use turbopack_binding::{ use turbopack_binding::{
turbo::tasks_fs::FileSystemPath, turbo::{tasks_env::EnvMap, tasks_fs::FileSystemPath},
turbopack::{ turbopack::{
core::{ core::{
compile_time_defines, compile_time_defines,
compile_time_info::{ compile_time_info::{
CompileTimeDefines, CompileTimeInfo, FreeVarReference, FreeVarReferences, CompileTimeDefineValue, CompileTimeDefines, CompileTimeInfo, FreeVarReference,
FreeVarReferences,
}, },
environment::{EdgeWorkerEnvironment, Environment, ExecutionEnvironment, ServerAddr}, environment::{EdgeWorkerEnvironment, Environment, ExecutionEnvironment, ServerAddr},
free_var_references, free_var_references,
...@@ -31,46 +33,40 @@ use crate::{ ...@@ -31,46 +33,40 @@ use crate::{
util::foreign_code_context_condition, util::foreign_code_context_condition,
}; };
fn defines(dist_root_path: Option<&str>) -> CompileTimeDefines { fn defines(mode: NextMode, define_env: &IndexMap<String, String>) -> CompileTimeDefines {
// [TODO] macro may need to allow dynamically expand from some iterable values
let mut defines = compile_time_defines!( let mut defines = compile_time_defines!(
process.turbopack = true, process.turbopack = true,
process.env.NODE_ENV = "development",
process.env.__NEXT_CLIENT_ROUTER_FILTER_ENABLED = false,
process.env.NEXT_RUNTIME = "edge", process.env.NEXT_RUNTIME = "edge",
process.env.NODE_ENV = mode.node_env(),
process.env.TURBOPACK = true,
); );
if let Some(dist_root_path) = dist_root_path { for (k, v) in define_env {
defines.0.insert( defines
vec![ .0
"process".to_string(), .entry(k.split('.').map(|s| s.to_string()).collect())
"env".to_string(), .or_insert_with(|| CompileTimeDefineValue::JSON(v.clone()));
"__NEXT_DIST_DIR".to_string(),
],
dist_root_path.to_string().into(),
);
} }
// TODO(WEB-937) there are more defines needed, see
// packages/next/src/build/webpack-config.ts
defines defines
} }
#[turbo_tasks::function] #[turbo_tasks::function]
async fn next_edge_defines(dist_root_path: Vc<String>) -> Result<Vc<CompileTimeDefines>> { async fn next_edge_defines(
let dist_root_path = &*dist_root_path.await?; mode: NextMode,
Ok(defines(Some(dist_root_path.as_str())).cell()) define_env: Vc<EnvMap>,
) -> Result<Vc<CompileTimeDefines>> {
Ok(defines(mode, &*define_env.await?).cell())
} }
#[turbo_tasks::function] #[turbo_tasks::function]
async fn next_edge_free_vars( async fn next_edge_free_vars(
mode: NextMode,
project_path: Vc<FileSystemPath>, project_path: Vc<FileSystemPath>,
dist_root_path: Vc<String>, define_env: Vc<EnvMap>,
) -> Result<Vc<FreeVarReferences>> { ) -> Result<Vc<FreeVarReferences>> {
let dist_root_path = &*dist_root_path.await?;
Ok(free_var_references!( Ok(free_var_references!(
..defines(Some(dist_root_path.as_str())).into_iter(), ..defines(mode, &*define_env.await?).into_iter(),
Buffer = FreeVarReference::EcmaScriptModule { Buffer = FreeVarReference::EcmaScriptModule {
request: "next/dist/compiled/buffer".to_string(), request: "next/dist/compiled/buffer".to_string(),
lookup_path: Some(project_path), lookup_path: Some(project_path),
...@@ -87,15 +83,16 @@ async fn next_edge_free_vars( ...@@ -87,15 +83,16 @@ async fn next_edge_free_vars(
#[turbo_tasks::function] #[turbo_tasks::function]
pub fn get_edge_compile_time_info( pub fn get_edge_compile_time_info(
mode: NextMode,
project_path: Vc<FileSystemPath>, project_path: Vc<FileSystemPath>,
server_addr: Vc<ServerAddr>, server_addr: Vc<ServerAddr>,
dist_root_path: Vc<String>, define_env: Vc<EnvMap>,
) -> Vc<CompileTimeInfo> { ) -> Vc<CompileTimeInfo> {
CompileTimeInfo::builder(Environment::new(Value::new( CompileTimeInfo::builder(Environment::new(Value::new(
ExecutionEnvironment::EdgeWorker(EdgeWorkerEnvironment { server_addr }.into()), ExecutionEnvironment::EdgeWorker(EdgeWorkerEnvironment { server_addr }.into()),
))) )))
.defines(next_edge_defines(dist_root_path)) .defines(next_edge_defines(mode, define_env))
.free_var_references(next_edge_free_vars(project_path, dist_root_path)) .free_var_references(next_edge_free_vars(mode, project_path, define_env))
.cell() .cell()
} }
......
use anyhow::Result; use anyhow::Result;
use indexmap::IndexMap;
use turbo_tasks::{Value, Vc}; use turbo_tasks::{Value, Vc};
use turbo_tasks_fs::FileSystem; use turbo_tasks_fs::FileSystem;
use turbopack_binding::{ use turbopack_binding::{
turbo::{tasks_env::ProcessEnv, tasks_fs::FileSystemPath}, turbo::{
tasks_env::{EnvMap, ProcessEnv},
tasks_fs::FileSystemPath,
},
turbopack::{ turbopack::{
build::{BuildChunkingContext, MinifyType}, build::{BuildChunkingContext, MinifyType},
core::{ core::{
compile_time_defines, compile_time_defines,
compile_time_info::{CompileTimeDefines, CompileTimeInfo, FreeVarReferences}, compile_time_info::{
CompileTimeDefineValue, CompileTimeDefines, CompileTimeInfo, FreeVarReferences,
},
environment::{Environment, ExecutionEnvironment, NodeJsEnvironment, ServerAddr}, environment::{Environment, ExecutionEnvironment, NodeJsEnvironment, ServerAddr},
free_var_references, free_var_references,
resolve::{parse::Request, pattern::Pattern}, resolve::{parse::Request, pattern::Pattern},
...@@ -16,7 +22,6 @@ use turbopack_binding::{ ...@@ -16,7 +22,6 @@ use turbopack_binding::{
ecmascript_plugin::transform::directives::{ ecmascript_plugin::transform::directives::{
client::ClientDirectiveTransformer, server::ServerDirectiveTransformer, client::ClientDirectiveTransformer, server::ServerDirectiveTransformer,
}, },
env::ProcessEnvAsset,
node::execution_context::ExecutionContext, node::execution_context::ExecutionContext,
turbopack::{ turbopack::{
condition::ContextCondition, condition::ContextCondition,
...@@ -38,7 +43,6 @@ use super::{ ...@@ -38,7 +43,6 @@ use super::{
use crate::{ use crate::{
babel::maybe_add_babel_loader, babel::maybe_add_babel_loader,
embed_js::next_js_fs, embed_js::next_js_fs,
env::env_for_js,
mode::NextMode, mode::NextMode,
next_build::{get_external_next_compiled_package_mapping, get_postcss_package_mapping}, next_build::{get_external_next_compiled_package_mapping, get_postcss_package_mapping},
next_client::{RuntimeEntries, RuntimeEntry}, next_client::{RuntimeEntries, RuntimeEntry},
...@@ -174,26 +178,38 @@ pub async fn get_server_resolve_options_context( ...@@ -174,26 +178,38 @@ pub async fn get_server_resolve_options_context(
.cell()) .cell())
} }
fn defines(mode: NextMode) -> CompileTimeDefines { fn defines(mode: NextMode, define_env: &IndexMap<String, String>) -> CompileTimeDefines {
compile_time_defines!( let mut defines = compile_time_defines!(
process.turbopack = true, process.turbopack = true,
process.env.NODE_ENV = mode.node_env(),
process.env.__NEXT_CLIENT_ROUTER_FILTER_ENABLED = false,
process.env.NEXT_RUNTIME = "nodejs", process.env.NEXT_RUNTIME = "nodejs",
process.env.__NEXT_EXPERIMENTAL_REACT = false, process.env.NODE_ENV = mode.node_env(),
) process.env.TURBOPACK = true,
// TODO(WEB-937) there are more defines needed, see );
// packages/next/src/build/webpack-config.ts
for (k, v) in define_env {
defines
.0
.entry(k.split('.').map(|s| s.to_string()).collect())
.or_insert_with(|| CompileTimeDefineValue::JSON(v.clone()));
}
defines
} }
#[turbo_tasks::function] #[turbo_tasks::function]
fn next_server_defines(mode: NextMode) -> Vc<CompileTimeDefines> { async fn next_server_defines(
defines(mode).cell() mode: NextMode,
define_env: Vc<EnvMap>,
) -> Result<Vc<CompileTimeDefines>> {
Ok(defines(mode, &*define_env.await?).cell())
} }
#[turbo_tasks::function] #[turbo_tasks::function]
async fn next_server_free_vars(mode: NextMode) -> Result<Vc<FreeVarReferences>> { async fn next_server_free_vars(
Ok(free_var_references!(..defines(mode).into_iter()).cell()) mode: NextMode,
define_env: Vc<EnvMap>,
) -> Result<Vc<FreeVarReferences>> {
Ok(free_var_references!(..defines(mode, &*define_env.await?).into_iter()).cell())
} }
#[turbo_tasks::function] #[turbo_tasks::function]
...@@ -201,12 +217,13 @@ pub fn get_server_compile_time_info( ...@@ -201,12 +217,13 @@ pub fn get_server_compile_time_info(
mode: NextMode, mode: NextMode,
process_env: Vc<Box<dyn ProcessEnv>>, process_env: Vc<Box<dyn ProcessEnv>>,
server_addr: Vc<ServerAddr>, server_addr: Vc<ServerAddr>,
define_env: Vc<EnvMap>,
) -> Vc<CompileTimeInfo> { ) -> Vc<CompileTimeInfo> {
CompileTimeInfo::builder(Environment::new(Value::new( CompileTimeInfo::builder(Environment::new(Value::new(
ExecutionEnvironment::NodeJsLambda(NodeJsEnvironment::current(process_env, server_addr)), ExecutionEnvironment::NodeJsLambda(NodeJsEnvironment::current(process_env, server_addr)),
))) )))
.defines(next_server_defines(mode)) .defines(next_server_defines(mode, define_env))
.free_var_references(next_server_free_vars(mode)) .free_var_references(next_server_free_vars(mode, define_env))
.cell() .cell()
} }
...@@ -563,17 +580,10 @@ pub fn get_build_module_options_context() -> Vc<ModuleOptionsContext> { ...@@ -563,17 +580,10 @@ pub fn get_build_module_options_context() -> Vc<ModuleOptionsContext> {
#[turbo_tasks::function] #[turbo_tasks::function]
pub fn get_server_runtime_entries( pub fn get_server_runtime_entries(
project_root: Vc<FileSystemPath>,
env: Vc<Box<dyn ProcessEnv>>,
ty: Value<ServerContextType>, ty: Value<ServerContextType>,
mode: NextMode, mode: NextMode,
next_config: Vc<NextConfig>,
) -> Vc<RuntimeEntries> { ) -> Vc<RuntimeEntries> {
let mut runtime_entries = vec![RuntimeEntry::Source(Vc::upcast(ProcessEnvAsset::new( let mut runtime_entries = vec![];
project_root,
env_for_js(env, false, next_config),
)))
.cell()];
if matches!(mode, NextMode::Build) { if matches!(mode, NextMode::Build) {
if let ServerContextType::AppRSC { .. } = ty.into_value() { if let ServerContextType::AppRSC { .. } = ty.into_value() {
......
...@@ -123,6 +123,7 @@ import { ...@@ -123,6 +123,7 @@ import {
teardownCrashReporter, teardownCrashReporter,
loadBindings, loadBindings,
teardownHeapProfiler, teardownHeapProfiler,
createDefineEnv,
} from './swc' } from './swc'
import { getNamedRouteRegex } from '../shared/lib/router/utils/route-regex' import { getNamedRouteRegex } from '../shared/lib/router/utils/route-regex'
import { flatReaddir } from '../lib/flat-readdir' import { flatReaddir } from '../lib/flat-readdir'
...@@ -1018,10 +1019,28 @@ export default async function build( ...@@ -1018,10 +1019,28 @@ export default async function build(
: packagePath : packagePath
? path.dirname(packagePath) ? path.dirname(packagePath)
: undefined) : undefined)
const hasRewrites =
rewrites.beforeFiles.length > 0 ||
rewrites.afterFiles.length > 0 ||
rewrites.fallback.length > 0
await binding.turbo.nextBuild({ await binding.turbo.nextBuild({
...NextBuildContext, ...NextBuildContext,
root, root,
distDir: config.distDir, distDir: config.distDir,
defineEnv: createDefineEnv({
allowedRevalidateHeaderKeys:
config.experimental.allowedRevalidateHeaderKeys,
clientRouterFilters: NextBuildContext.clientRouterFilters,
config,
dev: false,
distDir,
fetchCacheKeyPrefix: config.experimental.fetchCacheKeyPrefix,
hasRewrites,
middlewareMatchers: undefined,
previewModeId: undefined,
}),
}) })
const [duration] = process.hrtime(turboNextBuildStart) const [duration] = process.hrtime(turboNextBuildStart)
......
...@@ -58,7 +58,7 @@ export default async function loadJsConfig( ...@@ -58,7 +58,7 @@ export default async function loadJsConfig(
) )
let implicitBaseurl let implicitBaseurl
let jsConfig let jsConfig: { compilerOptions: Record<string, any> } | undefined
// jsconfig is a subset of tsconfig // jsconfig is a subset of tsconfig
if (useTypeScript) { if (useTypeScript) {
if ( if (
......
...@@ -10,6 +10,10 @@ import { patchIncorrectLockfile } from '../../lib/patch-incorrect-lockfile' ...@@ -10,6 +10,10 @@ import { patchIncorrectLockfile } from '../../lib/patch-incorrect-lockfile'
import { downloadWasmSwc, downloadNativeNextSwc } from '../../lib/download-swc' import { downloadWasmSwc, downloadNativeNextSwc } from '../../lib/download-swc'
import { NextConfigComplete, TurboRule } from '../../server/config-shared' import { NextConfigComplete, TurboRule } from '../../server/config-shared'
import { isDeepStrictEqual } from 'util' import { isDeepStrictEqual } from 'util'
import {
DefineEnvPluginOptions,
getDefineEnv,
} from '../webpack/plugins/define-env-plugin'
const nextVersion = process.env.__NEXT_VERSION as string const nextVersion = process.env.__NEXT_VERSION as string
...@@ -365,7 +369,7 @@ function logLoadFailure(attempts: any, triedWasm = false) { ...@@ -365,7 +369,7 @@ function logLoadFailure(attempts: any, triedWasm = false) {
}) })
} }
interface ProjectOptions { export interface ProjectOptions {
/** /**
* A root path from which all files must be nested under. Trying to access * A root path from which all files must be nested under. Trying to access
* a file outside this root will fail. Think of this as a chroot. * a file outside this root will fail. Think of this as a chroot.
...@@ -398,6 +402,8 @@ interface ProjectOptions { ...@@ -398,6 +402,8 @@ interface ProjectOptions {
*/ */
env: Record<string, string> env: Record<string, string>
defineEnv: DefineEnv
/** /**
* Whether to watch the filesystem for file changes. * Whether to watch the filesystem for file changes.
*/ */
...@@ -409,7 +415,58 @@ interface ProjectOptions { ...@@ -409,7 +415,58 @@ interface ProjectOptions {
serverAddr: string serverAddr: string
} }
interface TurboEngineOptions { type RustifiedEnv = { name: string; value: string }[]
export interface DefineEnv {
client: RustifiedEnv
edge: RustifiedEnv
nodejs: RustifiedEnv
}
export function createDefineEnv({
allowedRevalidateHeaderKeys,
clientRouterFilters,
config,
dev,
distDir,
fetchCacheKeyPrefix,
hasRewrites,
middlewareMatchers,
previewModeId,
}: Omit<
DefineEnvPluginOptions,
'isClient' | 'isNodeOrEdgeCompilation' | 'isEdgeServer' | 'isNodeServer'
>): DefineEnv {
let defineEnv: DefineEnv = {
client: [],
edge: [],
nodejs: [],
}
for (const variant of Object.keys(defineEnv) as (keyof typeof defineEnv)[]) {
defineEnv[variant] = rustifyEnv(
getDefineEnv({
allowedRevalidateHeaderKeys,
clientRouterFilters,
config,
dev,
distDir,
fetchCacheKeyPrefix,
hasRewrites,
isClient: variant === 'client',
isEdgeServer: variant === 'edge',
isNodeOrEdgeCompilation: variant === 'nodejs' || variant === 'edge',
isNodeServer: variant === 'nodejs',
middlewareMatchers,
previewModeId,
})
)
}
return defineEnv
}
export interface TurboEngineOptions {
/** /**
* An upper bound of memory that turbopack will attempt to stay under. * An upper bound of memory that turbopack will attempt to stay under.
*/ */
...@@ -481,7 +538,7 @@ export interface ServerClientChange { ...@@ -481,7 +538,7 @@ export interface ServerClientChange {
} }
export interface Project { export interface Project {
update(options: ProjectOptions): Promise<void> update(options: Partial<ProjectOptions>): Promise<void>
entrypointsSubscribe(): AsyncIterableIterator<TurbopackResult<Entrypoints>> entrypointsSubscribe(): AsyncIterableIterator<TurbopackResult<Entrypoints>>
hmrEvents(identifier: string): AsyncIterableIterator<TurbopackResult<Update>> hmrEvents(identifier: string): AsyncIterableIterator<TurbopackResult<Update>>
hmrIdentifiersSubscribe(): AsyncIterableIterator< hmrIdentifiersSubscribe(): AsyncIterableIterator<
...@@ -563,6 +620,15 @@ export type WrittenEndpoint = ...@@ -563,6 +620,15 @@ export type WrittenEndpoint =
config: EndpointConfig config: EndpointConfig
} }
function rustifyEnv(env: Record<string, string>): RustifiedEnv {
return Object.entries(env)
.filter(([_, value]) => value != null)
.map(([name, value]) => ({
name,
value,
}))
}
// TODO(sokra) Support wasm option. // TODO(sokra) Support wasm option.
function bindingToApi(binding: any, _wasm: boolean) { function bindingToApi(binding: any, _wasm: boolean) {
type NativeFunction<T> = ( type NativeFunction<T> = (
...@@ -687,15 +753,16 @@ function bindingToApi(binding: any, _wasm: boolean) { ...@@ -687,15 +753,16 @@ function bindingToApi(binding: any, _wasm: boolean) {
}) })
} }
async function rustifyProjectOptions(options: ProjectOptions): Promise<any> { async function rustifyProjectOptions(
options: Partial<ProjectOptions>
): Promise<any> {
return { return {
...options, ...options,
nextConfig: await serializeNextConfig(options.nextConfig), nextConfig:
jsConfig: JSON.stringify(options.jsConfig ?? {}), options.nextConfig && (await serializeNextConfig(options.nextConfig)),
env: Object.entries(options.env).map(([name, value]) => ({ jsConfig: options.jsConfig && JSON.stringify(options.jsConfig),
name, env: options.env && rustifyEnv(options.env),
value, defineEnv: options.defineEnv,
})),
} }
} }
......
...@@ -142,17 +142,6 @@ function isModuleCSS(module: { type: string }) { ...@@ -142,17 +142,6 @@ function isModuleCSS(module: { type: string }) {
) )
} }
export function errorIfEnvConflicted(config: NextConfigComplete, key: string) {
const isPrivateKey = /^(?:NODE_.+)|^(?:__.+)$/i.test(key)
const hasNextRuntimeKey = key === 'NEXT_RUNTIME'
if (isPrivateKey || hasNextRuntimeKey) {
throw new Error(
`The key "${key}" under "env" in ${config.configFileName} is not allowed. https://nextjs.org/docs/messages/env-key-not-allowed`
)
}
}
function getReactProfilingInProduction() { function getReactProfilingInProduction() {
return { return {
'react-dom$': 'react-dom/profiling', 'react-dom$': 'react-dom/profiling',
......
...@@ -2,15 +2,28 @@ import type { NextConfigComplete } from '../../../server/config-shared' ...@@ -2,15 +2,28 @@ import type { NextConfigComplete } from '../../../server/config-shared'
import type { MiddlewareMatcher } from '../../analysis/get-page-static-info' import type { MiddlewareMatcher } from '../../analysis/get-page-static-info'
import { webpack } from 'next/dist/compiled/webpack/webpack' import { webpack } from 'next/dist/compiled/webpack/webpack'
import { needsExperimentalReact } from '../../../lib/needs-experimental-react' import { needsExperimentalReact } from '../../../lib/needs-experimental-react'
import getBaseWebpackConfig, {
errorIfEnvConflicted,
} from '../../webpack-config'
interface DefineEnvPluginOptions { function errorIfEnvConflicted(config: NextConfigComplete, key: string) {
const isPrivateKey = /^(?:NODE_.+)|^(?:__.+)$/i.test(key)
const hasNextRuntimeKey = key === 'NEXT_RUNTIME'
if (isPrivateKey || hasNextRuntimeKey) {
throw new Error(
`The key "${key}" under "env" in ${config.configFileName} is not allowed. https://nextjs.org/docs/messages/env-key-not-allowed`
)
}
}
export interface DefineEnvPluginOptions {
allowedRevalidateHeaderKeys: string[] | undefined allowedRevalidateHeaderKeys: string[] | undefined
clientRouterFilters: Parameters< clientRouterFilters?: {
typeof getBaseWebpackConfig staticFilter: ReturnType<
>[1]['clientRouterFilters'] import('../../../shared/lib/bloom-filter').BloomFilter['export']
>
dynamicFilter: ReturnType<
import('../../../shared/lib/bloom-filter').BloomFilter['export']
>
}
config: NextConfigComplete config: NextConfigComplete
dev: boolean dev: boolean
distDir: string distDir: string
......
...@@ -4,7 +4,7 @@ import type { Duplex } from 'stream' ...@@ -4,7 +4,7 @@ import type { Duplex } from 'stream'
import type { webpack } from 'next/dist/compiled/webpack/webpack' import type { webpack } from 'next/dist/compiled/webpack/webpack'
import type getBaseWebpackConfig from '../../build/webpack-config' import type getBaseWebpackConfig from '../../build/webpack-config'
import type { RouteMatch } from '../future/route-matches/route-match' import type { RouteMatch } from '../future/route-matches/route-match'
import type { Update as TurbopackUpdate } from '../../build/swc' import type { Project, Update as TurbopackUpdate } from '../../build/swc'
import type { VersionInfo } from './parse-version-info' import type { VersionInfo } from './parse-version-info'
export const enum HMR_ACTIONS_SENT_TO_BROWSER { export const enum HMR_ACTIONS_SENT_TO_BROWSER {
...@@ -107,6 +107,7 @@ export type HMR_ACTION_TYPES = ...@@ -107,6 +107,7 @@ export type HMR_ACTION_TYPES =
| ServerErrorAction | ServerErrorAction
export interface NextJsHotReloaderInterface { export interface NextJsHotReloaderInterface {
turbopackProject?: Project
activeWebpackConfigs?: Array<Awaited<ReturnType<typeof getBaseWebpackConfig>>> activeWebpackConfigs?: Array<Awaited<ReturnType<typeof getBaseWebpackConfig>>>
serverStats: webpack.Stats | null serverStats: webpack.Stats | null
edgeServerStats: webpack.Stats | null edgeServerStats: webpack.Stats | null
......
...@@ -7,6 +7,7 @@ import { ...@@ -7,6 +7,7 @@ import {
ServerClientChange, ServerClientChange,
ServerClientChangeType, ServerClientChangeType,
Issue, Issue,
createDefineEnv,
} from '../../../build/swc' } from '../../../build/swc'
import type { Socket } from 'net' import type { Socket } from 'net'
import ws from 'next/dist/compiled/ws' import ws from 'next/dist/compiled/ws'
...@@ -209,13 +210,29 @@ async function startWatcher(opts: SetupOpts) { ...@@ -209,13 +210,29 @@ async function startWatcher(opts: SetupOpts) {
}) })
} }
const hasRewrites =
opts.fsChecker.rewrites.afterFiles.length > 0 ||
opts.fsChecker.rewrites.beforeFiles.length > 0 ||
opts.fsChecker.rewrites.fallback.length > 0
const project = await bindings.turbo.createProject({ const project = await bindings.turbo.createProject({
projectPath: dir, projectPath: dir,
rootPath: opts.nextConfig.experimental.outputFileTracingRoot || dir, rootPath: opts.nextConfig.experimental.outputFileTracingRoot || dir,
nextConfig: opts.nextConfig, nextConfig: opts.nextConfig,
jsConfig, jsConfig: jsConfig ?? { compilerOptions: {} },
watch: true, watch: true,
env: process.env as Record<string, string>, env: process.env as Record<string, string>,
defineEnv: createDefineEnv({
allowedRevalidateHeaderKeys: undefined,
clientRouterFilters: undefined,
config: nextConfig,
dev: true,
distDir,
fetchCacheKeyPrefix: undefined,
hasRewrites,
middlewareMatchers: undefined,
previewModeId: undefined,
}),
serverAddr: `127.0.0.1:${opts.port}`, serverAddr: `127.0.0.1:${opts.port}`,
}) })
const iter = project.entrypointsSubscribe() const iter = project.entrypointsSubscribe()
...@@ -966,6 +983,7 @@ async function startWatcher(opts: SetupOpts) { ...@@ -966,6 +983,7 @@ async function startWatcher(opts: SetupOpts) {
await writeFontManifest() await writeFontManifest()
const turbopackHotReloader: NextJsHotReloaderInterface = { const turbopackHotReloader: NextJsHotReloaderInterface = {
turbopackProject: project,
activeWebpackConfigs: undefined, activeWebpackConfigs: undefined,
serverStats: null, serverStats: null,
edgeServerStats: null, edgeServerStats: null,
...@@ -1733,6 +1751,27 @@ async function startWatcher(opts: SetupOpts) { ...@@ -1733,6 +1751,27 @@ async function startWatcher(opts: SetupOpts) {
} }
} }
if (hotReloader.turbopackProject) {
const hasRewrites =
opts.fsChecker.rewrites.afterFiles.length > 0 ||
opts.fsChecker.rewrites.beforeFiles.length > 0 ||
opts.fsChecker.rewrites.fallback.length > 0
await hotReloader.turbopackProject.update({
defineEnv: createDefineEnv({
allowedRevalidateHeaderKeys: undefined,
clientRouterFilters,
config: nextConfig,
dev: true,
distDir,
fetchCacheKeyPrefix: undefined,
hasRewrites,
middlewareMatchers: undefined,
previewModeId: undefined,
}),
})
}
hotReloader.activeWebpackConfigs?.forEach((config, idx) => { hotReloader.activeWebpackConfigs?.forEach((config, idx) => {
const isClient = idx === 0 const isClient = idx === 0
const isNodeServer = idx === 1 const isNodeServer = idx === 1
......
...@@ -27,6 +27,7 @@ declare module 'next/dist/compiled/webpack/webpack' { ...@@ -27,6 +27,7 @@ declare module 'next/dist/compiled/webpack/webpack' {
export type EntryObject = any export type EntryObject = any
export type Chunk = any export type Chunk = any
export type ChunkGroup = any export type ChunkGroup = any
export type DefinePlugin = any
// eslint-disable-next-line @typescript-eslint/no-shadow // eslint-disable-next-line @typescript-eslint/no-shadow
namespace sources { namespace sources {
export type RawSource = any export type RawSource = any
......
...@@ -2,6 +2,7 @@ import { NextInstance, createNext } from 'e2e-utils' ...@@ -2,6 +2,7 @@ import { NextInstance, createNext } from 'e2e-utils'
import { trace } from 'next/src/trace' import { trace } from 'next/src/trace'
import { PHASE_DEVELOPMENT_SERVER } from 'next/constants' import { PHASE_DEVELOPMENT_SERVER } from 'next/constants'
import { import {
createDefineEnv,
Diagnostics, Diagnostics,
Entrypoints, Entrypoints,
Issue, Issue,
...@@ -173,6 +174,22 @@ describe('next.rs api', () => { ...@@ -173,6 +174,22 @@ describe('next.rs api', () => {
: next.testDir, : next.testDir,
watch: true, watch: true,
serverAddr: `127.0.0.1:3000`, serverAddr: `127.0.0.1:3000`,
defineEnv: createDefineEnv({
allowedRevalidateHeaderKeys: undefined,
clientRouterFilters: undefined,
config: nextConfig,
dev: true,
distDir: path.join(
process.env.NEXT_SKIP_ISOLATE
? path.resolve(__dirname, '../../..')
: next.testDir,
'.next'
),
fetchCacheKeyPrefix: undefined,
hasRewrites: false,
middlewareMatchers: undefined,
previewModeId: undefined,
}),
}) })
projectUpdateSubscription = project.updateInfoSubscribe() projectUpdateSubscription = project.updateInfoSubscribe()
}) })
......
...@@ -6987,8 +6987,6 @@ ...@@ -6987,8 +6987,6 @@
"Basic CSS Modules Ordering Development Mode should have correct color on index page (on hover)", "Basic CSS Modules Ordering Development Mode should have correct color on index page (on hover)",
"Basic CSS Modules Ordering Development Mode should have correct color on index page (on load)", "Basic CSS Modules Ordering Development Mode should have correct color on index page (on load)",
"Basic CSS Modules Ordering Development Mode should have correct color on index page (on nav)", "Basic CSS Modules Ordering Development Mode should have correct color on index page (on nav)",
"CSS Modules Composes Ordering Development Mode should have correct color on index page (on hover)",
"CSS Modules Composes Ordering Development Mode should have correct color on index page (on load)",
"Ordering with Global CSS and Modules (dev) should have the correct color (css ordering)", "Ordering with Global CSS and Modules (dev) should have the correct color (css ordering)",
"Ordering with Global CSS and Modules (dev) should have the correct color (css ordering) during hot reloads", "Ordering with Global CSS and Modules (dev) should have the correct color (css ordering) during hot reloads",
"Ordering with Global CSS and Modules (dev) should not execute scripts in any order" "Ordering with Global CSS and Modules (dev) should not execute scripts in any order"
...@@ -7012,6 +7010,8 @@ ...@@ -7012,6 +7010,8 @@
"should handle unresolved files gracefully production mode should have correct file references in CSS output" "should handle unresolved files gracefully production mode should have correct file references in CSS output"
], ],
"flakey": [ "flakey": [
"CSS Modules Composes Ordering Development Mode should have correct color on index page (on hover)",
"CSS Modules Composes Ordering Development Mode should have correct color on index page (on load)",
"CSS Modules Composes Ordering Development Mode should have correct color on index page (on nav from index)", "CSS Modules Composes Ordering Development Mode should have correct color on index page (on nav from index)",
"CSS Modules Composes Ordering Development Mode should have correct color on index page (on nav from other)" "CSS Modules Composes Ordering Development Mode should have correct color on index page (on nav from other)"
], ],
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment