diff --git a/crates/turbopack-create-test-app/src/main.rs b/crates/turbopack-create-test-app/src/main.rs index 13891ada04cb1..f5539c6c4ce40 100644 --- a/crates/turbopack-create-test-app/src/main.rs +++ b/crates/turbopack-create-test-app/src/main.rs @@ -30,6 +30,10 @@ struct Args { /// Generate a package.json with required dependencies #[clap(long)] package_json: bool, + + /// How to communicate changes to the consumer (none | hook | component) + #[clap(long)] + effect_mode: EffectMode, } fn main() -> Result<()> { @@ -48,7 +52,7 @@ fn main() -> Result<()> { } else { None }, - effect_mode: EffectMode::Hook + effect_mode: args.effect_mode, } .build()? .path() diff --git a/crates/turbopack-create-test-app/src/test_app_builder.rs b/crates/turbopack-create-test-app/src/test_app_builder.rs index be3678b0944c5..490f48a95b88c 100644 --- a/crates/turbopack-create-test-app/src/test_app_builder.rs +++ b/crates/turbopack-create-test-app/src/test_app_builder.rs @@ -3,9 +3,10 @@ use std::{ fs::{create_dir_all, File}, io::prelude::*, path::{Path, PathBuf}, + str::FromStr, }; -use anyhow::{Context, Result}; +use anyhow::{anyhow, Context, Result}; use indoc::{formatdoc, indoc}; use serde_json::json; use tempfile::TempDir; @@ -40,8 +41,11 @@ fn write_file>(name: &str, path: P, content: &[u8]) -> Result<()> } /// How to run effects in components. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] pub enum EffectMode { + /// No effects at all. + #[default] + None, /// As a direct `useEffect` hook in the component's body. Hook, /// Rendering an client-side component that has the `useEffect` @@ -50,6 +54,19 @@ pub enum EffectMode { Component, } +impl FromStr for EffectMode { + type Err = anyhow::Error; + + fn from_str(s: &str) -> Result { + match s { + "none" => Ok(EffectMode::None), + "hook" => Ok(EffectMode::Hook), + "component" => Ok(EffectMode::Component), + _ => Err(anyhow!("unknown effect mode: {}", s)), + } + } +} + #[derive(Debug)] pub struct TestAppBuilder { pub target: Option, @@ -121,6 +138,7 @@ impl TestAppBuilder { let mut is_root = true; let (additional_body, additional_elements) = match self.effect_mode { + EffectMode::None => ("", ""), EffectMode::Component => ("", EFFECT_ELEMENT), EffectMode::Hook => (USE_EFFECT, ""), }; @@ -129,7 +147,7 @@ impl TestAppBuilder { modules.push((file.clone(), depth)); let setup_imports = match self.effect_mode { - EffectMode::Hook => SETUP_IMPORTS.to_string(), + EffectMode::Hook | EffectMode::None => SETUP_IMPORTS.to_string(), EffectMode::Component => { let relative_effect = if src == file.parent().unwrap() { "./effect.jsx".to_string()