Skip to content

Commit

Permalink
feat(custom-transforms): partial page-static-info visitors (#63741)
Browse files Browse the repository at this point in the history
### What

Supports partial `get-page-static-info` in turbopack. Since turbopack
doesn't have equivalent place to webpack's ondemandhandler, it uses
turbopack's build time transform rule instead.

As noted, this is partial implementation to pagestatic info as it does
not have existing js side evaluations. Assertions will be added
gradually to ensure regressions, for now having 1 assertion for
getstaticparams.

Closes PACK-2849
  • Loading branch information
kwonoj committed Mar 28, 2024
1 parent 94749f0 commit 493130b
Show file tree
Hide file tree
Showing 12 changed files with 917 additions and 8 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.

10 changes: 8 additions & 2 deletions packages/next-swc/crates/next-core/src/next_client/transforms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ use crate::{
get_server_actions_transform_rule, next_amp_attributes::get_next_amp_attr_rule,
next_cjs_optimizer::get_next_cjs_optimizer_rule,
next_disallow_re_export_all_in_page::get_next_disallow_export_all_in_page_rule,
next_page_config::get_next_page_config_rule, next_pure::get_next_pure_rule,
server_actions::ActionsTransform,
next_page_config::get_next_page_config_rule,
next_page_static_info::get_next_page_static_info_assert_rule,
next_pure::get_next_pure_rule, server_actions::ActionsTransform,
},
};

Expand Down Expand Up @@ -76,6 +77,11 @@ pub async fn get_next_client_transforms_rules(
rules.push(get_next_dynamic_transform_rule(false, false, pages_dir, mode, mdx_rs).await?);

rules.push(get_next_image_rule());
rules.push(get_next_page_static_info_assert_rule(
mdx_rs,
None,
Some(context_ty),
));
}

Ok(rules)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub(crate) mod next_font;
pub(crate) mod next_middleware_dynamic_assert;
pub(crate) mod next_optimize_server_react;
pub(crate) mod next_page_config;
pub(crate) mod next_page_static_info;
pub(crate) mod next_pure;
pub(crate) mod next_react_server_components;
pub(crate) mod next_shake_exports;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
use anyhow::Result;
use async_trait::async_trait;
use next_custom_transforms::transforms::page_static_info::collect_exports;
use turbo_tasks::Vc;
use turbo_tasks_fs::FileSystemPath;
use turbopack_binding::{
swc::core::ecma::ast::Program,
turbopack::{
core::issue::{
Issue, IssueExt, IssueSeverity, IssueStage, OptionStyledString, StyledString,
},
ecmascript::{CustomTransformer, EcmascriptInputTransform, TransformContext},
turbopack::module_options::{ModuleRule, ModuleRuleEffect},
},
};

use super::module_rule_match_js_no_url;
use crate::{next_client::ClientContextType, next_server::ServerContextType};

/// Create a rule to run assertions for the page-static-info.
/// This assertion is partial implementation to the original
/// (analysis/get-page-static-info) Due to not able to bring all the evaluations
/// in the js implementation,
pub fn get_next_page_static_info_assert_rule(
enable_mdx_rs: bool,
server_context: Option<ServerContextType>,
client_context: Option<ClientContextType>,
) -> ModuleRule {
let transformer = EcmascriptInputTransform::Plugin(Vc::cell(Box::new(NextPageStaticInfo {
server_context,
client_context,
}) as _));
ModuleRule::new(
module_rule_match_js_no_url(enable_mdx_rs),
vec![ModuleRuleEffect::ExtendEcmascriptTransforms {
prepend: Vc::cell(vec![transformer]),
append: Vc::cell(vec![]),
}],
)
}

#[derive(Debug)]
struct NextPageStaticInfo {
server_context: Option<ServerContextType>,
client_context: Option<ClientContextType>,
}

