add support for pageExtensions in next.config.js (vercel/turbo#3499)

This commit is contained in:
Leah 2023-01-26 21:14:35 +01:00 committed by GitHub
parent ff69c0f4e0
commit dc13baf6e7
2 changed files with 58 additions and 50 deletions

View file

@ -37,18 +37,19 @@ use crate::embed_js::next_asset;
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct NextConfig {
pub cross_origin: Option<String>,
pub config_file: Option<String>,
pub config_file_name: String,
pub react_strict_mode: Option<bool>,
pub experimental: ExperimentalConfig,
pub env: IndexMap<String, String>,
pub compiler: Option<CompilerConfig>,
pub experimental: ExperimentalConfig,
pub images: ImageConfig,
pub page_extensions: Vec<String>,
pub react_strict_mode: Option<bool>,
pub transpile_packages: Option<Vec<String>>,
// unsupported
cross_origin: Option<String>,
compiler: Option<CompilerConfig>,
amp: AmpConfig,
analytics_id: String,
asset_prefix: String,
@ -72,7 +73,6 @@ pub struct NextConfig {
optimize_fonts: bool,
output: Option<OutputType>,
output_file_tracing: bool,
page_extensions: Vec<String>,
powered_by_header: bool,
production_browser_source_maps: bool,
public_runtime_config: IndexMap<String, serde_json::Value>,
@ -382,6 +382,11 @@ impl NextConfigVc {
Ok(self.await?.images.clone().cell())
}
#[turbo_tasks::function]
pub async fn page_extensions(self) -> Result<StringsVc> {
Ok(StringsVc::cell(self.await?.page_extensions.clone()))
}
#[turbo_tasks::function]
pub async fn transpile_packages(self) -> Result<StringsVc> {
Ok(StringsVc::cell(

View file

@ -3,7 +3,7 @@ use std::collections::HashMap;
use anyhow::Result;
use serde::{Deserialize, Serialize};
use turbo_tasks::{
primitives::{BoolVc, StringVc},
primitives::{BoolVc, StringVc, StringsVc},
trace::TraceRawVcs,
Value,
};
@ -230,6 +230,7 @@ pub async fn create_page_source(
server_data_context,
client_context,
pages_dir,
next_config.page_extensions(),
SpecificityVc::exact(),
0,
pages_dir,
@ -410,6 +411,7 @@ async fn create_page_source_for_directory(
server_data_context: AssetContextVc,
client_context: AssetContextVc,
pages_dir: FileSystemPathVc,
page_extensions: StringsVc,
specificity: SpecificityVc,
position: u32,
input_dir: FileSystemPathVc,
@ -421,6 +423,8 @@ async fn create_page_source_for_directory(
intermediate_output_path: FileSystemPathVc,
output_root: FileSystemPathVc,
) -> Result<CombinedContentSourceVc> {
let page_extensions_raw = &*page_extensions.await?;
let mut sources = vec![];
let dir_content = input_dir.read_dir().await?;
if let DirectoryContent::Entries(entries) = &*dir_content {
@ -435,51 +439,49 @@ async fn create_page_source_for_directory(
match entry {
DirectoryEntry::File(file) => {
if let Some((basename, extension)) = name.rsplit_once('.') {
match extension {
// pageExtensions option from next.js
// defaults: https://github.com/vercel/next.js/blob/611e13f5159457fedf96d850845650616a1f75dd/packages/next/server/config-shared.ts#L499
"js" | "ts" | "jsx" | "tsx" | "mdx" => {
let (dev_server_path, intermediate_output_path, specificity) =
if basename == "index" {
(
server_path.join("index.html"),
intermediate_output_path,
specificity,
)
} else if basename == "404" {
(
server_path.join("[...].html"),
intermediate_output_path.join(basename),
specificity.with_fallback(position),
)
} else {
(
server_path.join(basename).join("index.html"),
intermediate_output_path.join(basename),
specificity,
)
};
sources.push((
name,
create_page_source_for_file(
context_path,
server_context,
server_data_context,
client_context,
pages_dir,
specificity,
*file,
runtime_entries,
fallback_page,
server_root,
dev_server_path,
dev_server_path.is_inside(server_api_path),
if page_extensions_raw
.iter()
.any(|allowed| allowed == extension)
{
let (dev_server_path, intermediate_output_path, specificity) =
if basename == "index" {
(
server_path.join("index.html"),
intermediate_output_path,
output_root,
),
));
}
_ => {}
specificity,
)
} else if basename == "404" {
(
server_path.join("[...].html"),
intermediate_output_path.join(basename),
specificity.with_fallback(position),
)
} else {
(
server_path.join(basename).join("index.html"),
intermediate_output_path.join(basename),
specificity,
)
};
sources.push((
name,
create_page_source_for_file(
context_path,
server_context,
server_data_context,
client_context,
pages_dir,
specificity,
*file,
runtime_entries,
fallback_page,
server_root,
dev_server_path,
dev_server_path.is_inside(server_api_path),
intermediate_output_path,
output_root,
),
));
}
}
}
@ -492,6 +494,7 @@ async fn create_page_source_for_directory(
server_data_context,
client_context,
pages_dir,
page_extensions,
specificity,
position + 1,
*dir,