Skip to content

Commit

Permalink
change emit collectible to require ResolvedVc
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Dec 9, 2024
1 parent 7ce5b8d commit 89bda61
Show file tree
Hide file tree
Showing 51 changed files with 426 additions and 249 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions crates/next-api/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ impl Project {
async fn collect_project_feature_telemetry(self: Vc<Self>) -> Result<Vc<()>> {
let emit_event = |feature_name: &str, enabled: bool| {
NextFeatureTelemetry::new(feature_name.into(), enabled)
.cell()
.resolved_cell()
.emit();
};

Expand Down Expand Up @@ -898,7 +898,7 @@ impl Project {
.resolved_cell(),
severity: IssueSeverity::Error.resolved_cell(),
}
.cell()
.resolved_cell()
.emit();
*entry.get_mut() = Route::Conflict;
}
Expand Down
186 changes: 124 additions & 62 deletions crates/next-core/src/app_segment_config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::ops::Deref;
use std::{future::Future, ops::Deref};

use anyhow::{bail, Result};
use serde::{Deserialize, Serialize};
Expand All @@ -8,7 +8,9 @@ use swc_core::{
ecma::ast::{Decl, Expr, FnExpr, Ident, Program},
};
use turbo_rcstr::RcStr;
use turbo_tasks::{trace::TraceRawVcs, ResolvedVc, TryJoinIterExt, ValueDefault, Vc};
use turbo_tasks::{
trace::TraceRawVcs, util::WrapFuture, ResolvedVc, TryJoinIterExt, ValueDefault, Vc,
};
use turbo_tasks_fs::FileSystemPath;
use turbopack_core::{
file_source::FileSource,
Expand Down Expand Up @@ -292,43 +294,49 @@ pub async fn parse_segment_config_from_source(
return Ok(Default::default());
};

let config = GLOBALS.set(globals, || {
let mut config = NextSegmentConfig::default();

for item in &module_ast.body {
let Some(export_decl) = item
.as_module_decl()
.and_then(|mod_decl| mod_decl.as_export_decl())
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);
let config = WrapFuture::new(
async {
let mut config = NextSegmentConfig::default();

for item in &module_ast.body {
let Some(export_decl) = item
.as_module_decl()
.and_then(|mod_decl| mod_decl.as_export_decl())
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)
.await?;
}
}
}
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).await?;
}
_ => {}
}
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
});
anyhow::Ok(config)
},
|f, ctx| GLOBALS.set(globals, || f.poll(ctx)),
)
.await?;

Ok(config.cell())
}
Expand All @@ -337,44 +345,70 @@ fn issue_source(source: Vc<Box<dyn Source>>, span: Span) -> Vc<IssueSource> {
IssueSource::from_swc_offsets(source, span.lo.to_usize(), span.hi.to_usize())
}

