Skip to content

Commit

Permalink
feat(napi/transform): export react refresh options (#5533)
Browse files Browse the repository at this point in the history
Co-authored-by: Boshen <boshenc@gmail.com>
  • Loading branch information
underfin and Boshen authored Sep 8, 2024
1 parent 8c9179d commit aba9194
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
18 changes: 18 additions & 0 deletions napi/transform/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,24 @@ export interface ReactBindingOptions {
* @default false
*/
useSpread?: boolean
/** Enable react fast refresh transform */
refresh?: ReactRefreshBindingOptions
}

export interface ReactRefreshBindingOptions {
/**
* Specify the identifier of the refresh registration variable.
*
* @default `$RefreshReg$`.
*/
refreshReg?: string
/**
* Specify the identifier of the refresh signature variable.
*
* @default `$RefreshSig$`.
*/
refreshSig?: string
emitFullSignatures?: boolean
}

export interface SourceMap {
Expand Down
34 changes: 32 additions & 2 deletions napi/transform/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::path::PathBuf;
use napi::Either;
use napi_derive::napi;
use oxc_transformer::{
ArrowFunctionsOptions, ES2015Options, ReactJsxRuntime, ReactOptions, RewriteExtensionsMode,
TypeScriptOptions,
ArrowFunctionsOptions, ES2015Options, ReactJsxRuntime, ReactOptions, ReactRefreshOptions,
RewriteExtensionsMode, TypeScriptOptions,
};

#[napi(object)]
Expand Down Expand Up @@ -140,6 +140,9 @@ pub struct ReactBindingOptions {
///
/// @default false
pub use_spread: Option<bool>,

/// Enable react fast refresh transform
pub refresh: Option<ReactRefreshBindingOptions>,
}

impl From<ReactBindingOptions> for ReactOptions {
Expand All @@ -158,11 +161,38 @@ impl From<ReactBindingOptions> for ReactOptions {
pragma_frag: options.pragma_frag,
use_built_ins: options.use_built_ins,
use_spread: options.use_spread,
refresh: ops.refresh.map(Into::into),
..Default::default()
}
}
}

#[napi(object)]
pub struct ReactRefreshBindingOptions {
/// Specify the identifier of the refresh registration variable.
///
/// @default `$RefreshReg$`.
pub refresh_reg: Option<String>,

/// Specify the identifier of the refresh signature variable.
///
/// @default `$RefreshSig$`.
pub refresh_sig: Option<String>,

pub emit_full_signatures: Option<bool>,
}

impl From<ReactRefreshBindingOptions> for ReactRefreshOptions {
fn from(options: ReactRefreshBindingOptions) -> Self {
let ops = ReactRefreshOptions::default();
ReactRefreshOptions {
refresh_reg: options.refresh_reg.unwrap_or(ops.refresh_reg),
refresh_sig: options.refresh_sig.unwrap_or(ops.refresh_sig),
emit_full_signatures: options.emit_full_signatures.unwrap_or(ops.emit_full_signatures),
}
}
}

#[napi(object)]
pub struct ArrowFunctionsBindingOptions {
/// This option enables the following:
Expand Down

0 comments on commit aba9194

Please sign in to comment.