Skip to content

Commit

Permalink
handle decl export function
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Jun 7, 2024
1 parent 0f79bbd commit 84793be
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 20 deletions.
39 changes: 26 additions & 13 deletions packages/next-swc/crates/next-core/src/app_segment_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use turbo_tasks_fs::FileSystemPath;
use turbopack_binding::{
swc::core::{
common::{source_map::Pos, Span, Spanned, GLOBALS},
ecma::ast::{Expr, Ident, Program},
ecma::ast::{Decl, Expr, FnExpr, Ident, Program},
},
turbopack::{
core::{
Expand Down Expand Up @@ -74,8 +74,8 @@ pub struct NextSegmentConfig {
pub preferred_region: Option<Vec<RcStr>>,
pub experimental_ppr: Option<bool>,
/// Wether these metadata exports are defined in the source file.
pub generate_image_metadata: Option<bool>,
pub generate_sitemaps: Option<bool>,
pub generate_image_metadata: bool,
pub generate_sitemaps: bool,
}

#[turbo_tasks::value_impl]
Expand Down Expand Up @@ -277,22 +277,35 @@ pub async fn parse_segment_config_from_source(
let mut config = NextSegmentConfig::default();

for item in &module_ast.body {
let Some(decl) = item
let Some(export_decl) = item
.as_module_decl()
.and_then(|mod_decl| mod_decl.as_export_decl())
.and_then(|export_decl| export_decl.decl.as_var())
else {
continue;
};

for decl in &decl.decls {
let Some(ident) = decl.name.as_ident().map(|ident| ident.deref()) else {
continue;
};
match &export_decl.decl {
Decl::Var(var_decl) => {
for decl in &var_decl.decls {
let Some(ident) = decl.name.as_ident().map(|ident| ident.deref()) else {
continue;
};

if let Some(init) = decl.init.as_ref() {
parse_config_value(source, &mut config, ident, init, eval_context);
if let Some(init) = decl.init.as_ref() {
parse_config_value(source, &mut config, ident, init, eval_context);
}
}
}
Decl::Fn(fn_decl) => {
let ident = &fn_decl.ident;
// create an empty expression of {}, we don't need init for function
let init = Expr::Fn(FnExpr {
ident: None,
function: fn_decl.function.clone(),
});
parse_config_value(source, &mut config, ident, &init, eval_context);
}
_ => {}
}
}
config
Expand Down Expand Up @@ -439,10 +452,10 @@ fn parse_config_value(
// Match exported generateImageMetadata function and generateSitemaps function, and pass
// them to config.
"generateImageMetadata" => {
config.generate_image_metadata = Some(true);
config.generate_image_metadata = true;
}
"generateSitemaps" => {
config.generate_sitemaps = Some(true);
config.generate_sitemaps = true;
}
"experimental_ppr" => {
let value = eval_context.eval(init);
Expand Down
26 changes: 20 additions & 6 deletions packages/next-swc/crates/next-core/src/next_app/metadata/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub async fn get_app_metadata_route_source(
page: AppPage,
mode: NextMode,
metadata: MetadataItem,
is_multi_dynamic: bool,
) -> Result<Vc<Box<dyn Source>>> {
Ok(match metadata {
MetadataItem::Static { path } => static_route_source(mode, path),
Expand All @@ -45,7 +46,7 @@ pub async fn get_app_metadata_route_source(
if stem == "robots" || stem == "manifest" {
dynamic_text_route_source(path)
} else if stem == "sitemap" {
dynamic_site_map_route_source(mode, path, page)
dynamic_site_map_route_source(mode, path, page, is_multi_dynamic)
} else {
dynamic_image_route_source(path)
}
Expand Down Expand Up @@ -76,17 +77,20 @@ pub async fn get_app_metadata_route_entry(
// config.generateImageMetadata is defined
let is_multi_dynamic: bool = if Some(segment_config).is_some() {
let config = segment_config.await.unwrap();
config.generate_sitemaps.is_some() || config.generate_image_metadata.is_some()
config.generate_sitemaps || config.generate_image_metadata
} else {
false
};

let origin_page = page.clone();

Check warning on line 85 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / rustdoc check / build

unused variable: `origin_page`

Check warning on line 85 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / rust check / build

unused variable: `origin_page`

Check failure on line 85 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / rust check / build

unused variable: `origin_page`

Check warning on line 85 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / build-native / build

unused variable: `origin_page`

Check warning on line 85 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / build / build

unused variable: `origin_page`

Check warning on line 85 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test cargo unit / build

unused variable: `origin_page`

Check warning on line 85 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / types and precompiled / build

unused variable: `origin_page`

Check warning on line 85 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test unit (18) / build

unused variable: `origin_page`

Check warning on line 85 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test ppr integration / build

unused variable: `origin_page`

Check warning on line 85 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / lint / build

unused variable: `origin_page`

Check warning on line 85 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test unit (20) / build

unused variable: `origin_page`

Check warning on line 85 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test turbopack dev (3/5) / build

unused variable: `origin_page`

Check warning on line 85 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test turbopack development integration (3/5) / build

unused variable: `origin_page`

Check warning on line 85 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test next-swc wasm / build

unused variable: `origin_page`

Check warning on line 85 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test turbopack development integration (1/5) / build

unused variable: `origin_page`

Check warning on line 85 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test firefox and safari / build

unused variable: `origin_page`

Check warning on line 85 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test dev (2/4) / build

unused variable: `origin_page`

Check warning on line 85 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test integration (8/12) / build

unused variable: `origin_page`

Check warning on line 85 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test prod (4/5) / build

unused variable: `origin_page`

Check warning on line 85 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / Test new tests for flakes (dev) / build

unused variable: `origin_page`

Check warning on line 85 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / Test new tests for flakes (prod) / build

unused variable: `origin_page`

Check warning on line 85 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test turbopack development integration (4/5) / build

unused variable: `origin_page`

Check warning on line 85 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test turbopack production (2/5) / build

unused variable: `origin_page`

Check warning on line 85 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test turbopack dev (1/5) / build

unused variable: `origin_page`

Check warning on line 85 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test dev (4/4) / build

unused variable: `origin_page`

Check warning on line 85 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test turbopack production (1/5) / build

unused variable: `origin_page`

Check warning on line 85 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test turbopack dev (4/5) / build

unused variable: `origin_page`

Check warning on line 85 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test turbopack dev (5/5) / build

unused variable: `origin_page`

Check warning on line 85 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test turbopack dev (2/5) / build

unused variable: `origin_page`

Check warning on line 85 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test turbopack production (3/5) / build

unused variable: `origin_page`

Check warning on line 85 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test turbopack production (4/5) / build

unused variable: `origin_page`

Check warning on line 85 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test turbopack production (5/5) / build

unused variable: `origin_page`

Check warning on line 85 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test ppr prod (4/4) / build

unused variable: `origin_page`
// remove the last /route segment of page
page.0.pop();

let _ = if is_multi_dynamic {
// push /[__metadata_id__] to the page

page.push(PageSegment::Dynamic("__metadata_id__".into()))
// Ok(())
} else {
// if page last segment is sitemap, change to sitemap.xml
if page.last() == Some(&PageSegment::Static("sitemap".into())) {
Expand All @@ -99,11 +103,12 @@ pub async fn get_app_metadata_route_entry(
// Push /route back
let _ = page.push(PageSegment::PageType(PageType::Route));

println!("get_app_route_entry:page: {:?}", page.to_string());
get_app_route_entry(
nodejs_context,
edge_context,
get_app_metadata_route_source(page.clone(), mode, metadata),
page.clone(),
get_app_metadata_route_source(page.clone(), mode, metadata, is_multi_dynamic),
page,
project_root,
Some(segment_config),
next_config,
Expand Down Expand Up @@ -237,15 +242,18 @@ async fn dynamic_text_route_source(path: Vc<FileSystemPath>) -> Result<Vc<Box<dy
async fn dynamic_site_map_route_source(
mode: NextMode,
path: Vc<FileSystemPath>,
page: AppPage,
_page: AppPage,
is_multi_dynamic: bool,
) -> Result<Vc<Box<dyn Source>>> {
let stem = path.file_stem().await?;
let stem = stem.as_deref().unwrap_or_default();
let ext = &*path.extension().await?;
let content_type = get_content_type(path).await?;
let mut static_generation_code = "";

if mode.is_production() && page.contains(&PageSegment::Dynamic("__metadata_id__".into())) {
// if mode.is_production() &&
// page.contains(&PageSegment::Dynamic("__metadata_id__".into())) {
if mode.is_production() && is_multi_dynamic {
static_generation_code = indoc! {
r#"
export async function generateStaticParams() {
Expand All @@ -261,6 +269,12 @@ async fn dynamic_site_map_route_source(
};
}

let file_name = if is_multi_dynamic {

Check warning on line 272 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / rustdoc check / build

unused variable: `file_name`

Check warning on line 272 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / rust check / build

unused variable: `file_name`

Check failure on line 272 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / rust check / build

unused variable: `file_name`

Check warning on line 272 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / build-native / build

unused variable: `file_name`

Check warning on line 272 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / build / build

unused variable: `file_name`

Check warning on line 272 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test cargo unit / build

unused variable: `file_name`

Check warning on line 272 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / types and precompiled / build

unused variable: `file_name`

Check warning on line 272 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test unit (18) / build

unused variable: `file_name`

Check warning on line 272 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test ppr integration / build

unused variable: `file_name`

Check warning on line 272 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / lint / build

unused variable: `file_name`

Check warning on line 272 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test unit (20) / build

unused variable: `file_name`

Check warning on line 272 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test turbopack dev (3/5) / build

unused variable: `file_name`

Check warning on line 272 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test turbopack development integration (3/5) / build

unused variable: `file_name`

Check warning on line 272 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test next-swc wasm / build

unused variable: `file_name`

Check warning on line 272 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test turbopack development integration (1/5) / build

unused variable: `file_name`

Check warning on line 272 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test firefox and safari / build

unused variable: `file_name`

Check warning on line 272 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test dev (2/4) / build

unused variable: `file_name`

Check warning on line 272 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test integration (8/12) / build

unused variable: `file_name`

Check warning on line 272 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test prod (4/5) / build

unused variable: `file_name`

Check warning on line 272 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / Test new tests for flakes (dev) / build

unused variable: `file_name`

Check warning on line 272 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / Test new tests for flakes (prod) / build

unused variable: `file_name`

Check warning on line 272 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test turbopack development integration (4/5) / build

unused variable: `file_name`

Check warning on line 272 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test turbopack production (2/5) / build

unused variable: `file_name`

Check warning on line 272 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test turbopack dev (1/5) / build

unused variable: `file_name`

Check warning on line 272 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test dev (4/4) / build

unused variable: `file_name`

Check warning on line 272 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test turbopack production (1/5) / build

unused variable: `file_name`

Check warning on line 272 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test turbopack dev (4/5) / build

unused variable: `file_name`

Check warning on line 272 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test turbopack dev (5/5) / build

unused variable: `file_name`

Check warning on line 272 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test turbopack dev (2/5) / build

unused variable: `file_name`

Check warning on line 272 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test turbopack production (3/5) / build

unused variable: `file_name`

Check warning on line 272 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test turbopack production (4/5) / build

unused variable: `file_name`

Check warning on line 272 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test turbopack production (5/5) / build

unused variable: `file_name`

Check warning on line 272 in packages/next-swc/crates/next-core/src/next_app/metadata/route.rs

View workflow job for this annotation

GitHub Actions / test ppr prod (4/4) / build

unused variable: `file_name`
// append /[__metadata_id__] to the file path after stem
format!("{}/[__metadata_id__]", stem)
} else {
stem.to_string()
};
let code = formatdoc! {
r#"
import {{ NextResponse }} from 'next/server'
Expand Down
4 changes: 3 additions & 1 deletion packages/next/src/server/dev/turbopack-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,9 @@ export async function handleRouteType({

const type = writtenEndpoint?.type

await manifestLoader.loadAppPathsManifest(page)
await manifestLoader.loadAppPathsManifest(
page.replace('/[__metadata_id__]', '')
)
if (type === 'edge') {
await manifestLoader.loadMiddlewareManifest(page, 'app')
} else {
Expand Down

0 comments on commit 84793be

Please sign in to comment.