Skip to content

Commit

Permalink
Merge branch 'canary' into wbinnssmith/nightly-2023-09-21
Browse files Browse the repository at this point in the history
  • Loading branch information
kodiakhq[bot] committed Sep 22, 2023
2 parents 5bb6102 + 2cc30fc commit b7a8435
Show file tree
Hide file tree
Showing 22 changed files with 305 additions and 86 deletions.
10 changes: 9 additions & 1 deletion packages/next-swc/crates/napi/src/next_api/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down Expand Up @@ -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();
Expand Down
6 changes: 6 additions & 0 deletions packages/next-swc/crates/napi/src/turbopack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,

Expand All @@ -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
Expand Down
74 changes: 60 additions & 14 deletions packages/next-swc/crates/next-api/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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,

Expand Down Expand Up @@ -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]
Expand All @@ -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())
Expand Down Expand Up @@ -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]
Expand All @@ -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]
Expand Down
3 changes: 3 additions & 0 deletions packages/next-swc/crates/next-build/src/build_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,

Expand Down
4 changes: 4 additions & 0 deletions packages/next-swc/crates/next-build/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
14 changes: 10 additions & 4 deletions packages/next-swc/crates/next-build/src/next_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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.
Expand Down
20 changes: 16 additions & 4 deletions packages/next-swc/crates/next-core/src/app_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand All @@ -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(),
)
Expand Down Expand Up @@ -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>,
Expand All @@ -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 =
Expand All @@ -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>,
Expand All @@ -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(
Expand Down Expand Up @@ -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,
Expand All @@ -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(
Expand Down Expand Up @@ -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>,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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>,
Expand All @@ -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,
Expand All @@ -613,6 +624,7 @@ pub async fn create_app_source(
);
let context = app_context(
project_path,
dist_root,
execution_context,
server_root,
app_dir,
Expand Down
Loading

0 comments on commit b7a8435

Please sign in to comment.