diff --git a/crates/next-core/src/next_client/context.rs b/crates/next-core/src/next_client/context.rs index a62528ba68677..00b118ca1809b 100644 --- a/crates/next-core/src/next_client/context.rs +++ b/crates/next-core/src/next_client/context.rs @@ -1,4 +1,4 @@ -use std::iter::once; +use std::{iter::once, str::FromStr}; use anyhow::Result; use turbo_rcstr::{RcStr, rcstr}; @@ -80,11 +80,10 @@ fn defines(define_env: &FxIndexMap) -> CompileTimeDefines { .collect::>(), ) .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()), } }); } diff --git a/crates/next-core/src/next_edge/context.rs b/crates/next-core/src/next_edge/context.rs index ad2861f773fc5..611b5e7caf96b 100644 --- a/crates/next-core/src/next_edge/context.rs +++ b/crates/next-core/src/next_edge/context.rs @@ -44,11 +44,10 @@ fn defines(define_env: &FxIndexMap) -> CompileTimeDefines { .collect::>(), ) .or_insert_with(|| { - let val = serde_json::from_str(v); + let val = serde_json::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()), } }); } diff --git a/crates/next-core/src/next_server/context.rs b/crates/next-core/src/next_server/context.rs index 1e298bb9f658d..284750f4c927f 100644 --- a/crates/next-core/src/next_server/context.rs +++ b/crates/next-core/src/next_server/context.rs @@ -1,4 +1,4 @@ -use std::iter::once; +use std::{iter::once, str::FromStr}; use anyhow::{Result, bail}; use turbo_rcstr::{RcStr, rcstr}; @@ -357,11 +357,10 @@ fn defines(define_env: &FxIndexMap) -> CompileTimeDefines { .collect::>(), ) .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()), } }); } diff --git a/packages/next/src/build/swc/generated-native.d.ts b/packages/next/src/build/swc/generated-native.d.ts index e944b8dcc85ce..05e13462842d4 100644 --- a/packages/next/src/build/swc/generated-native.d.ts +++ b/packages/next/src/build/swc/generated-native.d.ts @@ -28,7 +28,7 @@ export function lightningCssTransformStyleAttribute( /* auto-generated by NAPI-RS */ -export declare class ExternalObject { +export class ExternalObject { readonly '': { readonly '': unique symbol [K: symbol]: T diff --git a/turbopack/crates/turbo-tasks/Cargo.toml b/turbopack/crates/turbo-tasks/Cargo.toml index fa0eadb829150..6093007563b18 100644 --- a/turbopack/crates/turbo-tasks/Cargo.toml +++ b/turbopack/crates/turbo-tasks/Cargo.toml @@ -33,7 +33,7 @@ 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 } @@ -41,7 +41,12 @@ 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"] } diff --git a/turbopack/crates/turbo-tasks/src/marker_trait.rs b/turbopack/crates/turbo-tasks/src/marker_trait.rs index b993eef5d11b8..dbcad957123f9 100644 --- a/turbopack/crates/turbo-tasks/src/marker_trait.rs +++ b/turbopack/crates/turbo-tasks/src/marker_trait.rs @@ -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. diff --git a/turbopack/crates/turbo-tasks/src/task/task_input.rs b/turbopack/crates/turbo-tasks/src/task/task_input.rs index 11eae0c5f7064..525b01c7f2701 100644 --- a/turbopack/crates/turbo-tasks/src/task/task_input.rs +++ b/turbopack/crates/turbo-tasks/src/task/task_input.rs @@ -67,7 +67,7 @@ where async fn resolve_input(&self) -> Result { 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) } diff --git a/turbopack/crates/turbopack-core/src/compile_time_info.rs b/turbopack/crates/turbopack-core/src/compile_time_info.rs index 8c4a96e536cc6..0b54a644bbd0e 100644 --- a/turbopack/crates/turbopack-core/src/compile_time_info.rs +++ b/turbopack/crates/turbopack-core/src/compile_time_info.rs @@ -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), + Object(Vec<(RcStr, CompileTimeDefineValue)>), Undefined, + Evaluate(RcStr), } impl From for CompileTimeDefineValue { @@ -136,7 +140,16 @@ impl From<&str> for CompileTimeDefineValue { impl From 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()) + } + } } } diff --git a/turbopack/crates/turbopack-ecmascript/src/analyzer/mod.rs b/turbopack/crates/turbopack-ecmascript/src/analyzer/mod.rs index 48d5a5bc33a6c..95e63d11dc0e2 100644 --- a/turbopack/crates/turbopack-ecmascript/src/analyzer/mod.rs +++ b/turbopack/crates/turbopack-ecmascript/src/analyzer/mod.rs @@ -185,6 +185,7 @@ pub enum ConstantValue { Null, BigInt(Box), Regex(Box<(Atom, Atom)>), + Evaluate(RcStr), } impl ConstantValue { @@ -203,25 +204,27 @@ impl ConstantValue { } } - pub fn is_truthy(&self) -> bool { + pub fn is_truthy(&self) -> Option { 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 { 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, } } @@ -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, + } } } @@ -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}"), } } } @@ -584,12 +597,37 @@ impl From 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::().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())) + } } } } @@ -2192,7 +2230,7 @@ impl JsValue { /// Some if we know if or if not the value is truthy. pub fn is_truthy(&self) -> Option { 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 { .. } @@ -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 { match self { - JsValue::Constant(c) => Some(c.is_nullish()), + JsValue::Constant(c) => c.is_nullish(), JsValue::Concat(..) | JsValue::Url(..) | JsValue::Array { .. } diff --git a/turbopack/crates/turbopack-ecmascript/src/references/constant_value.rs b/turbopack/crates/turbopack-ecmascript/src/references/constant_value.rs index 55b4e0fad41f6..a1bc75528ccb3 100644 --- a/turbopack/crates/turbopack-ecmascript/src/references/constant_value.rs +++ b/turbopack/crates/turbopack-ecmascript/src/references/constant_value.rs @@ -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, @@ -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])) @@ -71,3 +64,73 @@ impl From 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::().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), + ) +} diff --git a/turbopack/crates/turbopack-ecmascript/src/utils.rs b/turbopack/crates/turbopack-ecmascript/src/utils.rs index b4562da77ecef..0d264bb70f028 100644 --- a/turbopack/crates/turbopack-ecmascript/src/utils.rs +++ b/turbopack/crates/turbopack-ecmascript/src/utils.rs @@ -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 { diff --git a/turbopack/crates/turbopack-tests/tests/snapshot.rs b/turbopack/crates/turbopack-tests/tests/snapshot.rs index 1931515ceb119..c813939e52d05 100644 --- a/turbopack/crates/turbopack-tests/tests/snapshot.rs +++ b/turbopack/crates/turbopack-tests/tests/snapshot.rs @@ -36,7 +36,7 @@ use turbopack_core::{ EvaluatableAssets, MinifyType, availability_info::AvailabilityInfo, }, compile_time_defines, - compile_time_info::CompileTimeInfo, + compile_time_info::{CompileTimeDefineValue, CompileTimeInfo, DefineableNameSegment}, condition::ContextCondition, context::AssetContext, environment::{BrowserEnvironment, Environment, ExecutionEnvironment, NodeJsEnvironment}, @@ -258,15 +258,33 @@ async fn run_test_operation(resource: RcStr) -> Result> { .to_resolved() .await?; - let defines = compile_time_defines!( + let mut defines = compile_time_defines!( process.turbopack = true, process.env.TURBOPACK = true, process.env.NODE_ENV = "development", DEFINED_VALUE = "value", DEFINED_TRUE = true, + DEFINED_NULL = json!(null), + DEFINED_INT = json!(1), + DEFINED_FLOAT = json!(0.01), + DEFINED_ARRAY = json!([ false, 0, "1", { "v": "v" }, null ]), A.VERY.LONG.DEFINED.VALUE = json!({ "test": true }), ); + defines.0.insert( + vec![DefineableNameSegment::from("DEFINED_EVALED")], + CompileTimeDefineValue::Evaluate("1 + 1".into()), + ); + + defines.0.insert( + vec![DefineableNameSegment::from("DEFINED_EVALED_NESTED")], + CompileTimeDefineValue::Array(vec![ + CompileTimeDefineValue::Bool(true), + CompileTimeDefineValue::Undefined, + CompileTimeDefineValue::Evaluate("() => 1".into()), + ]), + ); + let compile_time_info = CompileTimeInfo::builder(env) .defines(defines.clone().resolved_cell()) .free_var_references(free_var_references!(..defines.into_iter()).resolved_cell()) diff --git a/turbopack/crates/turbopack-tests/tests/snapshot/comptime/define/input/index.js b/turbopack/crates/turbopack-tests/tests/snapshot/comptime/define/input/index.js index 4429017146fb9..7c6d2f64358f0 100644 --- a/turbopack/crates/turbopack-tests/tests/snapshot/comptime/define/input/index.js +++ b/turbopack/crates/turbopack-tests/tests/snapshot/comptime/define/input/index.js @@ -6,6 +6,30 @@ if (DEFINED_TRUE) { console.log('DEFINED_VALUE') } +if (!DEFINED_NULL) { + console.log('DEFINED_NULL', DEFINED_NULL) +} + +if (DEFINED_INT) { + console.log('DEFINED_INT', DEFINED_INT) +} + +if (DEFINED_FLOAT) { + console.log('DEFINED_FLOAT', DEFINED_FLOAT) +} + +if (DEFINED_ARRAY) { + console.log('DEFINED_ARRAY', DEFINED_ARRAY) +} + +if (DEFINED_EVALED) { + console.log('DEFINED_EVALED', DEFINED_EVALED) +} + +if (DEFINED_EVALED_NESTED) { + console.log('DEFINED_EVALED_NESTED', DEFINED_EVALED_NESTED) +} + if (A.VERY.LONG.DEFINED.VALUE) { console.log('A.VERY.LONG.DEFINED.VALUE') } diff --git a/turbopack/crates/turbopack-tests/tests/snapshot/comptime/define/output/4e721_crates_turbopack-tests_tests_snapshot_comptime_define_input_index_4d74c0a3.js b/turbopack/crates/turbopack-tests/tests/snapshot/comptime/define/output/4e721_crates_turbopack-tests_tests_snapshot_comptime_define_input_index_4d74c0a3.js index 5aa314e52b8ad..fde3f302b73db 100644 --- a/turbopack/crates/turbopack-tests/tests/snapshot/comptime/define/output/4e721_crates_turbopack-tests_tests_snapshot_comptime_define_input_index_4d74c0a3.js +++ b/turbopack/crates/turbopack-tests/tests/snapshot/comptime/define/output/4e721_crates_turbopack-tests_tests_snapshot_comptime_define_input_index_4d74c0a3.js @@ -10,20 +10,54 @@ if ("TURBOPACK compile-time truthy", 1) { if ("TURBOPACK compile-time truthy", 1) { console.log('DEFINED_VALUE'); } -if ("TURBOPACK compile-time value", JSON.parse('{"test":true}')) { +if ("TURBOPACK compile-time truthy", 1) { + console.log('DEFINED_NULL', ("TURBOPACK compile-time value", null)); +} +if ("TURBOPACK compile-time truthy", 1) { + console.log('DEFINED_INT', ("TURBOPACK compile-time value", 1)); +} +if ("TURBOPACK compile-time truthy", 1) { + console.log('DEFINED_FLOAT', ("TURBOPACK compile-time value", 0.01)); +} +if ("TURBOPACK compile-time truthy", 1) { + console.log('DEFINED_ARRAY', ("TURBOPACK compile-time value", [ + ("TURBOPACK compile-time value", false), + ("TURBOPACK compile-time value", 0), + ("TURBOPACK compile-time value", "1"), + ("TURBOPACK compile-time value", { + "v": ("TURBOPACK compile-time value", "v") + }), + ("TURBOPACK compile-time value", null) + ])); +} +if ("TURBOPACK compile-time value", 1 + 1) { + console.log('DEFINED_EVALED', ("TURBOPACK compile-time value", 1 + 1)); +} +if ("TURBOPACK compile-time truthy", 1) { + console.log('DEFINED_EVALED_NESTED', ("TURBOPACK compile-time value", [ + ("TURBOPACK compile-time value", true), + ("TURBOPACK compile-time value", void 0), + ("TURBOPACK compile-time value", ()=>1) + ])); +} +if ("TURBOPACK compile-time truthy", 1) { console.log('A.VERY.LONG.DEFINED.VALUE'); } if ("TURBOPACK compile-time truthy", 1) { console.log('something'); } -if ("TURBOPACK compile-time falsy", 0) //TURBOPACK unreachable -; +if (("TURBOPACK compile-time value", "development") === 'production') { + console.log('production'); +} var p = process; -console.log(("TURBOPACK compile-time value", JSON.parse('{"test":true}'))); +console.log(("TURBOPACK compile-time value", { + "test": ("TURBOPACK compile-time value", true) +})); console.log(("TURBOPACK compile-time value", "value")); console.log(("TURBOPACK compile-time value", "development")); -if ("TURBOPACK compile-time falsy", 0) //TURBOPACK unreachable -; +if (("TURBOPACK compile-time value", "development") === 'production') { + console.log('production'); +} ("TURBOPACK compile-time falsy", 0) ? "TURBOPACK unreachable" : console.log('development'); // TODO short-circuit is not implemented yet ("TURBOPACK compile-time value", "development") != 'production' && console.log('development'); diff --git a/turbopack/crates/turbopack-tests/tests/snapshot/comptime/define/output/4e721_crates_turbopack-tests_tests_snapshot_comptime_define_input_index_4d74c0a3.js.map b/turbopack/crates/turbopack-tests/tests/snapshot/comptime/define/output/4e721_crates_turbopack-tests_tests_snapshot_comptime_define_input_index_4d74c0a3.js.map index 99ad30ad9d349..2732f6ba53656 100644 --- a/turbopack/crates/turbopack-tests/tests/snapshot/comptime/define/output/4e721_crates_turbopack-tests_tests_snapshot_comptime_define_input_index_4d74c0a3.js.map +++ b/turbopack/crates/turbopack-tests/tests/snapshot/comptime/define/output/4e721_crates_turbopack-tests_tests_snapshot_comptime_define_input_index_4d74c0a3.js.map @@ -2,5 +2,5 @@ "version": 3, "sources": [], "sections": [ - {"offset": {"line": 6, "column": 0}, "map": {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/comptime/define/input/index.js"],"sourcesContent":["if (DEFINED_VALUE) {\n console.log('DEFINED_VALUE')\n}\n\nif (DEFINED_TRUE) {\n console.log('DEFINED_VALUE')\n}\n\nif (A.VERY.LONG.DEFINED.VALUE) {\n console.log('A.VERY.LONG.DEFINED.VALUE')\n}\n\nif (process.env.NODE_ENV) {\n console.log('something')\n}\n\nif (process.env.NODE_ENV === 'production') {\n console.log('production')\n}\n\nvar p = process\n\nconsole.log(A.VERY.LONG.DEFINED.VALUE)\nconsole.log(DEFINED_VALUE)\nconsole.log(p.env.NODE_ENV)\n\nif (p.env.NODE_ENV === 'production') {\n console.log('production')\n}\n\np.env.NODE_ENV == 'production'\n ? console.log('production')\n : console.log('development')\n\n// TODO short-circuit is not implemented yet\np.env.NODE_ENV != 'production' && console.log('development')\np.env.NODE_ENV == 'production' && console.log('production')\n\nconsole.log(__dirname)\n"],"names":[],"mappings":"AAAA,wCAAmB;IACjB,QAAQ,GAAG,CAAC;AACd;AAEA,wCAAkB;IAChB,QAAQ,GAAG,CAAC;AACd;AAEA,iEAA+B;IAC7B,QAAQ,GAAG,CAAC;AACd;AAEA,wCAA0B;IACxB,QAAQ,GAAG,CAAC;AACd;AAEA;;AAIA,IAAI,IAAI;AAER,QAAQ,GAAG;AACX,QAAQ,GAAG;AACX,QAAQ,GAAG;AAEX;;AAIA,sCACI,0BACA,QAAQ,GAAG,CAAC;AAEhB,4CAA4C;AAC5C,mDAAkB,gBAAgB,QAAQ,GAAG,CAAC;AAC9C,mDAAkB,gBAAgB,QAAQ,GAAG,CAAC;AAE9C,QAAQ,GAAG"}}] + {"offset": {"line": 6, "column": 0}, "map": {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/comptime/define/input/index.js"],"sourcesContent":["if (DEFINED_VALUE) {\n console.log('DEFINED_VALUE')\n}\n\nif (DEFINED_TRUE) {\n console.log('DEFINED_VALUE')\n}\n\nif (!DEFINED_NULL) {\n console.log('DEFINED_NULL', DEFINED_NULL)\n}\n\nif (DEFINED_INT) {\n console.log('DEFINED_INT', DEFINED_INT)\n}\n\nif (DEFINED_FLOAT) {\n console.log('DEFINED_FLOAT', DEFINED_FLOAT)\n}\n\nif (DEFINED_ARRAY) {\n console.log('DEFINED_ARRAY', DEFINED_ARRAY)\n}\n\nif (DEFINED_EVALED) {\n console.log('DEFINED_EVALED', DEFINED_EVALED)\n}\n\nif (DEFINED_EVALED_NESTED) {\n console.log('DEFINED_EVALED_NESTED', DEFINED_EVALED_NESTED)\n}\n\nif (A.VERY.LONG.DEFINED.VALUE) {\n console.log('A.VERY.LONG.DEFINED.VALUE')\n}\n\nif (process.env.NODE_ENV) {\n console.log('something')\n}\n\nif (process.env.NODE_ENV === 'production') {\n console.log('production')\n}\n\nvar p = process\n\nconsole.log(A.VERY.LONG.DEFINED.VALUE)\nconsole.log(DEFINED_VALUE)\nconsole.log(p.env.NODE_ENV)\n\nif (p.env.NODE_ENV === 'production') {\n console.log('production')\n}\n\np.env.NODE_ENV == 'production'\n ? console.log('production')\n : console.log('development')\n\n// TODO short-circuit is not implemented yet\np.env.NODE_ENV != 'production' && console.log('development')\np.env.NODE_ENV == 'production' && console.log('production')\n\nconsole.log(__dirname)\n"],"names":[],"mappings":"AAAA,wCAAmB;IACjB,QAAQ,GAAG,CAAC;AACd;AAEA,wCAAkB;IAChB,QAAQ,GAAG,CAAC;AACd;AAEA,wCAAmB;IACjB,QAAQ,GAAG,CAAC;AACd;AAEA,wCAAiB;IACf,QAAQ,GAAG,CAAC;AACd;AAEA,wCAAmB;IACjB,QAAQ,GAAG,CAAC;AACd;AAEA,wCAAmB;IACjB,QAAQ,GAAG,CAAC;;;;;;;;;AACd;AAEA,oCAxBA,IAAI,GAwBgB;IAClB,QAAQ,GAAG,CAAC,mDAzBd,IAAI;AA0BJ;AAEA,wCAA2B;IACzB,QAAQ,GAAG,CAAC;;;yCA7Bd,IAAM;;AA8BN;AAEA,wCAA+B;IAC7B,QAAQ,GAAG,CAAC;AACd;AAEA,wCAA0B;IACxB,QAAQ,GAAG,CAAC;AACd;AAEA,IAAI,oDAAyB,cAAc;IACzC,QAAQ,GAAG,CAAC;AACd;AAEA,IAAI,IAAI;AAER,QAAQ,GAAG;;;AACX,QAAQ,GAAG;AACX,QAAQ,GAAG;AAEX,IAAI,oDAAmB,cAAc;IACnC,QAAQ,GAAG,CAAC;AACd;AAEA,sCACI,0BACA,QAAQ,GAAG,CAAC;AAEhB,4CAA4C;AAC5C,mDAAkB,gBAAgB,QAAQ,GAAG,CAAC;AAC9C,mDAAkB,gBAAgB,QAAQ,GAAG,CAAC;AAE9C,QAAQ,GAAG"}}] } \ No newline at end of file