Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions crates/next-core/src/next_client/context.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::iter::once;
use std::{iter::once, str::FromStr};

use anyhow::Result;
use turbo_rcstr::{RcStr, rcstr};
Expand Down Expand Up @@ -80,11 +80,10 @@ fn defines(define_env: &FxIndexMap<RcStr, RcStr>) -> CompileTimeDefines {
.collect::<Vec<_>>(),
)
.or_insert_with(|| {
let val = serde_json::from_str(v);
let val = serde_json::Value::from_str(v);
match val {
Ok(serde_json::Value::Bool(v)) => CompileTimeDefineValue::Bool(v),
Ok(serde_json::Value::String(v)) => CompileTimeDefineValue::String(v.into()),
_ => CompileTimeDefineValue::JSON(v.clone()),
Ok(v) => v.into(),
_ => CompileTimeDefineValue::Evaluate(v.clone()),
}
});
}
Expand Down
7 changes: 3 additions & 4 deletions crates/next-core/src/next_edge/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,10 @@ fn defines(define_env: &FxIndexMap<RcStr, RcStr>) -> CompileTimeDefines {
.collect::<Vec<_>>(),
)
.or_insert_with(|| {
let val = serde_json::from_str(v);
let val = serde_json::from_str::<serde_json::Value>(v);
match val {
Ok(serde_json::Value::Bool(v)) => CompileTimeDefineValue::Bool(v),
Ok(serde_json::Value::String(v)) => CompileTimeDefineValue::String(v.into()),
_ => CompileTimeDefineValue::JSON(v.clone()),
Ok(v) => v.into(),
_ => CompileTimeDefineValue::Evaluate(v.clone()),
}
});
}
Expand Down
9 changes: 4 additions & 5 deletions crates/next-core/src/next_server/context.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::iter::once;
use std::{iter::once, str::FromStr};

