Unverified Commit 2df7cf60 authored by OJ Kwon's avatar OJ Kwon Committed by GitHub

feat(turbopack): construct output path with distDir next.js config (#55613)

### What?

This PR enables basic support for next.config.js's `distDir` config. PR have two main pieces, one for honoring distDir and construct output path other than `.next`, and secondly assign `process.env.__NEXT_DIST_DIR` to client / edge. The latter increased size of PR bit as had to downstream to the compile_define calls.

Corresponding tests are enabled to verify its behavior.

Closes WEB-1610
parent d9ca3cc6
...@@ -58,6 +58,10 @@ pub struct NapiProjectOptions { ...@@ -58,6 +58,10 @@ pub struct NapiProjectOptions {
/// A path inside the root_path which contains the app/pages directories. /// A path inside the root_path which contains the app/pages directories.
pub project_path: String, pub project_path: String,
/// next.config's distDir. Project initialization occurs eariler than
/// deserializing next.config, so passing it as separate option.
pub dist_dir: Option<String>,
/// Whether to watch he filesystem for file changes. /// Whether to watch he filesystem for file changes.
pub watch: bool, pub watch: bool,
...@@ -135,8 +139,12 @@ pub async fn project_new( ...@@ -135,8 +139,12 @@ pub async fn project_new(
let subscriber = Registry::default(); let subscriber = Registry::default();
let subscriber = subscriber.with(EnvFilter::builder().parse(trace).unwrap()); let subscriber = subscriber.with(EnvFilter::builder().parse(trace).unwrap());
let dist_dir = options
.dist_dir
.as_ref()
.map_or_else(|| ".next".to_string(), |d| d.to_string());
let internal_dir = PathBuf::from(&options.project_path).join(".next"); let internal_dir = PathBuf::from(&options.project_path).join(dist_dir);
std::fs::create_dir_all(&internal_dir) std::fs::create_dir_all(&internal_dir)
.context("Unable to create .next directory") .context("Unable to create .next directory")
.unwrap(); .unwrap();
......
...@@ -22,6 +22,11 @@ pub struct NextBuildContext { ...@@ -22,6 +22,11 @@ pub struct NextBuildContext {
/// The project's directory. /// The project's directory.
pub dir: Option<String>, pub dir: Option<String>,
/// next.config.js's distDir. Current there's some early stage setup
/// requires this Before construct a context to read next.config.js,
/// which we passes separately here.
pub dist_dir: Option<String>,
/// The build ID. /// The build ID.
pub build_id: Option<String>, pub build_id: Option<String>,
...@@ -47,6 +52,7 @@ impl TryFrom<NextBuildContext> for NextBuildOptions { ...@@ -47,6 +52,7 @@ impl TryFrom<NextBuildContext> for NextBuildOptions {
log_detail: true, log_detail: true,
full_stats: true, full_stats: true,
memory_limit: None, memory_limit: None,
dist_dir: value.dist_dir,
build_context: Some(BuildContext { build_context: Some(BuildContext {
build_id: value build_id: value
.build_id .build_id
......
...@@ -23,7 +23,7 @@ use turbo_tasks::{ ...@@ -23,7 +23,7 @@ use turbo_tasks::{
graph::{AdjacencyMap, GraphTraversal}, graph::{AdjacencyMap, GraphTraversal},
trace::TraceRawVcs, trace::TraceRawVcs,
Completion, Completions, IntoTraitRef, State, TaskInput, TransientInstance, TryFlatJoinIterExt, Completion, Completions, IntoTraitRef, State, TaskInput, TransientInstance, TryFlatJoinIterExt,
Value, Vc, Value, ValueToString, Vc,
}; };
use turbopack_binding::{ use turbopack_binding::{
turbo::{ turbo::{
...@@ -121,17 +121,41 @@ impl ProjectContainer { ...@@ -121,17 +121,41 @@ impl ProjectContainer {
#[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 options = this.options_state.get();
let next_config = NextConfig::from_string(Vc::cell(options.next_config.clone())); let (env, next_config, js_config, root_path, project_path, watch, server_addr) = {
let js_config = JsConfig::from_string(Vc::cell(options.js_config.clone())); 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 next_config = NextConfig::from_string(Vc::cell(options.next_config.clone()));
let js_config = JsConfig::from_string(Vc::cell(options.js_config.clone()));
let root_path = options.root_path.clone();
let project_path = options.project_path.clone();
let watch = options.watch;
let server_addr = options.server_addr.parse()?;
(
env,
next_config,
js_config,
root_path,
project_path,
watch,
server_addr,
)
};
let dist_dir = next_config
.await?
.dist_dir
.as_ref()
.map_or_else(|| ".next".to_string(), |d| d.to_string());
Ok(Project { Ok(Project {
root_path: options.root_path.clone(), root_path,
project_path: options.project_path.clone(), project_path,
watch: options.watch, watch,
server_addr: options.server_addr.parse()?, server_addr,
next_config, next_config,
js_config, js_config,
dist_dir,
env: Vc::upcast(env), env: Vc::upcast(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"
...@@ -161,6 +185,9 @@ pub struct Project { ...@@ -161,6 +185,9 @@ pub struct Project {
/// 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.
root_path: String, root_path: String,
/// A path where to emit the build outputs. next.config.js's distDir.
dist_dir: String,
/// A path inside the root_path which contains the app/pages directories. /// A path inside the root_path which contains the app/pages directories.
project_path: String, project_path: String,
...@@ -240,8 +267,9 @@ impl Project { ...@@ -240,8 +267,9 @@ impl Project {
} }
#[turbo_tasks::function] #[turbo_tasks::function]
pub(super) fn node_root(self: Vc<Self>) -> Vc<FileSystemPath> { pub(super) async fn node_root(self: Vc<Self>) -> Result<Vc<FileSystemPath>> {
self.node_fs().root().join(".next".to_string()) let this = self.await?;
Ok(self.node_fs().root().join(this.dist_dir.to_string()))
} }
#[turbo_tasks::function] #[turbo_tasks::function]
...@@ -254,6 +282,15 @@ impl Project { ...@@ -254,6 +282,15 @@ impl Project {
self.project_fs().root() self.project_fs().root()
} }
/// Returns a path to dist_dir resolved from project root path as string.
/// Currently this is only for embedding process.env.__NEXT_DIST_DIR.
/// [Note] in webpack-config, it is being injected when
/// dev && (isClient || isEdgeServer)
#[turbo_tasks::function]
async fn dist_root_string(self: Vc<Self>) -> Result<Vc<String>> {
Ok(self.node_root().to_string())
}
#[turbo_tasks::function] #[turbo_tasks::function]
pub(super) fn client_relative_path(self: Vc<Self>) -> Vc<FileSystemPath> { pub(super) fn client_relative_path(self: Vc<Self>) -> Vc<FileSystemPath> {
self.client_root().join("_next".to_string()) self.client_root().join("_next".to_string())
...@@ -309,8 +346,13 @@ impl Project { ...@@ -309,8 +346,13 @@ impl Project {
} }
#[turbo_tasks::function] #[turbo_tasks::function]
pub(super) fn client_compile_time_info(&self) -> Vc<CompileTimeInfo> { pub(super) async fn client_compile_time_info(self: Vc<Self>) -> Result<Vc<CompileTimeInfo>> {
get_client_compile_time_info(self.mode, self.browserslist_query.clone()) let this = self.await?;
Ok(get_client_compile_time_info(
this.mode,
this.browserslist_query.clone(),
self.dist_root_string(),
))
} }
#[turbo_tasks::function] #[turbo_tasks::function]
...@@ -325,7 +367,11 @@ impl Project { ...@@ -325,7 +367,11 @@ impl Project {
#[turbo_tasks::function] #[turbo_tasks::function]
pub(super) fn edge_compile_time_info(self: Vc<Self>) -> Vc<CompileTimeInfo> { pub(super) fn edge_compile_time_info(self: Vc<Self>) -> Vc<CompileTimeInfo> {
get_edge_compile_time_info(self.project_path(), self.server_addr()) get_edge_compile_time_info(
self.project_path(),
self.server_addr(),
self.dist_root_string(),
)
} }
#[turbo_tasks::function] #[turbo_tasks::function]
......
...@@ -10,6 +10,9 @@ pub struct BuildOptions { ...@@ -10,6 +10,9 @@ pub struct BuildOptions {
/// The project's directory. /// The project's directory.
pub dir: Option<PathBuf>, pub dir: Option<PathBuf>,
/// next.config.js's distDir.
pub dist_dir: Option<String>,
/// The maximum memory to use for the build. /// The maximum memory to use for the build.
pub memory_limit: Option<usize>, pub memory_limit: Option<usize>,
......
...@@ -26,6 +26,9 @@ pub struct BuildCliArgs { ...@@ -26,6 +26,9 @@ pub struct BuildCliArgs {
#[clap(long, value_parser)] #[clap(long, value_parser)]
pub root: Option<PathBuf>, pub root: Option<PathBuf>,
#[clap(long, value_parser)]
pub dist_dir: Option<String>,
/// Display version of the binary. Noop if used in library mode. /// Display version of the binary. Noop if used in library mode.
#[clap(long)] #[clap(long)]
pub display_version: bool, pub display_version: bool,
...@@ -100,6 +103,7 @@ async fn main_inner() -> Result<()> { ...@@ -100,6 +103,7 @@ async fn main_inner() -> Result<()> {
log_detail: args.log_detail, log_detail: args.log_detail,
full_stats: args.full_stats, full_stats: args.full_stats,
build_context: None, build_context: None,
dist_dir: args.dist_dir,
}) })
.await .await
} }
...@@ -25,7 +25,7 @@ use next_core::{ ...@@ -25,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, Vc, Completion, Completions, TransientInstance, TryJoinIterExt, ValueToString, Vc,
}; };
use turbopack_binding::{ use turbopack_binding::{
turbo::tasks_fs::{rebase, DiskFileSystem, FileContent, FileSystem, FileSystemPath}, turbo::tasks_fs::{rebase, DiskFileSystem, FileContent, FileSystem, FileSystemPath},
...@@ -90,12 +90,17 @@ pub(crate) async fn next_build(options: TransientInstance<BuildOptions>) -> Resu ...@@ -90,12 +90,17 @@ pub(crate) async fn next_build(options: TransientInstance<BuildOptions>) -> Resu
log_level: options.log_level.unwrap_or(IssueSeverity::Warning), log_level: options.log_level.unwrap_or(IssueSeverity::Warning),
}; };
let dist_dir = options
.dist_dir
.as_ref()
.map_or_else(|| ".next".to_string(), |d| d.to_string());
let issue_reporter: Vc<Box<dyn IssueReporter>> = let issue_reporter: Vc<Box<dyn IssueReporter>> =
Vc::upcast(ConsoleUi::new(TransientInstance::new(log_options))); Vc::upcast(ConsoleUi::new(TransientInstance::new(log_options)));
let node_fs = node_fs(project_root.clone(), issue_reporter); let node_fs = node_fs(project_root.clone(), issue_reporter);
let node_root = node_fs.root().join(".next".to_string()); let node_root = node_fs.root().join(dist_dir.clone());
let client_fs = client_fs(project_root.clone(), issue_reporter); let client_fs = client_fs(project_root.clone(), issue_reporter);
let client_root = client_fs.root().join(".next".to_string()); let client_root = client_fs.root().join(dist_dir);
// TODO(alexkirsz) This should accept a URL for assetPrefix. // TODO(alexkirsz) This should accept a URL for assetPrefix.
// let client_public_fs = VirtualFileSystem::new(); // let client_public_fs = VirtualFileSystem::new();
// let client_public_root = client_public_fs.root(); // let client_public_root = client_public_fs.root();
...@@ -127,7 +132,8 @@ pub(crate) async fn next_build(options: TransientInstance<BuildOptions>) -> Resu ...@@ -127,7 +132,8 @@ 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 client_compile_time_info = get_client_compile_time_info(mode, browserslist_query); let client_compile_time_info =
get_client_compile_time_info(mode, browserslist_query, node_root.to_string());
let server_compile_time_info = get_server_compile_time_info(mode, env, ServerAddr::empty()); 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.
......
...@@ -191,6 +191,7 @@ fn next_ssr_client_module_transition( ...@@ -191,6 +191,7 @@ fn next_ssr_client_module_transition(
#[turbo_tasks::function] #[turbo_tasks::function]
fn next_edge_ssr_client_module_transition( fn next_edge_ssr_client_module_transition(
project_path: Vc<FileSystemPath>, project_path: Vc<FileSystemPath>,
dist_root: Vc<String>,
execution_context: Vc<ExecutionContext>, execution_context: Vc<ExecutionContext>,
app_dir: Vc<FileSystemPath>, app_dir: Vc<FileSystemPath>,
next_config: Vc<NextConfig>, next_config: Vc<NextConfig>,
...@@ -214,7 +215,7 @@ fn next_edge_ssr_client_module_transition( ...@@ -214,7 +215,7 @@ fn next_edge_ssr_client_module_transition(
next_config, next_config,
execution_context, execution_context,
), ),
ssr_environment: get_edge_compile_time_info(project_path, server_addr), ssr_environment: get_edge_compile_time_info(project_path, server_addr, dist_root),
} }
.cell(), .cell(),
) )
...@@ -290,6 +291,7 @@ fn next_route_transition( ...@@ -290,6 +291,7 @@ fn next_route_transition(
#[turbo_tasks::function] #[turbo_tasks::function]
fn next_edge_server_component_transition( fn next_edge_server_component_transition(
project_path: Vc<FileSystemPath>, project_path: Vc<FileSystemPath>,
dist_root: Vc<String>,
execution_context: Vc<ExecutionContext>, execution_context: Vc<ExecutionContext>,
app_dir: Vc<FileSystemPath>, app_dir: Vc<FileSystemPath>,
server_root: Vc<FileSystemPath>, server_root: Vc<FileSystemPath>,
...@@ -305,7 +307,7 @@ fn next_edge_server_component_transition( ...@@ -305,7 +307,7 @@ fn next_edge_server_component_transition(
ecmascript_client_reference_transition_name, ecmascript_client_reference_transition_name,
), ),
}); });
let rsc_compile_time_info = get_edge_compile_time_info(project_path, server_addr); let rsc_compile_time_info = get_edge_compile_time_info(project_path, server_addr, dist_root);
let rsc_resolve_options_context = let rsc_resolve_options_context =
get_edge_resolve_options_context(project_path, ty, mode, next_config, execution_context); get_edge_resolve_options_context(project_path, ty, mode, next_config, execution_context);
let rsc_module_options_context = let rsc_module_options_context =
...@@ -325,6 +327,7 @@ fn next_edge_server_component_transition( ...@@ -325,6 +327,7 @@ fn next_edge_server_component_transition(
#[turbo_tasks::function] #[turbo_tasks::function]
fn next_edge_route_transition( fn next_edge_route_transition(
project_path: Vc<FileSystemPath>, project_path: Vc<FileSystemPath>,
dist_root: Vc<String>,
app_dir: Vc<FileSystemPath>, app_dir: Vc<FileSystemPath>,
server_root: Vc<FileSystemPath>, server_root: Vc<FileSystemPath>,
next_config: Vc<NextConfig>, next_config: Vc<NextConfig>,
...@@ -335,7 +338,7 @@ fn next_edge_route_transition( ...@@ -335,7 +338,7 @@ fn next_edge_route_transition(
let mode = NextMode::DevServer; let mode = NextMode::DevServer;
let server_ty = Value::new(ServerContextType::AppRoute { app_dir }); let server_ty = Value::new(ServerContextType::AppRoute { app_dir });
let edge_compile_time_info = get_edge_compile_time_info(project_path, server_addr); let edge_compile_time_info = get_edge_compile_time_info(project_path, server_addr, dist_root);
let edge_chunking_context = Vc::upcast( let edge_chunking_context = Vc::upcast(
DevChunkingContext::builder( DevChunkingContext::builder(
...@@ -374,6 +377,7 @@ fn next_edge_route_transition( ...@@ -374,6 +377,7 @@ fn next_edge_route_transition(
#[turbo_tasks::function] #[turbo_tasks::function]
fn next_edge_page_transition( fn next_edge_page_transition(
project_path: Vc<FileSystemPath>, project_path: Vc<FileSystemPath>,
dist_root: Vc<String>,
app_dir: Vc<FileSystemPath>, app_dir: Vc<FileSystemPath>,
server_root: Vc<FileSystemPath>, server_root: Vc<FileSystemPath>,
mode: NextMode, mode: NextMode,
...@@ -384,7 +388,7 @@ fn next_edge_page_transition( ...@@ -384,7 +388,7 @@ fn next_edge_page_transition(
) -> Vc<Box<dyn Transition>> { ) -> Vc<Box<dyn Transition>> {
let server_ty = Value::new(ServerContextType::AppSSR { app_dir }); let server_ty = Value::new(ServerContextType::AppSSR { app_dir });
let edge_compile_time_info = get_edge_compile_time_info(project_path, server_addr); let edge_compile_time_info = get_edge_compile_time_info(project_path, server_addr, dist_root);
let edge_chunking_context = Vc::upcast( let edge_chunking_context = Vc::upcast(
DevChunkingContext::builder( DevChunkingContext::builder(
...@@ -422,6 +426,7 @@ fn next_edge_page_transition( ...@@ -422,6 +426,7 @@ fn next_edge_page_transition(
#[turbo_tasks::function] #[turbo_tasks::function]
fn app_context( fn app_context(
project_path: Vc<FileSystemPath>, project_path: Vc<FileSystemPath>,
dist_root: Vc<String>,
execution_context: Vc<ExecutionContext>, execution_context: Vc<ExecutionContext>,
server_root: Vc<FileSystemPath>, server_root: Vc<FileSystemPath>,
app_dir: Vc<FileSystemPath>, app_dir: Vc<FileSystemPath>,
...@@ -439,6 +444,7 @@ fn app_context( ...@@ -439,6 +444,7 @@ fn app_context(
"next-edge-route".to_string(), "next-edge-route".to_string(),
next_edge_route_transition( next_edge_route_transition(
project_path, project_path,
dist_root,
app_dir, app_dir,
server_root, server_root,
next_config, next_config,
...@@ -462,6 +468,7 @@ fn app_context( ...@@ -462,6 +468,7 @@ fn app_context(
"next-edge-page".to_string(), "next-edge-page".to_string(),
next_edge_page_transition( next_edge_page_transition(
project_path, project_path,
dist_root,
app_dir, app_dir,
server_root, server_root,
mode, mode,
...@@ -494,6 +501,7 @@ fn app_context( ...@@ -494,6 +501,7 @@ fn app_context(
"next-edge-server-component".to_string(), "next-edge-server-component".to_string(),
next_edge_server_component_transition( next_edge_server_component_transition(
project_path, project_path,
dist_root,
execution_context, execution_context,
app_dir, app_dir,
server_root, server_root,
...@@ -547,6 +555,7 @@ fn app_context( ...@@ -547,6 +555,7 @@ fn app_context(
"next-edge-ssr-client-module".to_string(), "next-edge-ssr-client-module".to_string(),
next_edge_ssr_client_module_transition( next_edge_ssr_client_module_transition(
project_path, project_path,
dist_root,
execution_context, execution_context,
app_dir, app_dir,
next_config, next_config,
...@@ -580,6 +589,7 @@ fn app_context( ...@@ -580,6 +589,7 @@ fn app_context(
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn create_app_source( pub async fn create_app_source(
app_dir: Vc<OptionAppDir>, app_dir: Vc<OptionAppDir>,
dist_root: Vc<String>,
project_path: Vc<FileSystemPath>, project_path: Vc<FileSystemPath>,
execution_context: Vc<ExecutionContext>, execution_context: Vc<ExecutionContext>,
output_path: Vc<FileSystemPath>, output_path: Vc<FileSystemPath>,
...@@ -599,6 +609,7 @@ pub async fn create_app_source( ...@@ -599,6 +609,7 @@ pub async fn create_app_source(
let context_ssr = app_context( let context_ssr = app_context(
project_path, project_path,
dist_root,
execution_context, execution_context,
server_root, server_root,
app_dir, app_dir,
...@@ -613,6 +624,7 @@ pub async fn create_app_source( ...@@ -613,6 +624,7 @@ pub async fn create_app_source(
); );
let context = app_context( let context = app_context(
project_path, project_path,
dist_root,
execution_context, execution_context,
server_root, server_root,
app_dir, app_dir,
......
...@@ -65,27 +65,50 @@ use crate::{ ...@@ -65,27 +65,50 @@ use crate::{
util::foreign_code_context_condition, util::foreign_code_context_condition,
}; };
fn defines(mode: NextMode) -> CompileTimeDefines { fn defines(mode: NextMode, dist_root_path: Option<&str>) -> CompileTimeDefines {
compile_time_defines!( // [TODO] macro may need to allow dynamically expand from some iterable values
let mut defines = compile_time_defines!(
process.turbopack = true, process.turbopack = true,
process.env.NODE_ENV = mode.node_env(), process.env.NODE_ENV = mode.node_env(),
process.env.__NEXT_CLIENT_ROUTER_FILTER_ENABLED = false, process.env.__NEXT_CLIENT_ROUTER_FILTER_ENABLED = false,
process.env.__NEXT_HAS_REWRITES = true, process.env.__NEXT_HAS_REWRITES = true,
process.env.__NEXT_I18N_SUPPORT = false, process.env.__NEXT_I18N_SUPPORT = false,
) );
if let Some(dist_root_path) = dist_root_path {
defines.0.insert(
vec![
"process".to_string(),
"env".to_string(),
"__NEXT_DIST_DIR".to_string(),
],
dist_root_path.to_string().into(),
);
}
// TODO(WEB-937) there are more defines needed, see // TODO(WEB-937) there are more defines needed, see
// packages/next/src/build/webpack-config.ts // packages/next/src/build/webpack-config.ts
defines
} }
#[turbo_tasks::function] #[turbo_tasks::function]
fn next_client_defines(mode: NextMode) -> Vc<CompileTimeDefines> { async fn next_client_defines(
defines(mode).cell() mode: NextMode,
dist_root_path: Vc<String>,
) -> Result<Vc<CompileTimeDefines>> {
let dist_root_path = &*dist_root_path.await?;
Ok(defines(mode, Some(dist_root_path.as_str())).cell())
} }
#[turbo_tasks::function] #[turbo_tasks::function]
async fn next_client_free_vars(mode: NextMode) -> Result<Vc<FreeVarReferences>> { async fn next_client_free_vars(
mode: NextMode,
dist_root_path: Vc<String>,
) -> Result<Vc<FreeVarReferences>> {
let dist_root_path = &*dist_root_path.await?;
Ok(free_var_references!( Ok(free_var_references!(
..defines(mode).into_iter(), ..defines(mode, Some(dist_root_path.as_str())).into_iter(),
Buffer = FreeVarReference::EcmaScriptModule { Buffer = FreeVarReference::EcmaScriptModule {
request: "node:buffer".to_string(), request: "node:buffer".to_string(),
lookup_path: None, lookup_path: None,
...@@ -104,6 +127,7 @@ async fn next_client_free_vars(mode: NextMode) -> Result<Vc<FreeVarReferences>> ...@@ -104,6 +127,7 @@ async fn next_client_free_vars(mode: NextMode) -> Result<Vc<FreeVarReferences>>
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>,
) -> Vc<CompileTimeInfo> { ) -> Vc<CompileTimeInfo> {
CompileTimeInfo::builder(Environment::new(Value::new(ExecutionEnvironment::Browser( CompileTimeInfo::builder(Environment::new(Value::new(ExecutionEnvironment::Browser(
BrowserEnvironment { BrowserEnvironment {
...@@ -114,8 +138,8 @@ pub fn get_client_compile_time_info( ...@@ -114,8 +138,8 @@ pub fn get_client_compile_time_info(
} }
.into(), .into(),
)))) ))))
.defines(next_client_defines(mode)) .defines(next_client_defines(mode, dist_root_path))
.free_var_references(next_client_free_vars(mode)) .free_var_references(next_client_free_vars(mode, dist_root_path))
.cell() .cell()
} }
......
...@@ -83,6 +83,7 @@ pub struct NextConfig { ...@@ -83,6 +83,7 @@ pub struct NextConfig {
pub react_strict_mode: Option<bool>, pub react_strict_mode: Option<bool>,
pub transpile_packages: Option<Vec<String>>, pub transpile_packages: Option<Vec<String>>,
pub modularize_imports: Option<IndexMap<String, ModularizeImportPackageConfig>>, pub modularize_imports: Option<IndexMap<String, ModularizeImportPackageConfig>>,
pub dist_dir: Option<String>,
sass_options: Option<serde_json::Value>, sass_options: Option<serde_json::Value>,
trailing_slash: bool, trailing_slash: bool,
...@@ -100,7 +101,6 @@ pub struct NextConfig { ...@@ -100,7 +101,6 @@ pub struct NextConfig {
clean_dist_dir: bool, clean_dist_dir: bool,
compress: bool, compress: bool,
dev_indicators: DevIndicatorsConfig, dev_indicators: DevIndicatorsConfig,
dist_dir: String,
eslint: EslintConfig, eslint: EslintConfig,
exclude_default_moment_locales: bool, exclude_default_moment_locales: bool,
// this can be a function in js land // this can be a function in js land
......
...@@ -31,26 +31,46 @@ use crate::{ ...@@ -31,26 +31,46 @@ use crate::{
util::foreign_code_context_condition, util::foreign_code_context_condition,
}; };
fn defines() -> CompileTimeDefines { fn defines(dist_root_path: Option<&str>) -> CompileTimeDefines {
compile_time_defines!( // [TODO] macro may need to allow dynamically expand from some iterable values
let mut defines = compile_time_defines!(
process.turbopack = true, process.turbopack = true,
process.env.NODE_ENV = "development", process.env.NODE_ENV = "development",
process.env.__NEXT_CLIENT_ROUTER_FILTER_ENABLED = false, process.env.__NEXT_CLIENT_ROUTER_FILTER_ENABLED = false,
process.env.NEXT_RUNTIME = "edge" process.env.NEXT_RUNTIME = "edge",
) );
if let Some(dist_root_path) = dist_root_path {
defines.0.insert(
vec![
"process".to_string(),
"env".to_string(),
"__NEXT_DIST_DIR".to_string(),
],
dist_root_path.to_string().into(),
);
}
// TODO(WEB-937) there are more defines needed, see // TODO(WEB-937) there are more defines needed, see
// packages/next/src/build/webpack-config.ts // packages/next/src/build/webpack-config.ts
defines
} }
#[turbo_tasks::function] #[turbo_tasks::function]
fn next_edge_defines() -> Vc<CompileTimeDefines> { async fn next_edge_defines(dist_root_path: Vc<String>) -> Result<Vc<CompileTimeDefines>> {
defines().cell() let dist_root_path = &*dist_root_path.await?;
Ok(defines(Some(dist_root_path.as_str())).cell())
} }
#[turbo_tasks::function] #[turbo_tasks::function]
fn next_edge_free_vars(project_path: Vc<FileSystemPath>) -> Vc<FreeVarReferences> { async fn next_edge_free_vars(
free_var_references!( project_path: Vc<FileSystemPath>,
..defines().into_iter(), dist_root_path: Vc<String>,
) -> Result<Vc<FreeVarReferences>> {
let dist_root_path = &*dist_root_path.await?;
Ok(free_var_references!(
..defines(Some(dist_root_path.as_str())).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),
...@@ -62,19 +82,20 @@ fn next_edge_free_vars(project_path: Vc<FileSystemPath>) -> Vc<FreeVarReferences ...@@ -62,19 +82,20 @@ fn next_edge_free_vars(project_path: Vc<FileSystemPath>) -> Vc<FreeVarReferences
export: Some("default".to_string()), export: Some("default".to_string()),
}, },
) )
.cell() .cell())
} }
#[turbo_tasks::function] #[turbo_tasks::function]
pub fn get_edge_compile_time_info( pub fn get_edge_compile_time_info(
project_path: Vc<FileSystemPath>, project_path: Vc<FileSystemPath>,
server_addr: Vc<ServerAddr>, server_addr: Vc<ServerAddr>,
dist_root_path: Vc<String>,
) -> 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()) .defines(next_edge_defines(dist_root_path))
.free_var_references(next_edge_free_vars(project_path)) .free_var_references(next_edge_free_vars(project_path, dist_root_path))
.cell() .cell()
} }
......
...@@ -82,6 +82,7 @@ use crate::{ ...@@ -82,6 +82,7 @@ use crate::{
pub async fn create_page_source( pub async fn create_page_source(
pages_structure: Vc<PagesStructure>, pages_structure: Vc<PagesStructure>,
project_root: Vc<FileSystemPath>, project_root: Vc<FileSystemPath>,
dist_root: Vc<String>,
execution_context: Vc<ExecutionContext>, execution_context: Vc<ExecutionContext>,
node_root: Vc<FileSystemPath>, node_root: Vc<FileSystemPath>,
client_root: Vc<FileSystemPath>, client_root: Vc<FileSystemPath>,
...@@ -139,7 +140,7 @@ pub async fn create_page_source( ...@@ -139,7 +140,7 @@ pub async fn create_page_source(
.cell(), .cell(),
); );
let edge_compile_time_info = get_edge_compile_time_info(project_root, server_addr); let edge_compile_time_info = get_edge_compile_time_info(project_root, server_addr, dist_root);
let edge_chunking_context = Vc::upcast( let edge_chunking_context = Vc::upcast(
DevChunkingContext::builder( DevChunkingContext::builder(
......
...@@ -213,6 +213,7 @@ fn route_executor( ...@@ -213,6 +213,7 @@ fn route_executor(
#[turbo_tasks::function] #[turbo_tasks::function]
fn edge_transition_map( fn edge_transition_map(
server_addr: Vc<ServerAddr>, server_addr: Vc<ServerAddr>,
dist_root: Vc<String>,
project_path: Vc<FileSystemPath>, project_path: Vc<FileSystemPath>,
output_path: Vc<FileSystemPath>, output_path: Vc<FileSystemPath>,
next_config: Vc<NextConfig>, next_config: Vc<NextConfig>,
...@@ -220,7 +221,7 @@ fn edge_transition_map( ...@@ -220,7 +221,7 @@ fn edge_transition_map(
) -> Vc<TransitionsByName> { ) -> Vc<TransitionsByName> {
let mode = NextMode::DevServer; let mode = NextMode::DevServer;
let edge_compile_time_info = get_edge_compile_time_info(project_path, server_addr); let edge_compile_time_info = get_edge_compile_time_info(project_path, server_addr, dist_root);
let edge_chunking_context = Vc::upcast( let edge_chunking_context = Vc::upcast(
DevChunkingContext::builder( DevChunkingContext::builder(
...@@ -275,6 +276,7 @@ fn edge_transition_map( ...@@ -275,6 +276,7 @@ fn edge_transition_map(
pub async fn route( pub async fn route(
execution_context: Vc<ExecutionContext>, execution_context: Vc<ExecutionContext>,
request: Vc<RouterRequest>, request: Vc<RouterRequest>,
dist_root: Vc<String>,
next_config: Vc<NextConfig>, next_config: Vc<NextConfig>,
server_addr: Vc<ServerAddr>, server_addr: Vc<ServerAddr>,
routes_changed: Vc<Completion>, routes_changed: Vc<Completion>,
...@@ -287,6 +289,7 @@ pub async fn route( ...@@ -287,6 +289,7 @@ pub async fn route(
route_internal( route_internal(
execution_context, execution_context,
request, request,
dist_root,
next_config, next_config,
server_addr, server_addr,
routes_changed, routes_changed,
...@@ -311,6 +314,7 @@ macro_rules! shared_anyhow { ...@@ -311,6 +314,7 @@ macro_rules! shared_anyhow {
async fn route_internal( async fn route_internal(
execution_context: Vc<ExecutionContext>, execution_context: Vc<ExecutionContext>,
request: Vc<RouterRequest>, request: Vc<RouterRequest>,
dist_root: Vc<String>,
next_config: Vc<NextConfig>, next_config: Vc<NextConfig>,
server_addr: Vc<ServerAddr>, server_addr: Vc<ServerAddr>,
routes_changed: Vc<Completion>, routes_changed: Vc<Completion>,
...@@ -326,6 +330,7 @@ async fn route_internal( ...@@ -326,6 +330,7 @@ async fn route_internal(
Some(get_next_build_import_map()), Some(get_next_build_import_map()),
Some(edge_transition_map( Some(edge_transition_map(
server_addr, server_addr,
dist_root,
project_path, project_path,
chunking_context.output_root(), chunking_context.output_root(),
next_config, next_config,
......
...@@ -31,6 +31,7 @@ pub struct NextRouterContentSource { ...@@ -31,6 +31,7 @@ pub struct NextRouterContentSource {
server_addr: Vc<ServerAddr>, server_addr: Vc<ServerAddr>,
app_dir: Vc<OptionAppDir>, app_dir: Vc<OptionAppDir>,
pages_structure: Vc<PagesStructure>, pages_structure: Vc<PagesStructure>,
dist_root: Vc<String>,
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
...@@ -43,6 +44,7 @@ impl NextRouterContentSource { ...@@ -43,6 +44,7 @@ impl NextRouterContentSource {
server_addr: Vc<ServerAddr>, server_addr: Vc<ServerAddr>,
app_dir: Vc<OptionAppDir>, app_dir: Vc<OptionAppDir>,
pages_structure: Vc<PagesStructure>, pages_structure: Vc<PagesStructure>,
dist_root: Vc<String>,
) -> Vc<NextRouterContentSource> { ) -> Vc<NextRouterContentSource> {
NextRouterContentSource { NextRouterContentSource {
inner, inner,
...@@ -51,6 +53,7 @@ impl NextRouterContentSource { ...@@ -51,6 +53,7 @@ impl NextRouterContentSource {
server_addr, server_addr,
app_dir, app_dir,
pages_structure, pages_structure,
dist_root,
} }
.cell() .cell()
} }
...@@ -144,6 +147,7 @@ impl GetContentSourceContent for NextRouterContentSource { ...@@ -144,6 +147,7 @@ impl GetContentSourceContent for NextRouterContentSource {
let res = route( let res = route(
this.execution_context, this.execution_context,
request, request,
this.dist_root,
this.next_config, this.next_config,
this.server_addr, this.server_addr,
routes_changed(this.app_dir, this.pages_structure, this.next_config), routes_changed(this.app_dir, this.pages_structure, this.next_config),
......
...@@ -40,7 +40,7 @@ use owo_colors::OwoColorize; ...@@ -40,7 +40,7 @@ use owo_colors::OwoColorize;
use tracing_subscriber::{prelude::*, EnvFilter, Registry}; use tracing_subscriber::{prelude::*, EnvFilter, Registry};
use turbo_tasks::{ use turbo_tasks::{
util::FormatDuration, StatsType, TransientInstance, TurboTasks, TurboTasksBackendApi, util::FormatDuration, StatsType, TransientInstance, TurboTasks, TurboTasksBackendApi,
UpdateInfo, Value, Vc, UpdateInfo, Value, ValueToString, Vc,
}; };
use turbopack_binding::{ use turbopack_binding::{
turbo::{ turbo::{
...@@ -318,9 +318,17 @@ async fn source( ...@@ -318,9 +318,17 @@ async fn source(
let server_addr = ServerAddr::new(*server_addr).cell(); let server_addr = ServerAddr::new(*server_addr).cell();
// [Note]: this is to conform changed signature to inject process.env.__NEXT_DIST_DIR,
// while we don't use this devserver for now
let dist_dir = ".next".to_string();
let env = load_env(project_path); let env = load_env(project_path);
let env = server_env(env, server_addr); let env = server_env(env, server_addr);
let build_output_root = output_fs.root().join(".next/build".to_string()); let build_output_root = output_fs
.root()
.join(dist_dir.clone())
.join("build".to_string());
let dist_root = output_fs.root().join(dist_dir.clone()).to_string();
let build_chunking_context = DevChunkingContext::builder( let build_chunking_context = DevChunkingContext::builder(
project_path, project_path,
...@@ -339,7 +347,10 @@ async fn source( ...@@ -339,7 +347,10 @@ async fn source(
let next_config = load_next_config(next_config_execution_context); let next_config = load_next_config(next_config_execution_context);
let rewrites = load_rewrites(next_config_execution_context); let rewrites = load_rewrites(next_config_execution_context);
let output_root = output_fs.root().join(".next/server".to_string()); let output_root = output_fs
.root()
.join(dist_dir.clone())
.join("server".to_string());
let dev_server_fs = Vc::upcast::<Box<dyn FileSystem>>(ServerFileSystem::new()); let dev_server_fs = Vc::upcast::<Box<dyn FileSystem>>(ServerFileSystem::new());
let dev_server_root = dev_server_fs.root(); let dev_server_root = dev_server_fs.root();
...@@ -364,7 +375,8 @@ async fn source( ...@@ -364,7 +375,8 @@ async fn source(
browserslist_query.clone(), browserslist_query.clone(),
next_config, next_config,
); );
let client_compile_time_info = get_client_compile_time_info(mode, browserslist_query); let client_compile_time_info =
get_client_compile_time_info(mode, browserslist_query, dist_root);
let client_chunking_context = get_client_chunking_context( let client_chunking_context = get_client_chunking_context(
project_path, project_path,
dev_server_root, dev_server_root,
...@@ -376,6 +388,7 @@ async fn source( ...@@ -376,6 +388,7 @@ async fn source(
let page_source = create_page_source( let page_source = create_page_source(
pages_structure, pages_structure,
project_path, project_path,
dist_root,
execution_context, execution_context,
output_root.join("pages".to_string()), output_root.join("pages".to_string()),
dev_server_root, dev_server_root,
...@@ -388,6 +401,7 @@ async fn source( ...@@ -388,6 +401,7 @@ async fn source(
let app_dir = find_app_dir_if_enabled(project_path); let app_dir = find_app_dir_if_enabled(project_path);
let app_source = create_app_source( let app_source = create_app_source(
app_dir, app_dir,
dist_root,
project_path, project_path,
execution_context, execution_context,
output_root.join("app".to_string()), output_root.join("app".to_string()),
...@@ -433,6 +447,7 @@ async fn source( ...@@ -433,6 +447,7 @@ async fn source(
server_addr, server_addr,
app_dir, app_dir,
pages_structure, pages_structure,
dist_root,
)); ));
let source = Vc::upcast( let source = Vc::upcast(
PrefixedRouterContentSource { PrefixedRouterContentSource {
......
...@@ -1012,6 +1012,7 @@ export default async function build( ...@@ -1012,6 +1012,7 @@ export default async function build(
await binding.turbo.nextBuild({ await binding.turbo.nextBuild({
...NextBuildContext, ...NextBuildContext,
root, root,
distDir: config.distDir,
}) })
const [duration] = process.hrtime(turboNextBuildStart) const [duration] = process.hrtime(turboNextBuildStart)
......
...@@ -29,6 +29,7 @@ const supportedTurbopackNextConfigOptions = [ ...@@ -29,6 +29,7 @@ const supportedTurbopackNextConfigOptions = [
'useFileSystemPublicRoutes', 'useFileSystemPublicRoutes',
'generateEtags', 'generateEtags',
'assetPrefix', 'assetPrefix',
'distDir',
'experimental.serverComponentsExternalPackages', 'experimental.serverComponentsExternalPackages',
'experimental.turbo', 'experimental.turbo',
'experimental.mdxRs', 'experimental.mdxRs',
......
...@@ -30,7 +30,8 @@ const apiJson = new File(join(appDir, 'app/api/json/route.js')) ...@@ -30,7 +30,8 @@ const apiJson = new File(join(appDir, 'app/api/json/route.js'))
export const expectedWhenTrailingSlashTrue = [ export const expectedWhenTrailingSlashTrue = [
'404.html', '404.html',
'404/index.html', '404/index.html',
'_next/static/media/test.3f1a293b.png', // Turbopack and plain next.js have different hash output for the file name
expect.stringMatching(/_next\/static\/media\/test\.[0-9a-f]+\.png/),
'_next/static/test-build-id/_buildManifest.js', '_next/static/test-build-id/_buildManifest.js',
'_next/static/test-build-id/_ssgManifest.js', '_next/static/test-build-id/_ssgManifest.js',
'another/first/index.html', 'another/first/index.html',
...@@ -53,7 +54,7 @@ export const expectedWhenTrailingSlashTrue = [ ...@@ -53,7 +54,7 @@ export const expectedWhenTrailingSlashTrue = [
const expectedWhenTrailingSlashFalse = [ const expectedWhenTrailingSlashFalse = [
'404.html', '404.html',
'_next/static/media/test.3f1a293b.png', expect.stringMatching(/_next\/static\/media\/test\.[0-9a-f]+\.png/),
'_next/static/test-build-id/_buildManifest.js', '_next/static/test-build-id/_buildManifest.js',
'_next/static/test-build-id/_ssgManifest.js', '_next/static/test-build-id/_ssgManifest.js',
'another.html', 'another.html',
......
...@@ -2,13 +2,14 @@ ...@@ -2,13 +2,14 @@
import fs from 'fs-extra' import fs from 'fs-extra'
import { join } from 'path' import { join } from 'path'
import { BUILD_ID_FILE } from 'next/constants' import { BUILD_ID_FILE, BUILD_MANIFEST } from 'next/constants'
import { import {
nextStart, nextStart,
nextBuild, nextBuild,
findPort, findPort,
killApp, killApp,
renderViaHTTP, renderViaHTTP,
launchApp,
} from 'next-test-utils' } from 'next-test-utils'
const appDir = join(__dirname, '../') const appDir = join(__dirname, '../')
...@@ -42,6 +43,30 @@ describe('distDir', () => { ...@@ -42,6 +43,30 @@ describe('distDir', () => {
}) })
}) })
describe('dev mode', () => {
beforeAll(async () => {
await fs.remove(join(appDir, '.next'))
await fs.remove(join(appDir, 'dist'))
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(() => killApp(app))
it('should render the page', async () => {
const html = await renderViaHTTP(appPort, '/')
expect(html).toMatch(/Hello World/)
})
it('should build the app within the given `dist` directory', async () => {
expect(
await fs.exists(join(__dirname, `/../dist/${BUILD_MANIFEST}`))
).toBeTruthy()
})
it('should not build the app within the default `.next` directory', async () => {
expect(await fs.exists(join(__dirname, '/../.next'))).toBeFalsy()
})
})
it('should throw error with invalid distDir', async () => { it('should throw error with invalid distDir', async () => {
const origNextConfig = await fs.readFile(nextConfig, 'utf8') const origNextConfig = await fs.readFile(nextConfig, 'utf8')
await fs.writeFile(nextConfig, `module.exports = { distDir: '' }`) await fs.writeFile(nextConfig, `module.exports = { distDir: '' }`)
......
...@@ -89,6 +89,7 @@ const enabledTests = [ ...@@ -89,6 +89,7 @@ const enabledTests = [
'test/e2e/typescript-version-warning/typescript-version-warning.test.ts', 'test/e2e/typescript-version-warning/typescript-version-warning.test.ts',
'test/e2e/undici-fetch/index.test.ts', 'test/e2e/undici-fetch/index.test.ts',
'test/integration/api-support/test/index.test.js', 'test/integration/api-support/test/index.test.js',
'test/integration/app-dir-export/test/config.test.ts',
'test/integration/404-page/test/index.test.js', 'test/integration/404-page/test/index.test.js',
'test/integration/404-page-app/test/index.test.js', 'test/integration/404-page-app/test/index.test.js',
'test/integration/404-page-custom-error/test/index.test.js', 'test/integration/404-page-custom-error/test/index.test.js',
...@@ -96,6 +97,7 @@ const enabledTests = [ ...@@ -96,6 +97,7 @@ const enabledTests = [
'test/integration/create-next-app/templates-app.test.ts', 'test/integration/create-next-app/templates-app.test.ts',
'test/integration/create-next-app/templates-pages.test.ts', 'test/integration/create-next-app/templates-pages.test.ts',
'test/integration/custom-routes-i18n-index-redirect/test/index.test.js', 'test/integration/custom-routes-i18n-index-redirect/test/index.test.js',
'test/integration/dist-dir/test/index.test.js',
'test/integration/index-index/test/index.test.js', 'test/integration/index-index/test/index.test.js',
'test/integration/next-image-new/middleware/test/index.test.ts', 'test/integration/next-image-new/middleware/test/index.test.ts',
'test/integration/next-image-new/react-virtualized/test/index.test.ts', 'test/integration/next-image-new/react-virtualized/test/index.test.ts',
...@@ -113,6 +115,7 @@ const enabledTests = [ ...@@ -113,6 +115,7 @@ const enabledTests = [
'test/integration/ssg-dynamic-routes-404-page/test/index.test.js', 'test/integration/ssg-dynamic-routes-404-page/test/index.test.js',
'test/integration/static-404/test/index.test.js', 'test/integration/static-404/test/index.test.js',
'test/integration/static-page-name/test/index.test.js', 'test/integration/static-page-name/test/index.test.js',
'test/integration/trailing-slash-dist/test/index.test.js',
'test/integration/trailing-slashes-href-resolving/test/index.test.js', 'test/integration/trailing-slashes-href-resolving/test/index.test.js',
'test/production/app-dir-hide-suppressed-error-during-next-export/index.test.ts', 'test/production/app-dir-hide-suppressed-error-during-next-export/index.test.ts',
'test/production/app-dir-prefetch-non-iso-url/index.test.ts', 'test/production/app-dir-prefetch-non-iso-url/index.test.ts',
...@@ -121,9 +124,6 @@ const enabledTests = [ ...@@ -121,9 +124,6 @@ const enabledTests = [
'test/production/jest/rsc/lib/utils.test.js', 'test/production/jest/rsc/lib/utils.test.js',
'test/production/jest/transpile-packages.test.ts', 'test/production/jest/transpile-packages.test.ts',
'test/production/postcss-plugin-config-as-string/index.test.ts', 'test/production/postcss-plugin-config-as-string/index.test.ts',
// disabled due to nextConfig.distDir
//'test/integration/trailing-slash-dist/test/index.test.js',
] ]
/// Naive check to ensure that the enabled tests actually exist /// Naive check to ensure that the enabled tests actually exist
......
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