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
3 changes: 3 additions & 0 deletions .changeset/hot-socks-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---

---
30 changes: 30 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/react/transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ swc_plugin_define_dce = { path = "./crates/swc_plugin_define_dce" }
swc_plugin_directive_dce = { path = "./crates/swc_plugin_directive_dce" }
swc_plugin_dynamic_import = { path = './crates/swc_plugin_dynamic_import' }
swc_plugin_inject = { path = "./crates/swc_plugin_inject" }
swc_plugin_list = { path = "./crates/swc_plugin_list" }
swc_plugin_shake = { path = './crates/swc_plugin_shake' }
swc_plugin_snapshot = { path = "./crates/swc_plugin_snapshot" }
swc_plugin_worklet = { path = "./crates/swc_plugin_worklet" }
swc_plugins_shared = { path = './crates/swc_plugins_shared' }
version-compare = { workspace = true }
Expand Down
12 changes: 12 additions & 0 deletions packages/react/transform/crates/swc_plugin_list/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "swc_plugin_list"
version = "0.1.0"
edition = "2021"

[lib]
path = "lib.rs"

[dependencies]
swc_core = { workspace = true, features = ["base", "ecma_codegen", "ecma_parser", "ecma_minifier", "ecma_transforms_typescript", "ecma_utils", "ecma_quote", "ecma_transforms_react", "ecma_transforms_optimization", "css_parser", "css_ast", "css_visit", "css_codegen", "__visit", "__testing_transform"] }
swc_plugin_snapshot = { path = "../swc_plugin_snapshot" }
swc_plugins_shared = { path = "../swc_plugins_shared" }
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ use swc_core::{
quote,
};

use crate::swc_plugin_snapshot::jsx_helpers::{
jsx_attr_value, jsx_children_to_expr, jsx_is_list_item,
};
use swc_plugins_shared::jsx_helpers::{jsx_attr_value, jsx_children_to_expr, jsx_is_list_item};

pub struct ListVisitor<C>
where
Expand Down Expand Up @@ -191,11 +189,8 @@ mod tests {
};

use super::ListVisitor;
use crate::{
swc_plugin_snapshot::{JSXTransformer, JSXTransformerConfig},
TransformMode,
};
use swc_plugins_shared::target_napi::TransformTarget;
use swc_plugin_snapshot::napi::{JSXTransformer, JSXTransformerConfig};
use swc_plugins_shared::{target_napi::TransformTarget, transform_mode_napi::TransformMode};

test!(
module,
Expand Down
20 changes: 20 additions & 0 deletions packages/react/transform/crates/swc_plugin_snapshot/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "swc_plugin_snapshot"
version = "0.1.0"
edition = "2021"

[lib]
path = "lib.rs"

[dependencies]
convert_case = { workspace = true }
hex = { workspace = true }
napi = { workspace = true }
napi-derive = { workspace = true }
once_cell = { workspace = true }
regex = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true, features = ["preserve_order"] }
sha-1 = { workspace = true }
swc_core = { workspace = true, features = ["base", "ecma_codegen", "ecma_parser", "ecma_minifier", "ecma_transforms_typescript", "ecma_utils", "ecma_quote", "ecma_transforms_react", "ecma_transforms_optimization"] }
swc_plugins_shared = { path = "../swc_plugins_shared" }
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,24 @@ use swc_core::{
};

mod attr_name;
pub mod jsx_helpers;
mod slot_marker;

use crate::TransformMode;
use swc_plugins_shared::{
css::get_string_inline_style_from_literal, target_napi::TransformTarget, utils::calc_hash,
};
pub mod napi;

use self::{
attr_name::AttrName,
use swc_plugins_shared::{
css::get_string_inline_style_from_literal,
jsx_helpers::{
jsx_attr_name, jsx_attr_to_prop, jsx_attr_value, jsx_children_to_expr,
jsx_is_children_full_dynamic, jsx_is_custom, jsx_is_list, jsx_is_list_item, jsx_name,
jsx_props_to_obj, jsx_text_to_str, transform_jsx_attr_str,
},
target::TransformTarget,
transform_mode::TransformMode,
utils::calc_hash,
};

use self::{
attr_name::AttrName,
slot_marker::{jsx_is_internal_slot, jsx_unwrap_internal_slot, WrapperMarker},
};

Expand Down Expand Up @@ -967,7 +970,6 @@ where
}

