Skip to main content

stylex_structures/
stylex_state_options.rs

1use indexmap::IndexMap;
2use rustc_hash::FxHashMap;
3use serde::Deserialize;
4
5use stylex_constants::constants::common::DEFAULT_INJECT_PATH;
6use stylex_enums::property_validation_mode::PropertyValidationMode;
7use stylex_enums::style_resolution::StyleResolution;
8
9use crate::{
10  named_import_source::{ImportSources, RuntimeInjection, RuntimeInjectionState},
11  stylex_env::{EnvEntry, JSFunction},
12  stylex_options::{CheckModuleResolution, StyleXOptions},
13};
14
15#[derive(Deserialize, Clone, Debug)]
16pub struct StyleXStateOptions {
17  pub dev: bool,
18  pub test: bool,
19  pub debug: bool,
20  pub enable_font_size_px_to_rem: bool,
21  pub class_name_prefix: String,
22  pub property_validation_mode: PropertyValidationMode,
23  pub enable_debug_class_names: bool,
24  pub enable_debug_data_prop: bool,
25  pub enable_dev_class_names: bool,
26  pub enable_minified_keys: bool,
27  pub enable_inlined_conditional_merge: bool,
28  pub enable_media_query_order: bool,
29  pub enable_logical_styles_polyfill: bool,
30  pub enable_legacy_value_flipping: bool,
31  #[allow(dead_code)]
32  pub enable_ltr_rtl_comments: bool,
33  pub use_real_file_for_source: bool,
34  // pub defined_stylex_css_variables: FxHashMap<String, String>,
35  pub style_resolution: StyleResolution,
36  pub import_sources: Vec<ImportSources>,
37  pub runtime_injection: Option<RuntimeInjectionState>,
38  pub treeshake_compensation: bool,
39  pub inject_stylex_side_effects: bool,
40  pub aliases: Option<FxHashMap<String, Vec<String>>>,
41  pub unstable_module_resolution: CheckModuleResolution,
42  pub sx_prop_name: Option<String>,
43  #[serde(skip)]
44  pub env: IndexMap<String, EnvEntry>,
45  #[serde(skip)]
46  pub debug_file_path: Option<JSFunction>,
47}
48
49impl Default for StyleXStateOptions {
50  fn default() -> Self {
51    StyleXStateOptions {
52      style_resolution: StyleResolution::PropertySpecificity,
53      property_validation_mode: PropertyValidationMode::Silent,
54      dev: false,
55      test: false,
56      debug: false,
57      enable_debug_class_names: false,
58      enable_debug_data_prop: true,
59      enable_dev_class_names: false,
60      enable_logical_styles_polyfill: false,
61      enable_legacy_value_flipping: false,
62      enable_ltr_rtl_comments: false,
63      use_real_file_for_source: true,
64      enable_inlined_conditional_merge: true,
65      enable_media_query_order: true,
66      enable_font_size_px_to_rem: false,
67      enable_minified_keys: true,
68      class_name_prefix: "x".to_string(),
69      import_sources: vec![],
70      treeshake_compensation: false,
71      inject_stylex_side_effects: false,
72      runtime_injection: None,
73      aliases: None,
74      unstable_module_resolution: CheckModuleResolution::CommonJS(
75        StyleXOptions::get_common_js_module_resolution(None),
76      ),
77      sx_prop_name: Some("sx".to_string()),
78      env: IndexMap::new(),
79      debug_file_path: None,
80    }
81  }
82}
83
84impl Default for CheckModuleResolution {
85  fn default() -> Self {
86    CheckModuleResolution::CommonJS(StyleXOptions::get_common_js_module_resolution(None))
87  }
88}
89impl From<StyleXOptions> for StyleXStateOptions {
90  fn from(options: StyleXOptions) -> Self {
91    let runtime_injection = match options.runtime_injection {
92      RuntimeInjection::Boolean(b) => {
93        if b {
94          Some(RuntimeInjectionState::Regular(
95            DEFAULT_INJECT_PATH.to_string(),
96          ))
97        } else {
98          None
99        }
100      },
101      RuntimeInjection::Named(n) => Some(RuntimeInjectionState::Named(n)),
102      RuntimeInjection::Regular(s) => Some(RuntimeInjectionState::Regular(s)),
103    };
104
105    StyleXStateOptions {
106      style_resolution: options.style_resolution,
107      property_validation_mode: options.property_validation_mode,
108      enable_font_size_px_to_rem: options.enable_font_size_px_to_rem,
109      runtime_injection,
110      class_name_prefix: options.class_name_prefix,
111      // defined_stylex_css_variables: options.defined_stylex_css_variables,
112      import_sources: options.import_sources,
113      dev: options.dev,
114      test: options.test,
115      debug: options.debug,
116      enable_debug_class_names: options.enable_debug_class_names,
117      enable_debug_data_prop: options.enable_debug_data_prop,
118      enable_dev_class_names: options.enable_dev_class_names,
119      enable_media_query_order: options.enable_media_query_order,
120      enable_inlined_conditional_merge: options.enable_inlined_conditional_merge,
121      enable_ltr_rtl_comments: options.enable_ltr_rtl_comments,
122      enable_logical_styles_polyfill: options.enable_logical_styles_polyfill,
123      enable_legacy_value_flipping: options.enable_legacy_value_flipping,
124      enable_minified_keys: options.enable_minified_keys,
125      use_real_file_for_source: options.use_real_file_for_source,
126      treeshake_compensation: options.treeshake_compensation,
127      inject_stylex_side_effects: options.inject_stylex_side_effects,
128      aliases: options.aliases,
129      unstable_module_resolution: options.unstable_module_resolution,
130      sx_prop_name: options.sx_prop_name,
131      env: options.env,
132      debug_file_path: options.debug_file_path,
133    }
134  }
135}