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 {
/// A path inside the root_path which contains the app/pages directories.
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.
pub watch: bool,
......@@ -135,8 +139,12 @@ pub async fn project_new(
let subscriber = Registry::default();
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)
.context("Unable to create .next directory")
.unwrap();
......
......@@ -22,6 +22,11 @@ pub struct NextBuildContext {
/// The project's directory.
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.
pub build_id: Option<String>,
......@@ -47,6 +52,7 @@ impl TryFrom<NextBuildContext> for NextBuildOptions {
log_detail: true,
full_stats: true,
memory_limit: None,
dist_dir: value.dist_dir,
build_context: Some(BuildContext {
build_id: value
.build_id
......
......@@ -23,7 +23,7 @@ use turbo_tasks::{
graph::{AdjacencyMap, GraphTraversal},
trace::TraceRawVcs,
Completion, Completions, IntoTraitRef, State, TaskInput, TransientInstance, TryFlatJoinIterExt,
Value, Vc,
Value, ValueToString, Vc,
};
use turbopack_binding::{
turbo::{
......@@ -121,17 +121,41 @@ impl ProjectContainer {
#[turbo_tasks::function]
pub async fn project(self: Vc<Self>) -> Result<Vc<Project>> {
let this = self.await?;
let options = this.options_state.get();
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 env: Vc<EnvMap> = Vc::cell(options.env.iter().cloned().collect());
let (env, next_config, js_config, root_path, project_path, watch, server_addr) = {
let options = this.options_state.get();
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 {
root_path: options.root_path.clone(),
project_path: options.project_path.clone(),
watch: options.watch,
server_addr: options.server_addr.parse()?,
root_path,
project_path,
watch,
server_addr,
next_config,
js_config,
dist_dir,
env: Vc::upcast(env),
browserslist_query: "last 1 Chrome versions, last 1 Firefox versions, last 1 Safari \
versions, last 1 Edge versions"
......@@ -161,6 +185,9 @@ pub struct Project {
/// a file outside this root will fail. Think of this as a chroot.
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.
project_path: String,
......@@ -240,8 +267,9 @@ impl Project {
}
#[turbo_tasks::function]
pub(super) fn node_root(self: Vc<Self>) -> Vc<FileSystemPath> {
self.node_fs().root().join(".next".to_string())
pub(super) async fn node_root(self: Vc<Self>) -> Result<Vc<FileSystemPath>> {
let this = self.await?;
Ok(self.node_fs().root().join(this.dist_dir.to_string()))
}
#[turbo_tasks::function]
......@@ -254,6 +282,15 @@ impl Project {
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]
pub(super) fn client_relative_path(self: Vc<Self>) -> Vc<FileSystemPath> {
self.client_root().join("_next".to_string())
......@@ -309,8 +346,13 @@ impl Project {
}
#[turbo_tasks::function]
pub(super) fn client_compile_time_info(&self) -> Vc<CompileTimeInfo> {
get_client_compile_time_info(self.mode, self.browserslist_query.clone())
pub(super) async fn client_compile_time_info(self: Vc<Self>) -> Result<Vc<CompileTimeInfo>> {
let this = self.await?;
Ok(get_client_compile_time_info(
this.mode,
this.browserslist_query.clone(),
self.dist_root_string(),
))
}
#[turbo_tasks::function]
......@@ -325,7 +367,11 @@ impl Project {
#[turbo_tasks::function]
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]
......
......@@ -10,6 +10,9 @@ pub struct BuildOptions {
/// The project's directory.
pub dir: Option<PathBuf>,
/// next.config.js's distDir.
pub dist_dir: Option<String>,
/// The maximum memory to use for the build.
pub memory_limit: Option<usize>,
......
......@@ -26,6 +26,9 @@ pub struct BuildCliArgs {
#[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,
......@@ -100,6 +103,7 @@ async fn main_inner() -> Result<()> {
log_detail: args.log_detail,
full_stats: args.full_stats,
build_context: None,
dist_dir: args.dist_dir,
})
.await
}
......@@ -25,7 +25,7 @@ use next_core::{
use serde::Serialize;
use turbo_tasks::{
graph::{AdjacencyMap, GraphTraversal},
Completion, Completions, TransientInstance, TryJoinIterExt, Vc,
Completion, Completions, TransientInstance, TryJoinIterExt, ValueToString, Vc,
};
use turbopack_binding::{
turbo::tasks_fs::{rebase, DiskFileSystem, FileContent, FileSystem, FileSystemPath},
......@@ -90,12 +90,17 @@ pub(crate) async fn next_build(options: TransientInstance<BuildOptions>) -> Resu
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>> =
Vc::upcast(ConsoleUi::new(TransientInstance::new(log_options)));
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_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.
// let client_public_fs = VirtualFileSystem::new();
// let client_public_root = client_public_fs.root();
......@@ -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 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());
// TODO(alexkirsz) Pages should build their own routes, outside of a FS.
......
......@@ -191,6 +191,7 @@ fn next_ssr_client_module_transition(
#[turbo_tasks::function]
fn next_edge_ssr_client_module_transition(
project_path: Vc<FileSystemPath>,
dist_root: Vc<String>,
execution_context: Vc<ExecutionContext>,
app_dir: Vc<FileSystemPath>,
next_config: Vc<NextConfig>,
......@@ -214,7 +215,7 @@ fn next_edge_ssr_client_module_transition(
next_config,
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(),
)
......@@ -290,6 +291,7 @@ fn next_route_transition(
#[turbo_tasks::function]
fn next_edge_server_component_transition(
project_path: Vc<FileSystemPath>,
dist_root: Vc<String>,
execution_context: Vc<ExecutionContext>,
app_dir: Vc<FileSystemPath>,
server_root: Vc<FileSystemPath>,
......@@ -305,7 +307,7 @@ fn next_edge_server_component_transition(
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 =
get_edge_resolve_options_context(project_path, ty, mode, next_config, execution_context);
let rsc_module_options_context =
......@@ -325,6 +327,7 @@ fn next_edge_server_component_transition(
#[turbo_tasks::function]
fn next_edge_route_transition(
project_path: Vc<FileSystemPath>,
dist_root: Vc<String>,
app_dir: Vc<FileSystemPath>,
server_root: Vc<FileSystemPath>,
next_config: Vc<NextConfig>,
......@@ -335,7 +338,7 @@ fn next_edge_route_transition(
let mode = NextMode::DevServer;
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(
DevChunkingContext::builder(
......@@ -374,6 +377,7 @@ fn next_edge_route_transition(
#[turbo_tasks::function]
fn next_edge_page_transition(
project_path: Vc<FileSystemPath>,
dist_root: Vc<String>,
app_dir: Vc<FileSystemPath>,
server_root: Vc<FileSystemPath>,
mode: NextMode,
......@@ -384,7 +388,7 @@ fn next_edge_page_transition(
) -> Vc<Box<dyn Transition>> {
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(
DevChunkingContext::builder(
......@@ -422,6 +426,7 @@ fn next_edge_page_transition(
#[turbo_tasks::function]
fn app_context(
project_path: Vc<FileSystemPath>,
dist_root: Vc<String>,
execution_context: Vc<ExecutionContext>,
server_root: Vc<FileSystemPath>,
app_dir: Vc<FileSystemPath>,
......@@ -439,6 +444,7 @@ fn app_context(
"next-edge-route".to_string(),
next_edge_route_transition(
project_path,
dist_root,
app_dir,
server_root,
next_config,
......@@ -462,6 +468,7 @@ fn app_context(
"next-edge-page".to_string(),
next_edge_page_transition(
project_path,
dist_root,
app_dir,
server_root,
mode,
......@@ -494,6 +501,7 @@ fn app_context(
"next-edge-server-component".to_string(),
next_edge_server_component_transition(
project_path,
dist_root,
execution_context,
app_dir,
server_root,
......@@ -547,6 +555,7 @@ fn app_context(
"next-edge-ssr-client-module".to_string(),
next_edge_ssr_client_module_transition(
project_path,
dist_root,
execution_context,
app_dir,
next_config,
......@@ -580,6 +589,7 @@ fn app_context(
#[turbo_tasks::function]
pub async fn create_app_source(
app_dir: Vc<OptionAppDir>,
dist_root: Vc<String>,
project_path: Vc<FileSystemPath>,
execution_context: Vc<ExecutionContext>,
output_path: Vc<FileSystemPath>,
......@@ -599,6 +609,7 @@ pub async fn create_app_source(
let context_ssr = app_context(
project_path,
dist_root,
execution_context,
server_root,
app_dir,
......@@ -613,6 +624,7 @@ pub async fn create_app_source(
);
let context = app_context(
project_path,
dist_root,
execution_context,
server_root,
app_dir,
......
......@@ -65,27 +65,50 @@ use crate::{
util::foreign_code_context_condition,
};
fn defines(mode: NextMode) -> CompileTimeDefines {
compile_time_defines!(
fn defines(mode: NextMode, dist_root_path: Option<&str>) -> CompileTimeDefines {
// [TODO] macro may need to allow dynamically expand from some iterable values
let mut defines = compile_time_defines!(
process.turbopack = true,
process.env.NODE_ENV = mode.node_env(),
process.env.__NEXT_CLIENT_ROUTER_FILTER_ENABLED = false,
process.env.__NEXT_HAS_REWRITES = true,
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
// packages/next/src/build/webpack-config.ts
defines
}
#[turbo_tasks::function]
fn next_client_defines(mode: NextMode) -> Vc<CompileTimeDefines> {
defines(mode).cell()
async fn next_client_defines(
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]
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!(
..defines(mode).into_iter(),
..defines(mode, Some(dist_root_path.as_str())).into_iter(),
Buffer = FreeVarReference::EcmaScriptModule {
request: "node:buffer".to_string(),
lookup_path: None,
......@@ -104,6 +127,7 @@ async fn next_client_free_vars(mode: NextMode) -> Result<Vc<FreeVarReferences>>
pub fn get_client_compile_time_info(
mode: NextMode,
browserslist_query: String,
dist_root_path: Vc<String>,
) -> Vc<CompileTimeInfo> {
CompileTimeInfo::builder(Environment::new(Value::new(ExecutionEnvironment::Browser(
BrowserEnvironment {
......@@ -114,8 +138,8 @@ pub fn get_client_compile_time_info(
}
.into(),
))))
.defines(next_client_defines(mode))
.free_var_references(next_client_free_vars(mode))
.defines(next_client_defines(mode, dist_root_path))
.free_var_references(next_client_free_vars(mode, dist_root_path))
.cell()
}
......
......@@ -83,6 +83,7 @@ pub struct NextConfig {
pub react_strict_mode: Option<bool>,
pub transpile_packages: Option<Vec<String>>,
pub modularize_imports: Option<IndexMap<String, ModularizeImportPackageConfig>>,
pub dist_dir: Option<String>,
sass_options: Option<serde_json::Value>,
trailing_slash: bool,
......@@ -100,7 +101,6 @@ pub struct NextConfig {
clean_dist_dir: bool,
compress: bool,
dev_indicators: DevIndicatorsConfig,
dist_dir: String,
eslint: EslintConfig,
exclude_default_moment_locales: bool,
// this can be a function in js land
......
......@@ -31,26 +31,46 @@ use crate::{
util::foreign_code_context_condition,
};
fn defines() -> CompileTimeDefines {
compile_time_defines!(
fn defines(dist_root_path: Option<&str>) -> CompileTimeDefines {
// [TODO] macro may need to allow dynamically expand from some iterable values
let mut defines = compile_time_defines!(
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",
);
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
// packages/next/src/build/webpack-config.ts
defines
}
#[turbo_tasks::function]
fn next_edge_defines() -> Vc<CompileTimeDefines> {
defines().cell()
async fn next_edge_defines(dist_root_path: Vc<String>) -> Result<Vc<CompileTimeDefines>> {
let dist_root_path = &*dist_root_path.await?;
Ok(defines(Some(dist_root_path.as_str())).cell())
}
#[turbo_tasks::function]
fn next_edge_free_vars(project_path: Vc<FileSystemPath>) -> Vc<FreeVarReferences> {
free_var_references!(
..defines().into_iter(),
async fn next_edge_free_vars(
project_path: Vc<FileSystemPath>,
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 {
request: "next/dist/compiled/buffer".to_string(),
lookup_path: Some(project_path),
......@@ -62,19 +82,20 @@ fn next_edge_free_vars(project_path: Vc<FileSystemPath>) -> Vc<FreeVarReferences
export: Some("default".to_string()),
},
)
.cell()
.cell())
}
#[turbo_tasks::function]
pub fn get_edge_compile_time_info(
project_path: Vc<FileSystemPath>,
server_addr: Vc<ServerAddr>,
dist_root_path: Vc<String>,
) -> Vc<CompileTimeInfo> {
CompileTimeInfo::builder(Environment::new(Value::new(
ExecutionEnvironment::EdgeWorker(EdgeWorkerEnvironment { server_addr }.into()),
)))
.defines(next_edge_defines())
.free_var_references(next_edge_free_vars(project_path))
.defines(next_edge_defines(dist_root_path))
.free_var_references(next_edge_free_vars(project_path, dist_root_path))
.cell()
}
......
......@@ -82,6 +82,7 @@ use crate::{
pub async fn create_page_source(
pages_structure: Vc<PagesStructure>,
project_root: Vc<FileSystemPath>,
dist_root: Vc<String>,
execution_context: Vc<ExecutionContext>,
node_root: Vc<FileSystemPath>,
client_root: Vc<FileSystemPath>,
......@@ -139,7 +140,7 @@ pub async fn create_page_source(
.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(
DevChunkingContext::builder(
......
......@@ -213,6 +213,7 @@ fn route_executor(
#[turbo_tasks::function]
fn edge_transition_map(
server_addr: Vc<ServerAddr>,
dist_root: Vc<String>,
project_path: Vc<FileSystemPath>,
output_path: Vc<FileSystemPath>,
next_config: Vc<NextConfig>,
......@@ -220,7 +221,7 @@ fn edge_transition_map(
) -> Vc<TransitionsByName> {
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(
DevChunkingContext::builder(
......@@ -275,6 +276,7 @@ fn edge_transition_map(
pub async fn route(
execution_context: Vc<ExecutionContext>,
request: Vc<RouterRequest>,
dist_root: Vc<String>,
next_config: Vc<NextConfig>,
server_addr: Vc<ServerAddr>,
routes_changed: Vc<Completion>,
......@@ -287,6 +289,7 @@ pub async fn route(
route_internal(
execution_context,
request,
dist_root,
next_config,
server_addr,
routes_changed,
......@@ -311,6 +314,7 @@ macro_rules! shared_anyhow {
async fn route_internal(
execution_context: Vc<ExecutionContext>,
request: Vc<RouterRequest>,
dist_root: Vc<String>,
next_config: Vc<NextConfig>,
server_addr: Vc<ServerAddr>,
routes_changed: Vc<Completion>,
......@@ -326,6 +330,7 @@ async fn route_internal(
Some(get_next_build_import_map()),
Some(edge_transition_map(
server_addr,
dist_root,
project_path,
chunking_context.output_root(),
next_config,
......
......@@ -31,6 +31,7 @@ pub struct NextRouterContentSource {
server_addr: Vc<ServerAddr>,
app_dir: Vc<OptionAppDir>,
pages_structure: Vc<PagesStructure>,
dist_root: Vc<String>,
}
#[turbo_tasks::value_impl]
......@@ -43,6 +44,7 @@ impl NextRouterContentSource {
server_addr: Vc<ServerAddr>,
app_dir: Vc<OptionAppDir>,
pages_structure: Vc<PagesStructure>,
dist_root: Vc<String>,
) -> Vc<NextRouterContentSource> {
NextRouterContentSource {
inner,
......@@ -51,6 +53,7 @@ impl NextRouterContentSource {
server_addr,
app_dir,
pages_structure,
dist_root,
}
.cell()
}
......@@ -144,6 +147,7 @@ impl GetContentSourceContent for NextRouterContentSource {
let res = route(
this.execution_context,
request,
this.dist_root,
this.next_config,
this.server_addr,
routes_changed(this.app_dir, this.pages_structure, this.next_config),
......
......@@ -40,7 +40,7 @@ use owo_colors::OwoColorize;
use tracing_subscriber::{prelude::*, EnvFilter, Registry};
use turbo_tasks::{
util::FormatDuration, StatsType, TransientInstance, TurboTasks, TurboTasksBackendApi,
UpdateInfo, Value, Vc,
UpdateInfo, Value, ValueToString, Vc,
};
use turbopack_binding::{
turbo::{
......@@ -318,9 +318,17 @@ async fn source(
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 = 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(
project_path,
......@@ -339,7 +347,10 @@ async fn source(
let next_config = load_next_config(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_root = dev_server_fs.root();
......@@ -364,7 +375,8 @@ async fn source(
browserslist_query.clone(),
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(
project_path,
dev_server_root,
......@@ -376,6 +388,7 @@ async fn source(
let page_source = create_page_source(
pages_structure,
project_path,
dist_root,
execution_context,
output_root.join("pages".to_string()),
dev_server_root,
......@@ -388,6 +401,7 @@ async fn source(
let app_dir = find_app_dir_if_enabled(project_path);
let app_source = create_app_source(
app_dir,
dist_root,
project_path,
execution_context,
output_root.join("app".to_string()),
......@@ -433,6 +447,7 @@ async fn source(
server_addr,
app_dir,
pages_structure,
dist_root,
));
let source = Vc::upcast(
PrefixedRouterContentSource {
......
......@@ -1012,6 +1012,7 @@ export default async function build(
await binding.turbo.nextBuild({
...NextBuildContext,
root,
distDir: config.distDir,
})
const [duration] = process.hrtime(turboNextBuildStart)
......
......@@ -29,6 +29,7 @@ const supportedTurbopackNextConfigOptions = [
'useFileSystemPublicRoutes',
'generateEtags',
'assetPrefix',
'distDir',
'experimental.serverComponentsExternalPackages',
'experimental.turbo',
'experimental.mdxRs',
......
......@@ -30,7 +30,8 @@ const apiJson = new File(join(appDir, 'app/api/json/route.js'))
export const expectedWhenTrailingSlashTrue = [
'404.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/_ssgManifest.js',
'another/first/index.html',
......@@ -53,7 +54,7 @@ export const expectedWhenTrailingSlashTrue = [
const expectedWhenTrailingSlashFalse = [
'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/_ssgManifest.js',
'another.html',
......
......@@ -2,13 +2,14 @@
import fs from 'fs-extra'
import { join } from 'path'
import { BUILD_ID_FILE } from 'next/constants'
import { BUILD_ID_FILE, BUILD_MANIFEST } from 'next/constants'
import {
nextStart,
nextBuild,
findPort,
killApp,
renderViaHTTP,
launchApp,
} from 'next-test-utils'
const appDir = join(__dirname, '../')
......@@ -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 () => {
const origNextConfig = await fs.readFile(nextConfig, 'utf8')
await fs.writeFile(nextConfig, `module.exports = { distDir: '' }`)
......
......@@ -89,6 +89,7 @@ const enabledTests = [
'test/e2e/typescript-version-warning/typescript-version-warning.test.ts',
'test/e2e/undici-fetch/index.test.ts',
'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-app/test/index.test.js',
'test/integration/404-page-custom-error/test/index.test.js',
......@@ -96,6 +97,7 @@ const enabledTests = [
'test/integration/create-next-app/templates-app.test.ts',
'test/integration/create-next-app/templates-pages.test.ts',
'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/next-image-new/middleware/test/index.test.ts',
'test/integration/next-image-new/react-virtualized/test/index.test.ts',
......@@ -113,6 +115,7 @@ const enabledTests = [
'test/integration/ssg-dynamic-routes-404-page/test/index.test.js',
'test/integration/static-404/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/production/app-dir-hide-suppressed-error-during-next-export/index.test.ts',
'test/production/app-dir-prefetch-non-iso-url/index.test.ts',
......@@ -121,9 +124,6 @@ const enabledTests = [
'test/production/jest/rsc/lib/utils.test.js',
'test/production/jest/transpile-packages.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
......
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