/// @internal
#[napi(object)]
#[derive(Clone, Debug)]
pub struct JSXTransformerConfig {
/// @internal
Expand All @@ -979,7 +981,6 @@ pub struct JSXTransformerConfig {
/// @internal
pub filename: String,
/// @internal
#[napi(ts_type = "'LEPUS' | 'JS' | 'MIXED'")]
pub target: TransformTarget,
/// @internal
pub is_dynamic_component: Option<bool>,
Expand Down Expand Up @@ -1562,8 +1563,8 @@ mod tests {
},
};

use crate::{swc_plugin_snapshot::JSXTransformer, TransformMode};
use swc_plugins_shared::target_napi::TransformTarget;
use crate::JSXTransformer;
use swc_plugins_shared::{target::TransformTarget, transform_mode::TransformMode};

test!(
module,
Expand Down
108 changes: 108 additions & 0 deletions packages/react/transform/crates/swc_plugin_snapshot/napi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
use napi_derive::napi;
use swc_core::{
common::comments::Comments,
ecma::{ast::*, visit::VisitMut},
};
use swc_plugins_shared::{target_napi::TransformTarget, transform_mode_napi::TransformMode};

use crate::{
JSXTransformer as CoreJSXTransformer, JSXTransformerConfig as CoreJSXTransformerConfig,
};

/// @internal
#[napi(object)]
#[derive(Clone, Debug)]
pub struct JSXTransformerConfig {
/// @internal
pub preserve_jsx: bool,
/// @internal
pub runtime_pkg: String,
/// @internal
pub jsx_import_source: Option<String>,
/// @internal
pub filename: String,
/// @internal
#[napi(ts_type = "'LEPUS' | 'JS' | 'MIXED'")]
pub target: TransformTarget,
/// @internal
pub is_dynamic_component: Option<bool>,
}

impl Default for JSXTransformerConfig {
fn default() -> Self {
Self {
preserve_jsx: false,
runtime_pkg: "@lynx-js/react".into(),
jsx_import_source: Some("@lynx-js/react".into()),
filename: Default::default(),
target: TransformTarget::LEPUS,
is_dynamic_component: Some(false),
}
}
}

impl From<JSXTransformerConfig> for CoreJSXTransformerConfig {
fn from(val: JSXTransformerConfig) -> Self {
Self {
preserve_jsx: val.preserve_jsx,
runtime_pkg: val.runtime_pkg,
jsx_import_source: val.jsx_import_source,
filename: val.filename,
target: val.target.into(),
is_dynamic_component: val.is_dynamic_component,
}
}
}

impl From<CoreJSXTransformerConfig> for JSXTransformerConfig {
fn from(val: CoreJSXTransformerConfig) -> Self {
Self {
preserve_jsx: val.preserve_jsx,
runtime_pkg: val.runtime_pkg,
jsx_import_source: val.jsx_import_source,
filename: val.filename,
target: val.target.into(),
is_dynamic_component: val.is_dynamic_component,
}
}
}

pub struct JSXTransformer<C>
where
C: Comments + Clone,
{
inner: CoreJSXTransformer<C>,
}

impl<C> JSXTransformer<C>
where
C: Comments + Clone,
{
pub fn with_content_hash(mut self, content_hash: String) -> Self {
self.inner.content_hash = content_hash;
self
}

pub fn new(cfg: JSXTransformerConfig, comments: Option<C>, mode: TransformMode) -> Self {
Self {
inner: CoreJSXTransformer::new(cfg.into(), comments, mode.into()),
}
}
}

impl<C> VisitMut for JSXTransformer<C>
where
C: Comments + Clone,
{
fn visit_mut_jsx_element(&mut self, node: &mut JSXElement) {
self.inner.visit_mut_jsx_element(node)
}

fn visit_mut_module_items(&mut self, n: &mut Vec<ModuleItem>) {
self.inner.visit_mut_module_items(n)
}

fn visit_mut_module(&mut self, n: &mut Module) {
self.inner.visit_mut_module(n)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ use swc_core::{
},
};

use super::{
jsx_helpers::{
jsx_children_to_expr, jsx_has_dynamic_key, jsx_is_children_full_dynamic, jsx_is_custom,
jsx_is_list, jsx_name, jsx_text_to_str,
},
WRAPPER_NODE_2,
use super::WRAPPER_NODE_2;

use swc_plugins_shared::jsx_helpers::{
jsx_children_to_expr, jsx_has_dynamic_key, jsx_is_children_full_dynamic, jsx_is_custom,
jsx_is_list, jsx_name, jsx_text_to_str,
};

pub static INTERNAL_SLOT_STR: &str = "internal-slot";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ convert_case = { workspace = true }
hex = { workspace = true }
napi = { workspace = true }
napi-derive = { workspace = true }
once_cell = { workspace = true }
regex = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true, features = ["preserve_order"] }
sha-1 = { workspace = true }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod css;
pub mod jsx_helpers;
pub mod target;
pub mod target_napi;
pub mod transform_mode;
Expand Down
4 changes: 1 addition & 3 deletions packages/react/transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ mod bundle;
mod esbuild;
mod swc_plugin_compat_post;
mod swc_plugin_extract_str;
mod swc_plugin_list;
mod swc_plugin_refresh;
mod swc_plugin_snapshot;
mod swc_plugin_worklet_post_process;

use std::vec;
Expand Down Expand Up @@ -60,7 +58,7 @@ use swc_plugin_dynamic_import::napi::{DynamicImportVisitor, DynamicImportVisitor
use swc_plugin_inject::napi::{InjectVisitor, InjectVisitorConfig};
use swc_plugin_refresh::{RefreshVisitor, RefreshVisitorConfig};
use swc_plugin_shake::napi::{ShakeVisitor, ShakeVisitorConfig};
use swc_plugin_snapshot::{JSXTransformer, JSXTransformerConfig};
use swc_plugin_snapshot::napi::{JSXTransformer, JSXTransformerConfig};
use swc_plugin_worklet::napi::{WorkletVisitor, WorkletVisitorConfig};
use swc_plugins_shared::{transform_mode_napi::TransformMode, utils::calc_hash};

Expand Down
Loading