#[async_trait]
impl CustomTransformer for NextPageStaticInfo {
async fn transform(&self, program: &mut Program, ctx: &TransformContext<'_>) -> Result<()> {
if let Some(collected_exports) = collect_exports(program)? {
let mut properties_to_extract = collected_exports.extra_properties.clone();
properties_to_extract.insert("config".to_string());

let is_app_page =
matches!(
self.server_context,
Some(ServerContextType::AppRSC { .. }) | Some(ServerContextType::AppSSR { .. })
) || matches!(self.client_context, Some(ClientContextType::App { .. }));

if collected_exports.directives.contains("client")
&& collected_exports.generate_static_params
&& is_app_page
{
PageStaticInfoIssue {
file_path: ctx.file_path,
messages: vec![format!(r#"Page "{}" cannot use both "use client" and export function "generateStaticParams()"."#, ctx.file_path_str)],
}
.cell()
.emit();
}
}

Ok(())
}
}

#[turbo_tasks::value(shared)]
pub struct PageStaticInfoIssue {
pub file_path: Vc<FileSystemPath>,
pub messages: Vec<String>,
}

#[turbo_tasks::value_impl]
impl Issue for PageStaticInfoIssue {
#[turbo_tasks::function]
fn severity(&self) -> Vc<IssueSeverity> {
IssueSeverity::Error.into()
}

#[turbo_tasks::function]
fn stage(&self) -> Vc<IssueStage> {
IssueStage::Transform.into()
}

#[turbo_tasks::function]
fn title(&self) -> Vc<StyledString> {
StyledString::Text("Invalid page configuration".into()).cell()
}

#[turbo_tasks::function]
fn file_path(&self) -> Vc<FileSystemPath> {
self.file_path
}

#[turbo_tasks::function]
async fn description(&self) -> Result<Vc<OptionStyledString>> {
Ok(Vc::cell(Some(
StyledString::Line(
self.messages
.iter()
.map(|v| StyledString::Text(format!("{}\n", v)))
.collect::<Vec<StyledString>>(),
)
.cell(),
)))
}
}
2 changes: 2 additions & 0 deletions packages/next-swc/crates/next-custom-transforms/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ serde = { workspace = true }
serde_json = { workspace = true, features = ["preserve_order"] }
sha1 = "0.10.1"
tracing = { version = "0.1.37" }
anyhow = { workspace = true }
lazy_static = { workspace = true }

turbopack-binding = { workspace = true, features = [
"__swc_core",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub mod middleware_dynamic;
pub mod next_ssg;
pub mod optimize_server_react;
pub mod page_config;
pub mod page_static_info;
pub mod pure;
pub mod react_server_components;
pub mod server_actions;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
use std::collections::{HashMap, HashSet};

use serde_json::{Map, Number, Value};
use turbopack_binding::swc::core::{
common::{Mark, SyntaxContext},
ecma::{
ast::{
BindingIdent, Decl, ExportDecl, Expr, Lit, ModuleDecl, ModuleItem, Pat, Prop, PropName,
PropOrSpread, VarDecl, VarDeclKind, VarDeclarator,
},
utils::{ExprCtx, ExprExt},
visit::{Visit, VisitWith},
},
};

/// The values extracted for the corresponding AST node.
/// refer extract_expored_const_values for the supported value types.
/// Undefined / null is treated as None.
pub enum Const {
Value(Value),
Unsupported(String),
}

pub(crate) struct CollectExportedConstVisitor {
pub properties: HashMap<String, Option<Const>>,
expr_ctx: ExprCtx,
}

impl CollectExportedConstVisitor {
pub fn new(properties_to_extract: HashSet<String>) -> Self {
Self {
properties: properties_to_extract
.into_iter()
.map(|p| (p, None))
.collect(),
expr_ctx: ExprCtx {
unresolved_ctxt: SyntaxContext::empty().apply_mark(Mark::new()),
is_unresolved_ref_safe: false,
},
}
}
}

impl Visit for CollectExportedConstVisitor {
fn visit_module_items(&mut self, module_items: &[ModuleItem]) {
for module_item in module_items {
if let ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(ExportDecl {
decl: Decl::Var(decl),
..
})) = module_item
{
let VarDecl { kind, decls, .. } = &**decl;
if kind == &VarDeclKind::Const {
for decl in decls {
if let VarDeclarator {
name: Pat::Ident(BindingIdent { id, .. }),
init: Some(init),
..
} = decl
{
let id = id.sym.as_ref();
if let Some(prop) = self.properties.get_mut(id) {
*prop = extract_value(&self.expr_ctx, init, id.to_string());
};
}
}
}
}
}

module_items.visit_children_with(self);
}
}

/// Coerece the actual value of the given ast node.
fn extract_value(ctx: &ExprCtx, init: &Expr, id: String) -> Option<Const> {
match init {
init if init.is_undefined(ctx) => Some(Const::Value(Value::Null)),
Expr::Ident(ident) => Some(Const::Unsupported(format!(
"Unknown identifier \"{}\" at \"{}\".",
ident.sym, id
))),
Expr::Lit(lit) => match lit {
Lit::Num(num) => Some(Const::Value(Value::Number(
Number::from_f64(num.value).expect("Should able to convert f64 to Number"),
))),
Lit::Null(_) => Some(Const::Value(Value::Null)),
Lit::Str(s) => Some(Const::Value(Value::String(s.value.to_string()))),
Lit::Bool(b) => Some(Const::Value(Value::Bool(b.value))),
Lit::Regex(r) => Some(Const::Value(Value::String(format!(
"/{}/{}",
r.exp, r.flags
)))),
_ => Some(Const::Unsupported("Unsupported Literal".to_string())),
},
Expr::Array(arr) => {
let mut a = vec![];

for elem in &arr.elems {
match elem {
Some(elem) => {
if elem.spread.is_some() {
return Some(Const::Unsupported(format!(
"Unsupported spread operator in the Array Expression at \"{}\"",
id
)));
}

match extract_value(ctx, &elem.expr, id.clone()) {
Some(Const::Value(value)) => a.push(value),
Some(Const::Unsupported(message)) => {
return Some(Const::Unsupported(format!(
"Unsupported value in the Array Expression: {message}"
)))
}
_ => {
return Some(Const::Unsupported(
"Unsupported value in the Array Expression".to_string(),
))
}
}
}
None => {
a.push(Value::Null);
}
}
}

Some(Const::Value(Value::Array(a)))
}
Expr::Object(obj) => {
let mut o = Map::new();

for prop in &obj.props {
let (key, value) = match prop {
PropOrSpread::Prop(box Prop::KeyValue(kv)) => (
match &kv.key {
PropName::Ident(i) => i.sym.as_ref(),
PropName::Str(s) => s.value.as_ref(),
_ => {
return Some(Const::Unsupported(format!(
"Unsupported key type in the Object Expression at \"{}\"",
id
)))
}
},
&kv.value,
),
_ => {
return Some(Const::Unsupported(format!(
"Unsupported spread operator in the Object Expression at \"{}\"",
id
)))
}
};
let new_value = extract_value(ctx, value, format!("{}.{}", id, key));
if let Some(Const::Unsupported(msg)) = new_value {
return Some(Const::Unsupported(msg));
}

if let Some(Const::Value(value)) = new_value {
o.insert(key.to_string(), value);
}
}

Some(Const::Value(Value::Object(o)))
}
Expr::Tpl(tpl) => {
// [TODO] should we add support for `${'e'}d${'g'}'e'`?
if !tpl.exprs.is_empty() {
Some(Const::Unsupported(format!(
"Unsupported template literal with expressions at \"{}\".",
id
)))
} else {
Some(
tpl.quasis
.first()
.map(|q| {
// When TemplateLiteral has 0 expressions, the length of quasis is
// always 1. Because when parsing
// TemplateLiteral, the parser yields the first quasi,
// then the first expression, then the next quasi, then the next
// expression, etc., until the last quasi.
// Thus if there is no expression, the parser ends at the frst and also
// last quasis
//
// A "cooked" interpretation where backslashes have special meaning,
// while a "raw" interpretation where
// backslashes do not have special meaning https://exploringjs.com/impatient-js/ch_template-literals.html#template-strings-cooked-vs-raw
let cooked = q.cooked.as_ref();
let raw = q.raw.as_ref();

Const::Value(Value::String(
cooked.map(|c| c.to_string()).unwrap_or(raw.to_string()),
))
})
.unwrap_or(Const::Unsupported(format!(
"Unsupported node type at \"{}\"",
id
))),
)
}
}
_ => Some(Const::Unsupported(format!(
"Unsupported node type at \"{}\"",
id
))),
}
}
Loading

0 comments on commit 493130b

Please sign in to comment.