Skip to content

Commit

Permalink
Merge 24afad8 into 45a69f1
Browse files Browse the repository at this point in the history
  • Loading branch information
jridgewell authored Feb 8, 2023
2 parents 45a69f1 + 24afad8 commit 7452089
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 23 deletions.
3 changes: 2 additions & 1 deletion crates/next-core/js/src/entry/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import loadNextConfig from "@vercel/turbopack-next/entry/config/next";
import { makeResolver } from "next/dist/server/router.js";

import "next/dist/server/node-polyfill-fetch.js";
import * as middleware from "MIDDLEWARE_CONFIG";

type RouterRequest = {
method: string;
Expand Down Expand Up @@ -52,7 +53,7 @@ let resolveRouteMemo: Promise<
>;
async function getResolveRoute(dir: string) {
const nextConfig = await loadNextConfig(true);
return await makeResolver(dir, nextConfig);
return await makeResolver(dir, nextConfig, middleware);
}

export default async function route(
Expand Down
94 changes: 75 additions & 19 deletions crates/next-core/src/router.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
use std::collections::HashMap;

use anyhow::{bail, Result};
use serde::Deserialize;
use turbo_tasks::{
primitives::{JsonValueVc, StringsVc},
Value,
};
use turbo_tasks_fs::{to_sys_path, FileSystemPathVc};
use turbo_tasks_fs::{to_sys_path, File, FileSystemPathVc};
use turbopack::evaluate_context::node_evaluate_asset_context;
use turbopack_core::{
asset::AssetVc,
context::{AssetContext, AssetContextVc},
resolve::{find_context_file, FindContextFileResult},
source_asset::SourceAssetVc,
virtual_asset::VirtualAssetVc,
};
use turbopack_dev_server::source::{headers::Headers, query::Query};
use turbopack_ecmascript::{
chunk::EcmascriptChunkPlaceablesVc, EcmascriptInputTransform, EcmascriptInputTransformsVc,
EcmascriptModuleAssetType, EcmascriptModuleAssetVc,
chunk::{EcmascriptChunkPlaceableVc, EcmascriptChunkPlaceablesVc},
EcmascriptInputTransform, EcmascriptInputTransformsVc, EcmascriptModuleAssetType,
EcmascriptModuleAssetVc, InnerAssetsVc,
};
use turbopack_node::{
evaluate::{evaluate, JavaScriptValue},
Expand All @@ -25,9 +29,11 @@ use turbopack_node::{

use crate::{
embed_js::{next_asset, wrap_with_next_js_fs},
next_config::NextConfigVc,
next_import_map::get_next_build_import_map,
};

#[turbo_tasks::function]
fn next_configs() -> StringsVc {
StringsVc::cell(
["next.config.mjs", "next.config.js"]
Expand All @@ -37,6 +43,16 @@ fn next_configs() -> StringsVc {
)
}

#[turbo_tasks::function]
async fn middleware_files(page_extensions: StringsVc) -> Result<StringsVc> {
let extensions = page_extensions.await?;
let files = ["middleware.", "src/middleware."]
.into_iter()
.flat_map(|f| extensions.iter().map(move |ext| String::from(f) + ext))
.collect();
Ok(StringsVc::cell(files))
}

#[turbo_tasks::value(shared)]
#[derive(Debug, Clone, Default)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -115,37 +131,73 @@ impl From<RouterIncomingMessage> for RouterResult {
}

#[turbo_tasks::function]
async fn extra_configs(
async fn get_config(
context: AssetContextVc,
project_path: FileSystemPathVc,
) -> Result<EcmascriptChunkPlaceablesVc> {
let find_config_result = find_context_file(project_path, next_configs());
configs: StringsVc,
) -> Result<EcmascriptChunkPlaceableVc> {
let find_config_result = find_context_file(project_path, configs);
let config_asset = match &*find_config_result.await? {
FindContextFileResult::Found(config_path, _) => Some(SourceAssetVc::new(*config_path)),
FindContextFileResult::NotFound(_) => None,
};
let Some(config_asset) = config_asset else {
return Ok(EcmascriptChunkPlaceablesVc::empty());
let config_asset: AssetVc = match config_asset {
Some(c) => c.into(),
None => {
let configs = configs.await?;
let Some(config_path) = configs.first() else {
bail!("no config path provided");
};
VirtualAssetVc::new(project_path.join(config_path), File::from("").into()).into()
}
};
let config_chunk = EcmascriptModuleAssetVc::new(
config_asset.into(),

Ok(EcmascriptModuleAssetVc::new(
config_asset,
context,
Value::new(EcmascriptModuleAssetType::Typescript),
EcmascriptInputTransformsVc::cell(vec![EcmascriptInputTransform::TypeScript]),
context.environment(),
)
.as_ecmascript_chunk_placeable();
Ok(EcmascriptChunkPlaceablesVc::cell(vec![config_chunk]))
.into())
}

#[turbo_tasks::function]
fn watch_files_hack(
context: AssetContextVc,
project_path: FileSystemPathVc,
) -> EcmascriptChunkPlaceablesVc {
let next_config = get_config(context, project_path, next_configs());
EcmascriptChunkPlaceablesVc::cell(vec![next_config])
}

#[turbo_tasks::function]
fn route_executor(context: AssetContextVc, project_path: FileSystemPathVc) -> AssetVc {
EcmascriptModuleAssetVc::new(
async fn config_assets(
context: AssetContextVc,
project_path: FileSystemPathVc,
page_extensions: StringsVc,
) -> Result<InnerAssetsVc> {
let mut inner = HashMap::new();

let middleware_config = get_config(context, project_path, middleware_files(page_extensions));
inner.insert("MIDDLEWARE_CONFIG".to_string(), middleware_config.into());

Ok(InnerAssetsVc::cell(inner))
}

#[turbo_tasks::function]
fn route_executor(
context: AssetContextVc,
project_path: FileSystemPathVc,
configs: InnerAssetsVc,
) -> AssetVc {
EcmascriptModuleAssetVc::new_with_inner_assets(
next_asset(project_path.join("router.js"), "entry/router.ts"),
context,
Value::new(EcmascriptModuleAssetType::Typescript),
EcmascriptInputTransformsVc::cell(vec![EcmascriptInputTransform::TypeScript]),
context.environment(),
configs,
)
.into()
}
Expand All @@ -154,16 +206,20 @@ fn route_executor(context: AssetContextVc, project_path: FileSystemPathVc) -> As
pub async fn route(
execution_context: ExecutionContextVc,
request: RouterRequestVc,
next_config: NextConfigVc,
) -> Result<RouterResultVc> {
let ExecutionContext {
project_root,
intermediate_output_path,
} = *execution_context.await?;
let project_path = wrap_with_next_js_fs(project_root);
let context = node_evaluate_asset_context(Some(get_next_build_import_map(project_path)));
let router_asset = route_executor(context, project_path);
// TODO this is a hack to get these files watched.
let extra_configs = extra_configs(context, project_path);

let configs = config_assets(context, project_path, next_config.page_extensions());
let router_asset = route_executor(context, project_path, configs);

// TODO this is a hack to get this files watched.
let next_config = watch_files_hack(context, project_path);

let request = serde_json::value::to_value(&*request.await?)?;
let Some(dir) = to_sys_path(project_root).await? else {
Expand All @@ -175,8 +231,8 @@ pub async fn route(
project_root,
project_root,
context,
intermediate_output_path,
Some(extra_configs),
intermediate_output_path.join("router"),
Some(next_config),
vec![
JsonValueVc::cell(request),
JsonValueVc::cell(dir.to_string_lossy().into()),
Expand Down
10 changes: 8 additions & 2 deletions crates/next-core/src/router_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ use turbopack_dev_server::source::{
};
use turbopack_node::execution_context::ExecutionContextVc;

use crate::router::{route, RouterRequest, RouterResult};
use crate::{
next_config::NextConfigVc,
router::{route, RouterRequest, RouterResult},
};

#[turbo_tasks::value(shared)]
pub struct NextRouterContentSource {
/// A wrapped content source from which we will fetch assets.
inner: ContentSourceVc,
execution_context: ExecutionContextVc,
next_config: NextConfigVc,
}

#[turbo_tasks::value_impl]
Expand All @@ -25,10 +29,12 @@ impl NextRouterContentSourceVc {
pub fn new(
inner: ContentSourceVc,
execution_context: ExecutionContextVc,
next_config: NextConfigVc,
) -> NextRouterContentSourceVc {
NextRouterContentSource {
inner,
execution_context,
next_config,
}
.cell()
}
Expand Down Expand Up @@ -78,7 +84,7 @@ impl ContentSource for NextRouterContentSource {
}
.cell();

let res = route(this.execution_context, request);
let res = route(this.execution_context, request, this.next_config);
let Ok(res) = res.await else {
return Ok(this
.inner
Expand Down
3 changes: 2 additions & 1 deletion crates/next-dev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,8 @@ async fn source(
CombinedContentSourceVc::new(vec![static_source, page_source]).into(),
)
.into();
let router_source = NextRouterContentSourceVc::new(main_source, execution_context).into();
let router_source =
NextRouterContentSourceVc::new(main_source, execution_context, next_config).into();
let source = RouterContentSource {
routes: vec![
("__turbopack__/".to_string(), introspect),
Expand Down

0 comments on commit 7452089

Please sign in to comment.