fn parse_config_value(
async fn parse_config_value(
source: Vc<Box<dyn Source>>,
config: &mut NextSegmentConfig,
ident: &Ident,
init: &Expr,
eval_context: &EvalContext,
) {
) -> Result<()> {
let span = init.span();
let invalid_config = |detail: &str, value: &JsValue| {
async fn invalid_config(
source: Vc<Box<dyn Source>>,
span: Span,
detail: &str,
value: &JsValue,
) -> Result<()> {
let (explainer, hints) = value.explain(2, 0);
let detail =
StyledString::Text(format!("{detail} Got {explainer}.{hints}").into()).resolved_cell();

NextSegmentConfigParsingIssue::new(source.ident(), *detail, issue_source(source, span))
.to_resolved()
.await?
.emit();
};
Ok(())
}

match &*ident.sym {
"dynamic" => {
let value = eval_context.eval(init);
let Some(val) = value.as_str() else {
invalid_config("`dynamic` needs to be a static string", &value);
return;
invalid_config(
source,
span,
"`dynamic` needs to be a static string",
&value,
)
.await?;
return Ok(());
};

config.dynamic = match serde_json::from_value(Value::String(val.to_string())) {
Ok(dynamic) => Some(dynamic),
Err(err) => {
invalid_config(&format!("`dynamic` has an invalid value: {}", err), &value);
return;
invalid_config(
source,
span,
&format!("`dynamic` has an invalid value: {}", err),
&value,
)
.await?;
return Ok(());
}
};
}
"dynamicParams" => {
let value = eval_context.eval(init);
let Some(val) = value.as_bool() else {
invalid_config("`dynamicParams` needs to be a static boolean", &value);
return;
invalid_config(
source,
span,
"`dynamicParams` needs to be a static boolean",
&value,
)
.await?;
return Ok(());
};

config.dynamic_params = Some(val);
Expand Down Expand Up @@ -402,33 +436,50 @@ fn parse_config_value(
"fetchCache" => {
let value = eval_context.eval(init);
let Some(val) = value.as_str() else {
invalid_config("`fetchCache` needs to be a static string", &value);
return;
return invalid_config(
source,
span,
"`fetchCache` needs to be a static string",
&value,
)
.await;
};

config.fetch_cache = match serde_json::from_value(Value::String(val.to_string())) {
Ok(fetch_cache) => Some(fetch_cache),
Err(err) => {
invalid_config(
return invalid_config(
source,
span,
&format!("`fetchCache` has an invalid value: {}", err),
&value,
);
return;
)
.await;
}
};
}
"runtime" => {
let value = eval_context.eval(init);
let Some(val) = value.as_str() else {
invalid_config("`runtime` needs to be a static string", &value);
return;
return invalid_config(
source,
span,
"`runtime` needs to be a static string",
&value,
)
.await;
};

config.runtime = match serde_json::from_value(Value::String(val.to_string())) {
Ok(runtime) => Some(runtime),
Err(err) => {
invalid_config(&format!("`runtime` has an invalid value: {}", err), &value);
return;
return invalid_config(
source,
span,
&format!("`runtime` has an invalid value: {}", err),
&value,
)
.await;
}
};
}
Expand All @@ -446,21 +497,25 @@ fn parse_config_value(
if let JsValue::Constant(ConstantValue::Str(str)) = item {
regions.push(str.to_string().into());
} else {
invalid_config(
return invalid_config(
source,
span,
"Values of the `preferredRegion` array need to static strings",
&item,
);
return;
)
.await;
}
}
regions
}
_ => {
invalid_config(
return invalid_config(
source,
span,
"`preferredRegion` needs to be a static string or array of static strings",
&value,
);
return;
)
.await;
}
};

Expand All @@ -477,14 +532,21 @@ fn parse_config_value(
"experimental_ppr" => {
let value = eval_context.eval(init);
let Some(val) = value.as_bool() else {
invalid_config("`experimental_ppr` needs to be a static boolean", &value);
return;
return invalid_config(
source,
span,
"`experimental_ppr` needs to be a static boolean",
&value,
)
.await;
};

config.experimental_ppr = Some(val);
}
_ => {}
}

Ok(())
}

#[turbo_tasks::function]
Expand Down
4 changes: 2 additions & 2 deletions crates/next-core/src/app_structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ fn conflict_issue(
.resolved_cell(),
severity: IssueSeverity::Error.resolved_cell(),
}
.cell()
.resolved_cell()
.emit();
}

Expand Down Expand Up @@ -803,7 +803,7 @@ async fn check_duplicate(
app_dir: app_dir.to_resolved().await?,
page: loader_tree.page.clone(),
}
.cell()
.resolved_cell()
.emit();
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/next-core/src/next_font/google/font_fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub(super) async fn get_font_fallback(
.resolved_cell(),
severity: IssueSeverity::Warning.resolved_cell(),
}
.cell()
.resolved_cell()
.emit();
FontFallback::Error.cell()
}
Expand Down
2 changes: 2 additions & 0 deletions crates/next-core/src/next_font/google/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,8 @@ async fn fetch_from_google_fonts(
// TODO(WEB-283): Use fallback in dev in this case
// TODO(WEB-293): Fail production builds (not dev) in this case
err.to_issue(IssueSeverity::Warning.into(), virtual_path)
.to_resolved()
.await?
.emit();

None
Expand Down
2 changes: 1 addition & 1 deletion crates/next-core/src/next_font/local/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl BeforeResolvePlugin for NextFontLocalResolvePlugin {
origin_path: lookup_path,
font_path: ResolvedVc::cell(font_path.clone()),
}
.cell()
.resolved_cell()
.emit();

return Ok(ResolveResultOption::some(
Expand Down
2 changes: 1 addition & 1 deletion crates/next-core/src/next_server/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ impl AfterResolvePlugin for ExternalCjsModulesResolvePlugin {
request_str: request_str.clone(),
reason,
}
.cell()
.resolved_cell()
.emit();
}
Ok(ResolveResultOption::none())
Expand Down
4 changes: 2 additions & 2 deletions crates/next-core/src/next_shared/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl BeforeResolvePlugin for InvalidImportResolvePlugin {
// styled-jsx specific resolve error has its own message
skip_context_message: self.invalid_import == "styled-jsx",
}
.cell()
.resolved_cell()
.emit();

ResolveResultOption::some(
Expand Down Expand Up @@ -383,7 +383,7 @@ impl BeforeResolvePlugin for ModuleFeatureReportResolvePlugin {

if let Some(sub_path) = sub_path {
ModuleFeatureTelemetry::new(format!("{}{}", module, sub_path).into(), 1)
.cell()
.resolved_cell()
.emit();
}
}
Expand Down
Loading

0 comments on commit 89bda61

Please sign in to comment.