Unverified Commit 655227f4 authored by OJ Kwon's avatar OJ Kwon Committed by GitHub

Revert "refactor(analysis): rust based page-static-info, deprecate js parse…

Revert "refactor(analysis): rust based page-static-info, deprecate js parse interface in next-swc" (#62838)

Reverts vercel/next.js#61832

Closes PACK-2654
parent c4a5bf5b
......@@ -3341,13 +3341,11 @@ dependencies = [
name = "next-custom-transforms"
version = "0.0.0"
dependencies = [
"anyhow",
"chrono",
"easy-error",
"either",
"fxhash",
"hex",
"lazy_static",
"once_cell",
"pathdiff",
"preset_env_base",
......@@ -3382,12 +3380,9 @@ dependencies = [
"next-build",
"next-core",
"next-custom-transforms",
"once_cell",
"regex",
"serde",
"serde_json",
"shadow-rs",
"swc_core",
"tokio",
"tracing",
"tracing-chrome",
......@@ -8541,9 +8536,6 @@ dependencies = [
"getrandom",
"js-sys",
"next-custom-transforms",
"once_cell",
"regex",
"serde",
"serde-wasm-bindgen",
"serde_json",
"swc_core",
......
......@@ -76,8 +76,6 @@ napi = { version = "2", default-features = false, features = [
napi-derive = "2"
next-custom-transforms = { workspace = true }
once_cell = { workspace = true }
regex = { workspace = true }
serde = "1"
serde_json = "1"
shadow-rs = { workspace = true }
......@@ -111,7 +109,6 @@ turbopack-binding = { workspace = true, features = [
[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2.9", default-features = false, features = ["js"] }
iana-time-zone = { version = "*", features = ["fallback"] }
swc_core = { workspace = true, features = ["ecma_visit_path"] }
turbopack-binding = { workspace = true, features = [
"__swc_core_binding_napi",
"__swc_core_serde",
......
This diff is collapsed.
......@@ -16,8 +16,6 @@ easy-error = "1.0.0"
either = "1"
fxhash = "0.2.1"
hex = "0.4.3"
anyhow = { workspace = true }
lazy_static = { workspace = true }
once_cell = { workspace = true }
pathdiff = { workspace = true }
regex = "1.5"
......
......@@ -8,7 +8,6 @@ pub mod import_analyzer;
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;
......
use std::collections::{HashMap, HashSet};
use serde_json::{Map, Number, Value};
use swc_core::{
common::{pass::AstNodePath, Mark, SyntaxContext},
ecma::{
ast::{
BindingIdent, Decl, ExportDecl, Expr, Lit, Pat, Prop, PropName, PropOrSpread, VarDecl,
VarDeclKind, VarDeclarator,
},
utils::{ExprCtx, ExprExt},
visit::{AstParentNodeRef, VisitAstPath, VisitWithPath},
},
};
/// 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 VisitAstPath for CollectExportedConstVisitor {
fn visit_export_decl<'ast: 'r, 'r>(
&mut self,
export_decl: &'ast ExportDecl,
ast_path: &mut AstNodePath<AstParentNodeRef<'r>>,
) {
match &export_decl.decl {
Decl::Var(box VarDecl { kind, decls, .. }) 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());
};
}
}
}
_ => {}
}
export_decl.visit_children_with_path(self, ast_path);
}
}
/// 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
))),
}
}
use std::collections::HashSet;
use lazy_static::lazy_static;
use swc_core::ecma::{
ast::{
Decl, ExportDecl, ExportNamedSpecifier, ExportSpecifier, Expr, ExprOrSpread, ExprStmt, Lit,
ModuleExportName, ModuleItem, NamedExport, Pat, Stmt, Str, VarDeclarator,
},
visit::{Visit, VisitWith},
};
use super::{ExportInfo, ExportInfoWarning};
lazy_static! {
static ref EXPORTS_SET: HashSet<&'static str> = HashSet::from([
"getStaticProps",
"getServerSideProps",
"generateImageMetadata",
"generateSitemaps",
"generateStaticParams",
]);
}
pub(crate) struct CollectExportsVisitor {
pub export_info: Option<ExportInfo>,
}
impl CollectExportsVisitor {
pub fn new() -> Self {
Self {
export_info: Default::default(),
}
}
}
impl Visit for CollectExportsVisitor {
fn visit_module_items(&mut self, stmts: &[swc_core::ecma::ast::ModuleItem]) {
let mut is_directive = true;
for stmt in stmts {
if let ModuleItem::Stmt(Stmt::Expr(ExprStmt {
expr: box Expr::Lit(Lit::Str(Str { value, .. })),
..
})) = stmt
{
if is_directive {
if value == "use server" {
let export_info = self.export_info.get_or_insert(Default::default());
export_info.directives.insert("server".to_string());
}
if value == "use client" {
let export_info = self.export_info.get_or_insert(Default::default());
export_info.directives.insert("client".to_string());
}
}
} else {
is_directive = false;
}
stmt.visit_children_with(self);
}
}
fn visit_export_decl(&mut self, export_decl: &ExportDecl) {
match &export_decl.decl {
Decl::Var(box var_decl) => {
if let Some(VarDeclarator {
name: Pat::Ident(name),
..
}) = var_decl.decls.first()
{
if EXPORTS_SET.contains(&name.sym.as_str()) {
let export_info = self.export_info.get_or_insert(Default::default());
export_info.ssg = name.sym == "getStaticProps";
export_info.ssr = name.sym == "getServerSideProps";
export_info.generate_image_metadata =
Some(name.sym == "generateImageMetadata");
export_info.generate_sitemaps = Some(name.sym == "generateSitemaps");
export_info.generate_static_params = name.sym == "generateStaticParams";
}
}
for decl in &var_decl.decls {
if let Pat::Ident(id) = &decl.name {
if id.sym == "runtime" {
let export_info = self.export_info.get_or_insert(Default::default());
export_info.runtime = decl.init.as_ref().and_then(|init| {
if let Expr::Lit(Lit::Str(Str { value, .. })) = &**init {
Some(value.to_string())
} else {
None
}
})
} else if id.sym == "preferredRegion" {
if let Some(init) = &decl.init {
if let Expr::Array(arr) = &**init {
for expr in arr.elems.iter().flatten() {
if let ExprOrSpread {
expr: box Expr::Lit(Lit::Str(Str { value, .. })),
..
} = expr
{
let export_info =
self.export_info.get_or_insert(Default::default());
export_info.preferred_region.push(value.to_string());
}
}
} else if let Expr::Lit(Lit::Str(Str { value, .. })) = &**init {
let export_info =
self.export_info.get_or_insert(Default::default());
export_info.preferred_region.push(value.to_string());
}
}
} else {
let export_info = self.export_info.get_or_insert(Default::default());
export_info.extra_properties.insert(id.sym.to_string());
}
}
}
}
Decl::Fn(fn_decl) => {
let id = &fn_decl.ident;
let export_info = self.export_info.get_or_insert(Default::default());
export_info.ssg = id.sym == "getStaticProps";
export_info.ssr = id.sym == "getServerSideProps";
export_info.generate_image_metadata = Some(id.sym == "generateImageMetadata");
export_info.generate_sitemaps = Some(id.sym == "generateSitemaps");
export_info.generate_static_params = id.sym == "generateStaticParams";
}
_ => {}
}
export_decl.visit_children_with(self);
}
fn visit_named_export(&mut self, named_export: &NamedExport) {
for specifier in &named_export.specifiers {
if let ExportSpecifier::Named(ExportNamedSpecifier {
orig: ModuleExportName::Ident(value),
..
}) = specifier
{
let export_info = self.export_info.get_or_insert(Default::default());
if !export_info.ssg && value.sym == "getStaticProps" {
export_info.ssg = true;
}
if !export_info.ssr && value.sym == "getServerSideProps" {
export_info.ssr = true;
}
if !export_info.generate_image_metadata.unwrap_or_default()
&& value.sym == "generateImageMetadata"
{
export_info.generate_image_metadata = Some(true);
}
if !export_info.generate_sitemaps.unwrap_or_default()
&& value.sym == "generateSitemaps"
{
export_info.generate_sitemaps = Some(true);
}
if !export_info.generate_static_params && value.sym == "generateStaticParams" {
export_info.generate_static_params = true;
}
if export_info.runtime.is_none() && value.sym == "runtime" {
export_info.warnings.push(ExportInfoWarning::new(
value.sym.to_string(),
"it was not assigned to a string literal".to_string(),
));
}
if export_info.preferred_region.is_empty() && value.sym == "preferredRegion" {
export_info.warnings.push(ExportInfoWarning::new(
value.sym.to_string(),
"it was not assigned to a string literal or an array of string literals"
.to_string(),
));
}
}
}
named_export.visit_children_with(self);
}
}
......@@ -17,9 +17,7 @@ plugin = ["turbopack-binding/__swc_core_binding_wasm_plugin"]
workspace = true
[dependencies]
anyhow = { workspace = true }
once_cell = { workspace = true }
serde = { workspace = true }
anyhow = "1.0.66"
console_error_panic_hook = "0.1.6"
next-custom-transforms = { workspace = true }
serde_json = "1"
......@@ -32,8 +30,8 @@ turbopack-binding = { workspace = true, features = [
"__swc_core_binding_wasm",
"__feature_mdx_rs",
] }
swc_core = { workspace = true, features = ["ecma_ast_serde", "common", "ecma_visit_path"] }
regex = "1.5"
swc_core = { workspace = true, features = ["ecma_ast_serde", "common"] }
# Workaround a bug
[package.metadata.wasm-pack.profile.release]
......
use std::{collections::HashMap, sync::Arc};
use std::sync::Arc;
use anyhow::{Context, Error};
use js_sys::JsString;
use next_custom_transforms::{
chain_transforms::{custom_before_pass, TransformOptions},
transforms::page_static_info::{
build_ast_from_source, collect_exports, collect_rsc_module_info,
extract_expored_const_values, Const, ExportInfo, RscModuleInfo,
},
};
use once_cell::sync::Lazy;
use regex::Regex;
use serde::{Deserialize, Serialize};
use next_custom_transforms::chain_transforms::{custom_before_pass, TransformOptions};
use swc_core::common::Mark;
use turbopack_binding::swc::core::{
base::{config::JsMinifyOptions, try_with_handler, Compiler},
base::{
config::{JsMinifyOptions, ParseOptions},
try_with_handler, Compiler,
},
common::{
comments::SingleThreadedComments, errors::ColorConfig, FileName, FilePathMapping,
SourceMap, GLOBALS,
comments::{Comments, SingleThreadedComments},
errors::ColorConfig,
FileName, FilePathMapping, SourceMap, GLOBALS,
},
ecma::transforms::base::pass::noop,
};
......@@ -26,21 +21,6 @@ use wasm_bindgen_futures::future_to_promise;
pub mod mdx;
/// A regex pattern to determine if is_dynamic_metadata_route should continue to
/// parse the page or short circuit and return false.
static DYNAMIC_METADATA_ROUTE_SHORT_CURCUIT: Lazy<Regex> =
Lazy::new(|| Regex::new("generateImageMetadata|generateSitemaps").unwrap());
/// A regex pattern to determine if get_page_static_info should continue to
/// parse the page or short circuit and return default.
static PAGE_STATIC_INFO_SHORT_CURCUIT: Lazy<Regex> = Lazy::new(|| {
Regex::new(
"runtime|preferredRegion|getStaticProps|getServerSideProps|generateStaticParams|export \
const",
)
.unwrap()
});
fn convert_err(err: Error) -> JsValue {
format!("{:?}", err).into()
}
......@@ -157,107 +137,57 @@ pub fn transform(s: JsValue, opts: JsValue) -> js_sys::Promise {
future_to_promise(async { transform_sync(s, opts) })
}
/// Detect if metadata routes is a dynamic route, which containing
/// generateImageMetadata or generateSitemaps as export
/// Unlike native bindings, caller should provide the contents of the pages
/// sine our wasm bindings does not have access to the file system
#[wasm_bindgen(js_name = "isDynamicMetadataRoute")]
pub fn is_dynamic_metadata_route(page_file_path: String, page_contents: String) -> js_sys::Promise {
// Returning promise to conform existing interfaces
future_to_promise(async move {
if !DYNAMIC_METADATA_ROUTE_SHORT_CURCUIT.is_match(&page_contents) {
return Ok(JsValue::from(false));
}
#[wasm_bindgen(js_name = "parseSync")]
pub fn parse_sync(s: JsString, opts: JsValue) -> Result<JsValue, JsValue> {
console_error_panic_hook::set_once();
let parsed = if let Ok(parsed) = build_ast_from_source(&page_contents, &page_file_path) {
parsed
} else {
return Ok(JsValue::null());
};
let c = turbopack_binding::swc::core::base::Compiler::new(Arc::new(SourceMap::new(
FilePathMapping::empty(),
)));
let opts: ParseOptions = serde_wasm_bindgen::from_value(opts)?;
let (source_ast, _) = parsed;
collect_exports(&source_ast)
.map(|exports_info| {
exports_info
.map(|exports_info| {
JsValue::from(
!exports_info.generate_image_metadata.unwrap_or_default()
|| !exports_info.generate_sitemaps.unwrap_or_default(),
try_with_handler(
c.cm.clone(),
turbopack_binding::swc::core::base::HandlerOpts {
..Default::default()
},
|handler| {
c.run(|| {
GLOBALS.set(&Default::default(), || {
let fm = c.cm.new_source_file(FileName::Anon, s.into());
let cmts = c.comments().clone();
let comments = if opts.comments {
Some(&cmts as &dyn Comments)
} else {
None
};
let program = c
.parse_js(
fm,
handler,
opts.target,
opts.syntax,
opts.is_module,
comments,
)
})
.unwrap_or_default()
})
.map_err(|e| JsValue::from_str(format!("{:?}", e).as_str()))
})
}
.context("failed to parse code")?;
#[derive(Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct StaticPageInfo {
pub exports_info: Option<ExportInfo>,
pub extracted_values: HashMap<String, serde_json::Value>,
pub rsc_info: Option<RscModuleInfo>,
pub warnings: Vec<String>,
let s = serde_json::to_string(&program).unwrap();
Ok(JsValue::from_str(&s))
})
})
},
)
.map_err(convert_err)
}
#[wasm_bindgen(js_name = "getPageStaticInfo")]
pub fn get_page_static_info(page_file_path: String, page_contents: String) -> js_sys::Promise {
future_to_promise(async move {
if !PAGE_STATIC_INFO_SHORT_CURCUIT.is_match(&page_contents) {
return Ok(JsValue::null());
}
let parsed = if let Ok(parsed) = build_ast_from_source(&page_contents, &page_file_path) {
parsed
} else {
return Ok(JsValue::null());
};
let (source_ast, comments) = parsed;
let exports_info = collect_exports(&source_ast)
.map_err(|e| JsValue::from_str(format!("{:?}", e).as_str()))?;
match exports_info {
None => Ok(JsValue::null()),
Some(exports_info) => {
let rsc_info = collect_rsc_module_info(&comments, true);
let mut properties_to_extract = exports_info.extra_properties.clone();
properties_to_extract.insert("config".to_string());
let mut exported_const_values =
extract_expored_const_values(&source_ast, properties_to_extract);
let mut extracted_values = HashMap::new();
let mut warnings = vec![];
for (key, value) in exported_const_values.drain() {
match value {
Some(Const::Value(v)) => {
extracted_values.insert(key.clone(), v);
}
Some(Const::Unsupported(msg)) => {
warnings.push(msg);
}
_ => {}
}
}
let ret = StaticPageInfo {
exports_info: Some(exports_info),
extracted_values,
rsc_info: Some(rsc_info),
warnings,
};
let s = serde_json::to_string(&ret)
.map(|s| JsValue::from_str(&s))
.unwrap_or(JsValue::null());
Ok(s)
}
}
})
#[wasm_bindgen(js_name = "parse")]
pub fn parse(s: JsString, opts: JsValue) -> js_sys::Promise {
// TODO: This'll be properly scheduled once wasm have standard backed thread
// support.
future_to_promise(async { parse_sync(s, opts) })
}
/// Get global sourcemap
......
import type {
ArrayExpression,
BooleanLiteral,
ExportDeclaration,
Identifier,
KeyValueProperty,
Module,
Node,
NullLiteral,
NumericLiteral,
ObjectExpression,
RegExpLiteral,
StringLiteral,
TemplateLiteral,
VariableDeclaration,
} from '@swc/core'
export class NoSuchDeclarationError extends Error {}
function isExportDeclaration(node: Node): node is ExportDeclaration {
return node.type === 'ExportDeclaration'
}
function isVariableDeclaration(node: Node): node is VariableDeclaration {
return node.type === 'VariableDeclaration'
}
function isIdentifier(node: Node): node is Identifier {
return node.type === 'Identifier'
}
function isBooleanLiteral(node: Node): node is BooleanLiteral {
return node.type === 'BooleanLiteral'
}
function isNullLiteral(node: Node): node is NullLiteral {
return node.type === 'NullLiteral'
}
function isStringLiteral(node: Node): node is StringLiteral {
return node.type === 'StringLiteral'
}
function isNumericLiteral(node: Node): node is NumericLiteral {
return node.type === 'NumericLiteral'
}
function isArrayExpression(node: Node): node is ArrayExpression {
return node.type === 'ArrayExpression'
}
function isObjectExpression(node: Node): node is ObjectExpression {
return node.type === 'ObjectExpression'
}
function isKeyValueProperty(node: Node): node is KeyValueProperty {
return node.type === 'KeyValueProperty'
}
function isRegExpLiteral(node: Node): node is RegExpLiteral {
return node.type === 'RegExpLiteral'
}
function isTemplateLiteral(node: Node): node is TemplateLiteral {
return node.type === 'TemplateLiteral'
}
export class UnsupportedValueError extends Error {
/** @example `config.runtime[0].value` */
path?: string
constructor(message: string, paths?: string[]) {
super(message)
// Generating "path" that looks like "config.runtime[0].value"
let codePath: string | undefined
if (paths) {
codePath = ''
for (const path of paths) {
if (path[0] === '[') {
// "array" + "[0]"
codePath += path
} else {
if (codePath === '') {
codePath = path
} else {
// "object" + ".key"
codePath += `.${path}`
}
}
}
}
this.path = codePath
}
}
function extractValue(node: Node, path?: string[]): any {
if (isNullLiteral(node)) {
return null
} else if (isBooleanLiteral(node)) {
// e.g. true / false
return node.value
} else if (isStringLiteral(node)) {
// e.g. "abc"
return node.value
} else if (isNumericLiteral(node)) {
// e.g. 123
return node.value
} else if (isRegExpLiteral(node)) {
// e.g. /abc/i
return new RegExp(node.pattern, node.flags)
} else if (isIdentifier(node)) {
switch (node.value) {
case 'undefined':
return undefined
default:
throw new UnsupportedValueError(
`Unknown identifier "${node.value}"`,
path
)
}
} else if (isArrayExpression(node)) {
// e.g. [1, 2, 3]
const arr = []
for (let i = 0, len = node.elements.length; i < len; i++) {
const elem = node.elements[i]
if (elem) {
if (elem.spread) {
// e.g. [ ...a ]
throw new UnsupportedValueError(
'Unsupported spread operator in the Array Expression',
path
)
}
arr.push(extractValue(elem.expression, path && [...path, `[${i}]`]))
} else {
// e.g. [1, , 2]
// ^^
arr.push(undefined)
}
}
return arr
} else if (isObjectExpression(node)) {
// e.g. { a: 1, b: 2 }
const obj: any = {}
for (const prop of node.properties) {
if (!isKeyValueProperty(prop)) {
// e.g. { ...a }
throw new UnsupportedValueError(
'Unsupported spread operator in the Object Expression',
path
)
}
let key
if (isIdentifier(prop.key)) {
// e.g. { a: 1, b: 2 }
key = prop.key.value
} else if (isStringLiteral(prop.key)) {
// e.g. { "a": 1, "b": 2 }
key = prop.key.value
} else {
throw new UnsupportedValueError(
`Unsupported key type "${prop.key.type}" in the Object Expression`,
path
)
}
obj[key] = extractValue(prop.value, path && [...path, key])
}
return obj
} else if (isTemplateLiteral(node)) {
// e.g. `abc`
if (node.expressions.length !== 0) {
// TODO: should we add support for `${'e'}d${'g'}'e'`?
throw new UnsupportedValueError(
'Unsupported template literal with expressions',
path
)
}
// 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
const [{ cooked, raw }] = node.quasis
return cooked ?? raw
} else {
throw new UnsupportedValueError(
`Unsupported node type "${node.type}"`,
path
)
}
}
/**
* Extracts the value of an exported const variable named `exportedName`
* (e.g. "export const config = { runtime: 'edge' }") from swc's AST.
* The value must be one of (or throws UnsupportedValueError):
* - string
* - boolean
* - number
* - null
* - undefined
* - array containing values listed in this list
* - object containing values listed in this list
*
* Throws NoSuchDeclarationError if the declaration is not found.
*/
export function extractExportedConstValue(
module: Module,
exportedName: string
): any {
for (const moduleItem of module.body) {
if (!isExportDeclaration(moduleItem)) {
continue
}
const declaration = moduleItem.declaration
if (!isVariableDeclaration(declaration)) {
continue
}
if (declaration.kind !== 'const') {
continue
}
for (const decl of declaration.declarations) {
if (
isIdentifier(decl.id) &&
decl.id.value === exportedName &&
decl.init
) {
return extractValue(decl.init, [exportedName])
}
}
}
throw new NoSuchDeclarationError()
}
import LRUCache from 'next/dist/compiled/lru-cache'
import { withPromiseCache } from '../../lib/with-promise-cache'
import { createHash } from 'crypto'
import { parse } from '../swc'
/**
* Parses a module with SWC using an LRU cache where the parsed module will
* be indexed by a sha of its content holding up to 500 entries.
*/
export const parseModule = withPromiseCache(
new LRUCache<string, any>({ max: 500 }),
async (filename: string, content: string) =>
parse(content, { isModule: 'unknown', filename }).catch(() => null),
(_, content) => createHash('sha1').update(content).digest('hex')
)
......@@ -99,7 +99,10 @@ import {
} from '../telemetry/events'
import type { EventBuildFeatureUsage } from '../telemetry/events'
import { Telemetry } from '../telemetry/storage'
import { getPageStaticInfo } from './analysis/get-page-static-info'
import {
isDynamicMetadataRoute,
getPageStaticInfo,
} from './analysis/get-page-static-info'
import { createPagesMapping, getPageFilePath, sortByPageExts } from './entries'
import { PAGE_TYPES } from '../lib/page-types'
import { generateBuildId } from './generate-build-id'
......@@ -748,6 +751,7 @@ export default async function build(
const cacheDir = getCacheDir(distDir)
const telemetry = new Telemetry({ distDir })
setGlobal('telemetry', telemetry)
const publicDir = path.join(dir, 'public')
......@@ -755,8 +759,6 @@ export default async function build(
NextBuildContext.pagesDir = pagesDir
NextBuildContext.appDir = appDir
const binding = await loadBindings(config?.experimental?.useWasmBinary)
const enabledDirectories: NextEnabledDirectories = {
app: typeof appDir === 'string',
pages: typeof pagesDir === 'string',
......@@ -946,10 +948,7 @@ export default async function build(
rootDir,
})
const isDynamic = await binding.analysis.isDynamicMetadataRoute(
pageFilePath
)
const isDynamic = await isDynamicMetadataRoute(pageFilePath)
if (!isDynamic) {
delete mappedAppPages[pageKey]
mappedAppPages[pageKey.replace('[[...__metadata_id__]]/', '')] =
......
......@@ -2,9 +2,9 @@
import path from 'path'
import { pathToFileURL } from 'url'
import { arch, platform } from 'os'
import { promises as fs } from 'fs'
import { platformArchTriples } from 'next/dist/compiled/@napi-rs/triples'
import * as Log from '../output/log'
import { getParserOptions } from './options'
import { eventSwcLoadFailure } from '../../telemetry/events/swc-load-failure'
import { patchIncorrectLockfile } from '../../lib/patch-incorrect-lockfile'
import { downloadNativeNextSwc, downloadWasmSwc } from '../../lib/download-swc'
......@@ -15,7 +15,6 @@ import { getDefineEnv } from '../webpack/plugins/define-env-plugin'
import type { PageExtensions } from '../page-extensions-type'
const nextVersion = process.env.__NEXT_VERSION as string
const isYarnPnP = !!process?.versions?.pnp
const ArchName = arch()
const PlatformName = platform()
......@@ -109,19 +108,6 @@ function checkVersionMismatch(pkgData: any) {
}
}
async function tryToReadFile(filePath: string, shouldThrow: boolean) {
try {
return await fs.readFile(filePath, {
encoding: 'utf8',
})
} catch (error: any) {
if (shouldThrow) {
error.message = `Next.js ERROR: Failed to read file ${filePath}:\n${error.message}`
throw error
}
}
}
// These are the platforms we'll try to load wasm bindings first,
// only try to load native bindings if loading wasm binding somehow fails.
// Fallback to native binding is for migration period only,
......@@ -174,13 +160,12 @@ export interface Binding {
turboEngineOptions?: TurboEngineOptions
) => Promise<Project>
}
analysis: {
isDynamicMetadataRoute(pageFilePath: string): Promise<boolean>
}
minify: any
minifySync: any
transform: any
transformSync: any
parse: any
parseSync: any
getTargetTriple(): string | undefined
......@@ -1194,26 +1179,6 @@ function bindingToApi(binding: any, _wasm: boolean) {
return createProject
}
const warnedInvalidValueMap = {
runtime: new Map<string, boolean>(),
preferredRegion: new Map<string, boolean>(),
} as const
function warnInvalidValue(
pageFilePath: string,
key: keyof typeof warnedInvalidValueMap,
message: string
): void {
if (warnedInvalidValueMap[key].has(pageFilePath)) return
Log.warn(
`Next.js can't recognize the exported \`${key}\` field in "${pageFilePath}" as ${message}.` +
'\n' +
'The default runtime will be used instead.'
)
warnedInvalidValueMap[key].set(pageFilePath, true)
}
async function loadWasm(importPath = '') {
if (wasmBindings) {
return wasmBindings
......@@ -1255,33 +1220,13 @@ async function loadWasm(importPath = '') {
minifySync(src: string, options: any) {
return bindings.minifySync(src.toString(), options)
},
analysis: {
isDynamicMetadataRoute: async (pageFilePath: string) => {
const fileContent = (await tryToReadFile(pageFilePath, true)) || ''
const { isDynamicMetadataRoute, warnings } =
await bindings.isDynamicMetadataRoute(pageFilePath, fileContent)
warnings?.forEach(
({
key,
message,
}: {
key: keyof typeof warnedInvalidValueMap
message: string
}) => warnInvalidValue(pageFilePath, key, message)
)
return isDynamicMetadataRoute
},
getPageStaticInfo: async (params: Record<string, any>) => {
const fileContent =
(await tryToReadFile(params.pageFilePath, !params.isDev)) || ''
const raw = await bindings.getPageStaticInfo(
params.pageFilePath,
fileContent
)
return coercePageStaticInfo(params.pageFilePath, raw)
},
parse(src: string, options: any) {
return bindings?.parse
? bindings.parse(src.toString(), options)
: Promise.resolve(bindings.parseSync(src.toString(), options))
},
parseSync(src: string, options: any) {
return bindings.parseSync(src.toString(), options)
},
getTargetTriple() {
return undefined
......@@ -1446,40 +1391,8 @@ function loadNative(importPath?: string) {
return bindings.minifySync(toBuffer(src), toBuffer(options ?? {}))
},
analysis: {
isDynamicMetadataRoute: async (pageFilePath: string) => {
let fileContent: string | undefined = undefined
if (isYarnPnP) {
fileContent = (await tryToReadFile(pageFilePath, true)) || ''
}
const { isDynamicMetadataRoute, warnings } =
await bindings.isDynamicMetadataRoute(pageFilePath, fileContent)
// Instead of passing js callback into napi's context, bindings bubble up the warning messages
// and let next.js logger handles it.
warnings?.forEach(
({
key,
message,
}: {
key: keyof typeof warnedInvalidValueMap
message: string
}) => warnInvalidValue(pageFilePath, key, message)
)
return isDynamicMetadataRoute
},
getPageStaticInfo: async (params: Record<string, any>) => {
let fileContent: string | undefined = undefined
if (isYarnPnP) {
fileContent =
(await tryToReadFile(params.pageFilePath, !params.isDev)) || ''
}
const raw = await bindings.getPageStaticInfo(params, fileContent)
return coercePageStaticInfo(params.pageFilePath, raw)
},
parse(src: string, options: any) {
return bindings.parse(src, toBuffer(options ?? {}))
},
getTargetTriple: bindings.getTargetTriple,
......@@ -1568,31 +1481,6 @@ function toBuffer(t: any) {
return Buffer.from(JSON.stringify(t))
}
function coercePageStaticInfo(pageFilePath: string, raw?: string) {
if (!raw) return raw
const parsed = JSON.parse(raw)
parsed?.exportsInfo?.warnings?.forEach(
({
key,
message,
}: {
key: keyof typeof warnedInvalidValueMap
message: string
}) => warnInvalidValue(pageFilePath, key, message)
)
return {
...parsed,
exportsInfo: {
...parsed.exportsInfo,
directives: new Set(parsed?.exportsInfo?.directives ?? []),
extraProperties: new Set(parsed?.exportsInfo?.extraProperties ?? []),
},
}
}
export async function isWasm(): Promise<boolean> {
let bindings = await loadBindings()
return bindings.isWasm
......@@ -1618,6 +1506,14 @@ export function minifySync(src: string, options: any): string {
return bindings.minifySync(src, options)
}
export async function parse(src: string, options: any): Promise<any> {
let bindings = await loadBindings()
let parserOptions = getParserOptions(options)
return bindings
.parse(src, parserOptions)
.then((astStr: any) => JSON.parse(astStr))
}
export function getBinaryMetadata() {
let bindings
try {
......
......@@ -250,8 +250,6 @@ createNextDescribe(
await next.fetch('/')
await check(async () => {
expect(next.cliOutput).toContain(`Expected '{', got '}'`)
// [NOTE] [Flaky] expect at least 2 occurrences of the error message,
// on CI sometimes have more message appended somehow
expect(
next.cliOutput.split(`Expected '{', got '}'`).length
).toBeGreaterThanOrEqual(2)
......
......@@ -46,7 +46,9 @@ describe('Exported runtimes value validation', () => {
)
)
expect(result.stderr).toEqual(
expect.stringContaining('Unsupported node type at "config.runtime"')
expect.stringContaining(
'Unsupported node type "BinaryExpression" at "config.runtime"'
)
)
// Spread Operator within Object Expression
expect(result.stderr).toEqual(
......@@ -88,7 +90,9 @@ describe('Exported runtimes value validation', () => {
)
)
expect(result.stderr).toEqual(
expect.stringContaining('Unsupported node type at "config.runtime"')
expect.stringContaining(
'Unsupported node type "CallExpression" at "config.runtime"'
)
)
// Unknown Object Key
expect(result.stderr).toEqual(
......@@ -98,7 +102,7 @@ describe('Exported runtimes value validation', () => {
)
expect(result.stderr).toEqual(
expect.stringContaining(
'Unsupported key type in the Object Expression at "config.runtime"'
'Unsupported key type "Computed" in the Object Expression at "config.runtime"'
)
)
})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment