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
7 changes: 4 additions & 3 deletions crates/pack-core/src/client/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ pub async fn get_client_module_options_context(
} else {
false
};
let jsx_runtime_options = get_jsx_transform_options(
let jsx_transform_options = get_jsx_transform_options(
project_path.clone(),
mode,
false,
Expand Down Expand Up @@ -381,7 +381,6 @@ pub async fn get_client_module_options_context(
enable_typescript_transform: Some(
TypescriptTransformOptions::default().resolved_cell(),
),
enable_jsx: Some(JsxTransformOptions::default().resolved_cell()),
ignore_dynamic_requests: true,
..Default::default()
},
Expand All @@ -406,6 +405,7 @@ pub async fn get_client_module_options_context(
let foreign_codes_options_context = ModuleOptionsContext {
ecmascript: EcmascriptOptionsContext {
enable_typeof_window_inlining: None,
enable_jsx: Some(jsx_transform_options),
..module_options_context.ecmascript
},
enable_webpack_loaders: foreign_enable_webpack_loaders,
Expand All @@ -417,6 +417,7 @@ pub async fn get_client_module_options_context(

let internal_context = ModuleOptionsContext {
ecmascript: EcmascriptOptionsContext {
enable_jsx: Some(JsxTransformOptions::default().resolved_cell()),
..module_options_context.ecmascript.clone()
},
enable_postcss_transform: None,
Expand All @@ -428,7 +429,7 @@ pub async fn get_client_module_options_context(
// we try resolve it once at the root and pass down a context to all
// the modules.
ecmascript: EcmascriptOptionsContext {
enable_jsx: Some(jsx_runtime_options),
enable_jsx: Some(jsx_transform_options),
enable_typescript_transform: Some(tsconfig),
enable_decorators: Some(decorators_options.to_resolved().await?),
..module_options_context.ecmascript.clone()
Expand Down
14 changes: 14 additions & 0 deletions crates/pack-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ pub struct Config {
provider: Option<FxIndexMap<RcStr, ProviderConfigValue>>,
images: Option<ImageConfig>,
pub styles: Option<StyleConfig>,
react: Option<ReactConfig>,
optimization: Option<OptimizationConfig>,
stats: Option<bool>,
persistent_caching: Option<bool>,
Expand Down Expand Up @@ -240,6 +241,14 @@ pub struct ExternalAdvanced {
pub sub_path: Option<ExternalSubPath>,
}

#[turbo_tasks::value(eq = "manual")]
#[derive(Clone, Debug, PartialEq, Default, Deserialize, OperationValue)]
#[serde(rename_all = "camelCase")]
pub struct ReactConfig {
/// JSX runtime mode: "automatic" (default) or "classic"
pub runtime: Option<RcStr>,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enum !

}
Comment on lines +244 to +250

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For better type safety and to provide validation for the runtime option, it's recommended to use an enum instead of a raw RcStr. This will ensure that only valid values ("automatic" or "classic") are accepted, providing better error messages to the user for invalid configurations at deserialization time.

You can define a JsxRuntime enum and use it in ReactConfig.

#[turbo_tasks::value]
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, OperationValue)]
#[serde(rename_all = "camelCase")]
pub enum JsxRuntime {
    Automatic,
    Classic,
}

impl JsxRuntime {
    pub fn as_str(&self) -> &'static str {
        match self {
            JsxRuntime::Automatic => "automatic",
            JsxRuntime::Classic => "classic",
        }
    }
}

#[turbo_tasks::value(eq = "manual")]
#[derive(Clone, Debug, PartialEq, Default, Deserialize, OperationValue)]
#[serde(rename_all = "camelCase")]
pub struct ReactConfig {
    /// JSX runtime mode: "automatic" (default) or "classic"
    pub runtime: Option<JsxRuntime>,
}


#[turbo_tasks::value(eq = "manual")]
#[derive(Clone, Debug, PartialEq, Default, Deserialize, OperationValue)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -944,6 +953,11 @@ impl Config {
self.styles.clone().unwrap_or_default().cell()
}

#[turbo_tasks::function]
pub fn react(&self) -> Vc<ReactConfig> {
self.react.clone().unwrap_or_default().cell()
}

#[turbo_tasks::function]
pub fn output(&self) -> Vc<OutputConfig> {
self.output.clone().unwrap_or_default().cell()
Expand Down
8 changes: 2 additions & 6 deletions crates/pack-core/src/transform_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,9 @@ pub async fn get_jsx_transform_options(
enable_react_refresh: bool,
) -> Result<Vc<JsxTransformOptions>> {
let tsconfig = get_typescript_options(project_path.clone()).await?;

let is_emotion_enabled = config.styles().await?.emotion.is_some();
let react_config = config.react().await?;

// [NOTE]: ref: WEB-901
// next.js does not allow to overriding react runtime config via tsconfig /
// jsconfig, it forces overrides into automatic runtime instead.
// [TODO]: we need to emit / validate config message like next.js devserver does
let react_transform_options = JsxTransformOptions {
development: mode.await?.is_react_development(),
// https://github.com/vercel/next.js/blob/3dc2c1c7f8441cdee31da9f7e0986d654c7fd2e7/packages/next/src/build/swc/options.ts#L112
Expand All @@ -137,7 +133,7 @@ pub async fn get_jsx_transform_options(
} else {
None
},
runtime: Some("automatic".into()),
runtime: react_config.runtime.clone().or(Some("automatic".into())),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To align with the suggested change of using an enum for runtime in ReactConfig, this line should be updated to handle the enum type. This involves mapping the enum variant to its string representation.

Suggested change
runtime: react_config.runtime.clone().or(Some("automatic".into())),
runtime: react_config.runtime.as_ref().map(|r| r.as_str().into()).or(Some("automatic".into())),

react_refresh: enable_react_refresh,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"import": "input/index.js",
"name": "main"
}
]
],
"react": {
"runtime": "classic"
}
}
}

This file was deleted.

Loading