use anyhow::{Result, bail};
use turbo_rcstr::{RcStr, rcstr};
Expand Down Expand Up @@ -357,11 +357,10 @@ fn defines(define_env: &FxIndexMap<RcStr, RcStr>) -> CompileTimeDefines {
.collect::<Vec<_>>(),
)
.or_insert_with(|| {
let val = serde_json::from_str(v);
let val = serde_json::Value::from_str(v);
match val {
Ok(serde_json::Value::Bool(v)) => CompileTimeDefineValue::Bool(v),
Ok(serde_json::Value::String(v)) => CompileTimeDefineValue::String(v.into()),
_ => CompileTimeDefineValue::JSON(v.clone()),
Ok(v) => v.into(),
_ => CompileTimeDefineValue::Evaluate(v.clone()),
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/build/swc/generated-native.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function lightningCssTransformStyleAttribute(

/* auto-generated by NAPI-RS */

export declare class ExternalObject<T> {
export class ExternalObject<T> {
readonly '': {
readonly '': unique symbol
[K: symbol]: T
Expand Down
9 changes: 7 additions & 2 deletions turbopack/crates/turbo-tasks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,20 @@ futures = { workspace = true }
indexmap = { workspace = true, features = ["serde"] }
mopa = "0.2.0"
once_cell = { workspace = true }
parking_lot = { workspace = true, features = ["serde"]}
parking_lot = { workspace = true, features = ["serde"] }
pin-project-lite = { workspace = true }
rayon = { workspace = true }
regex = { workspace = true }
rustc-hash = { workspace = true }
serde = { workspace = true, features = ["rc", "derive"] }
serde_json = { workspace = true }
serde_regex = "1.1.0"
shrink-to-fit = { workspace=true,features = ["indexmap", "serde_json", "smallvec", "nightly"] }
shrink-to-fit = { workspace = true, features = [
"indexmap",
"serde_json",
"smallvec",
"nightly",
] }
smallvec = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true, features = ["full"] }
Expand Down
2 changes: 1 addition & 1 deletion turbopack/crates/turbo-tasks/src/marker_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ macro_rules! impl_marker_trait_fn_ptr {

/// Create an implementation for every possible tuple where every element implements `$trait`.
///
/// Must be passed a sequence of identifier fo the tuple's generic parameters. This will only
/// Must be passed a sequence of identifier to the tuple's generic parameters. This will only
/// generate implementations up to the length of the passed in sequence.
///
/// Based on stdlib's internal `tuple_impls!` macro.
Expand Down
2 changes: 1 addition & 1 deletion turbopack/crates/turbo-tasks/src/task/task_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ where
async fn resolve_input(&self) -> Result<Self> {
let mut resolved = Vec::with_capacity(self.len());
for value in self {
resolved.push(value.resolve_input().await?);
resolved.push(Box::pin(value.resolve_input()).await?);
}
Ok(resolved)
}
Expand Down
17 changes: 15 additions & 2 deletions turbopack/crates/turbopack-core/src/compile_time_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,14 @@ macro_rules! free_var_references {
#[turbo_tasks::value]
#[derive(Debug, Clone, Hash, TaskInput)]
pub enum CompileTimeDefineValue {
Null,
Bool(bool),
Number(RcStr),
String(RcStr),
JSON(RcStr),
Array(Vec<CompileTimeDefineValue>),
Object(Vec<(RcStr, CompileTimeDefineValue)>),
Undefined,
Evaluate(RcStr),
}

impl From<bool> for CompileTimeDefineValue {
Expand Down Expand Up @@ -136,7 +140,16 @@ impl From<&str> for CompileTimeDefineValue {

impl From<serde_json::Value> for CompileTimeDefineValue {
fn from(value: serde_json::Value) -> Self {
Self::JSON(value.to_string().into())
match value {
serde_json::Value::Null => Self::Null,
serde_json::Value::Bool(b) => Self::Bool(b),
serde_json::Value::Number(n) => Self::Number(n.to_string().into()),
serde_json::Value::String(s) => Self::String(s.into()),
serde_json::Value::Array(a) => Self::Array(a.into_iter().map(|i| i.into()).collect()),
serde_json::Value::Object(m) => {
Self::Object(m.into_iter().map(|(k, v)| (k.into(), v.into())).collect())
}
}
}
}

Expand Down
68 changes: 53 additions & 15 deletions turbopack/crates/turbopack-ecmascript/src/analyzer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ pub enum ConstantValue {
Null,
BigInt(Box<BigInt>),
Regex(Box<(Atom, Atom)>),
Evaluate(RcStr),
}

impl ConstantValue {
Expand All @@ -203,25 +204,27 @@ impl ConstantValue {
}
}

pub fn is_truthy(&self) -> bool {
pub fn is_truthy(&self) -> Option<bool> {
match self {
Self::Undefined | Self::False | Self::Null => false,
Self::True | Self::Regex(..) => true,
Self::Str(s) => !s.is_empty(),
Self::Num(ConstantNumber(n)) => *n != 0.0,
Self::BigInt(n) => !n.is_zero(),
Self::Undefined | Self::False | Self::Null => Some(false),
Self::True | Self::Regex(..) => Some(true),
Self::Str(s) => Some(!s.is_empty()),
Self::Num(ConstantNumber(n)) => Some(*n != 0.0),
Self::BigInt(n) => Some(!n.is_zero()),
Self::Evaluate(_) => None,
}
}

pub fn is_nullish(&self) -> bool {
pub fn is_nullish(&self) -> Option<bool> {
match self {
Self::Undefined | Self::Null => true,
Self::Undefined | Self::Null => Some(true),
Self::Str(..)
| Self::Num(..)
| Self::True
| Self::False
| Self::BigInt(..)
| Self::Regex(..) => false,
| Self::Regex(..) => Some(false),
Self::Evaluate(_) => None,
}
}

Expand All @@ -233,7 +236,16 @@ impl ConstantValue {
}

pub fn is_value_type(&self) -> bool {
!matches!(self, Self::Regex(..))
match self {
ConstantValue::Undefined
| ConstantValue::Null
| ConstantValue::Str(_)
| ConstantValue::Num(_)
| ConstantValue::True
| ConstantValue::False
| ConstantValue::BigInt(_) => true,
ConstantValue::Regex(_) | ConstantValue::Evaluate(_) => false,
}
}
}

Expand Down Expand Up @@ -283,6 +295,7 @@ impl Display for ConstantValue {
ConstantValue::Num(ConstantNumber(n)) => write!(f, "{n}"),
ConstantValue::BigInt(n) => write!(f, "{n}"),
ConstantValue::Regex(regex) => write!(f, "/{}/{}", regex.0, regex.1),
ConstantValue::Evaluate(eval) => write!(f, "{eval}"),
}
}
}
Expand Down Expand Up @@ -584,12 +597,37 @@ impl From<ConstantValue> for JsValue {
impl From<&CompileTimeDefineValue> for JsValue {
fn from(v: &CompileTimeDefineValue) -> Self {
match v {
CompileTimeDefineValue::String(s) => JsValue::Constant(s.as_str().into()),
CompileTimeDefineValue::Null => JsValue::Constant(ConstantValue::Null),
CompileTimeDefineValue::Bool(b) => JsValue::Constant((*b).into()),
CompileTimeDefineValue::JSON(_) => {
JsValue::unknown_empty(false, "compile time injected JSON")
CompileTimeDefineValue::Number(n) => JsValue::Constant(ConstantValue::Num(
ConstantNumber(n.as_str().parse::<f64>().unwrap()),
)),
CompileTimeDefineValue::String(s) => JsValue::Constant(s.as_str().into()),
CompileTimeDefineValue::Array(a) => {
let mut js_value = JsValue::Array {
total_nodes: a.len() as u32,
items: a.iter().map(|i| i.into()).collect(),
mutable: false,
};
js_value.update_total_nodes();
js_value
}
CompileTimeDefineValue::Object(m) => {
let mut js_value = JsValue::Object {
total_nodes: m.len() as u32,
parts: m
.iter()
.map(|(k, v)| ObjectPart::KeyValue(k.clone().into(), v.into()))
.collect(),
mutable: false,
};
js_value.update_total_nodes();
js_value
}
CompileTimeDefineValue::Undefined => JsValue::Constant(ConstantValue::Undefined),
CompileTimeDefineValue::Evaluate(s) => {
JsValue::Constant(ConstantValue::Evaluate(s.clone()))
}
}
}
}
Expand Down Expand Up @@ -2192,7 +2230,7 @@ impl JsValue {
/// Some if we know if or if not the value is truthy.
pub fn is_truthy(&self) -> Option<bool> {
match self {
JsValue::Constant(c) => Some(c.is_truthy()),
JsValue::Constant(c) => c.is_truthy(),
JsValue::Concat(..) => self.is_empty_string().map(|x| !x),
JsValue::Url(..)
| JsValue::Array { .. }
Expand Down Expand Up @@ -2273,7 +2311,7 @@ impl JsValue {
/// don't know. Returns Some if we know if or if not the value is nullish.
pub fn is_nullish(&self) -> Option<bool> {
match self {
JsValue::Constant(c) => Some(c.is_nullish()),
JsValue::Constant(c) => c.is_nullish(),
JsValue::Concat(..)
| JsValue::Url(..)
| JsValue::Array { .. }
Expand Down
101 changes: 82 additions & 19 deletions turbopack/crates/turbopack-ecmascript/src/references/constant_value.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
use std::{path::PathBuf, str::FromStr};

use anyhow::Result;
use serde::{Deserialize, Serialize};
use swc_core::quote;
use swc_core::{
common::{DUMMY_SP, SourceMap, sync::Lrc},
ecma::{
ast::{ArrayLit, EsVersion, Expr, KeyValueProp, ObjectLit, Prop, PropName, Str},
parser::{Syntax, parse_file_as_expr},
},
quote,
};
use turbo_tasks::{NonLocalValue, TaskInput, Vc, debug::ValueDebugFormat, trace::TraceRawVcs};
use turbopack_core::{
chunk::ChunkingContext, compile_time_info::CompileTimeDefineValue, module_graph::ModuleGraph,
Expand Down Expand Up @@ -42,24 +51,8 @@ impl ConstantValueCodeGen {
let value = self.value.clone();

let visitor = create_visitor!(self.path, visit_mut_expr, |expr: &mut Expr| {
*expr = match value {
CompileTimeDefineValue::Bool(true) => {
quote!("(\"TURBOPACK compile-time value\", true)" as Expr)
}
CompileTimeDefineValue::Bool(false) => {
quote!("(\"TURBOPACK compile-time value\", false)" as Expr)
}
CompileTimeDefineValue::String(ref s) => {
quote!("(\"TURBOPACK compile-time value\", $e)" as Expr, e: Expr = s.to_string().into())
}
CompileTimeDefineValue::JSON(ref s) => {
quote!("(\"TURBOPACK compile-time value\", JSON.parse($e))" as Expr, e: Expr = s.to_string().into())
}
// undefined can be re-bound, so use `void 0` to avoid any risks
CompileTimeDefineValue::Undefined => {
quote!("(\"TURBOPACK compile-time value\", void 0)" as Expr)
}
};
// TODO: avoid this clone
*expr = define_env_to_expr((value).clone());
});

Ok(CodeGeneration::visitors(vec![visitor]))
Expand All @@ -71,3 +64,73 @@ impl From<ConstantValueCodeGen> for CodeGen {
CodeGen::ConstantValueCodeGen(val)
}
}

fn define_env_to_expr(value: CompileTimeDefineValue) -> Expr {
match value {
CompileTimeDefineValue::Null => {
quote!("(\"TURBOPACK compile-time value\", null)" as Expr)
}
CompileTimeDefineValue::Bool(true) => {
quote!("(\"TURBOPACK compile-time value\", true)" as Expr)
}
CompileTimeDefineValue::Bool(false) => {
quote!("(\"TURBOPACK compile-time value\", false)" as Expr)
}
CompileTimeDefineValue::Number(ref n) => {
quote!("(\"TURBOPACK compile-time value\", $e)" as Expr, e: Expr = n.parse::<f64>().unwrap().into())
}
CompileTimeDefineValue::String(ref s) => {
quote!("(\"TURBOPACK compile-time value\", $e)" as Expr, e: Expr = s.to_string().into())
}
CompileTimeDefineValue::Array(a) => {
quote!("(\"TURBOPACK compile-time value\", $e)" as Expr, e: Expr = Expr::Array(ArrayLit {
span: DUMMY_SP,
elems: a.into_iter().map(|i| Some(define_env_to_expr(i).into())).collect(),
}))
}
CompileTimeDefineValue::Object(m) => {
quote!("(\"TURBOPACK compile-time value\", $e)" as Expr, e: Expr = Expr::Object(ObjectLit {
span: DUMMY_SP,
props: m
.into_iter()
.map(|(k, v)| {
swc_core::ecma::ast::PropOrSpread::Prop(
Prop::KeyValue(KeyValueProp {
key: PropName::Str(Str::from(k.as_str())),
value: define_env_to_expr(v).into(),
})
.into(),
)
})
.collect(),
}))
}
CompileTimeDefineValue::Undefined => {
quote!("(\"TURBOPACK compile-time value\", void 0)" as Expr)
}
CompileTimeDefineValue::Evaluate(ref s) => parse_code_to_expr(s.to_string()),
}
}

fn parse_code_to_expr(code: String) -> Expr {
let cm = Lrc::new(SourceMap::default());
let fm = cm.new_source_file(
Lrc::new(
PathBuf::from_str("__compile_time_define_value_internal__.js")
.unwrap()
.into(),
),
code.clone(),
);
parse_file_as_expr(
&fm,
Syntax::Es(Default::default()),
EsVersion::latest(),
None,
&mut vec![],
)
.map_or(
quote!("$s" as Expr, s: Expr = code.into()),
|expr| quote!("(\"TURBOPACK compile-time value\", $e)" as Expr, e: Expr = *expr),
)
}
1 change: 1 addition & 0 deletions turbopack/crates/turbopack-ecmascript/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub fn js_value_to_pattern(value: &JsValue) -> Pattern {
ConstantValue::BigInt(n) => n.to_string().into(),
ConstantValue::Regex(box (exp, flags)) => format!("/{exp}/{flags}").into(),
ConstantValue::Undefined => rcstr!("undefined"),
ConstantValue::Evaluate(eval) => eval.clone(),
}),
JsValue::Url(v, JsValueUrlKind::Relative) => Pattern::Constant(v.as_rcstr()),
JsValue::Alternatives {
Expand Down
Loading
Loading