这是indexloc提供的服务,不要输入任何密码
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
6 changes: 5 additions & 1 deletion crates/turbopack-create-test-app/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()> {
Expand All @@ -48,7 +52,7 @@ fn main() -> Result<()> {
} else {
None
},
effect_mode: EffectMode::Hook
effect_mode: args.effect_mode,
}
.build()?
.path()
Expand Down
24 changes: 21 additions & 3 deletions crates/turbopack-create-test-app/src/test_app_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -40,8 +41,11 @@ fn write_file<P: AsRef<Path>>(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 <Effect /> client-side component that has the `useEffect`
Expand All @@ -50,6 +54,19 @@ pub enum EffectMode {
Component,
}

impl FromStr for EffectMode {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
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<PathBuf>,
Expand Down Expand Up @@ -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, ""),
};
Expand All @@ -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()
Expand Down