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/fine-places-boil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---

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

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ resolver = "2"

members = [
"packages/react/transform",
"packages/react/transform/crates/*",
"packages/react/transform/swc-plugin-reactlynx",
"packages/react/transform/swc-plugin-reactlynx-compat",
"packages/web-platform/inline-style-parser",
Expand All @@ -16,6 +17,8 @@ hex = "0.4.3"
indexmap = "2.7.0"
js-sys = "0.3.77"
lazy_static = "1.5.0"
napi = { version = "2.7.0", default-features = false, features = ["napi4"] }
napi-derive = "2.7.0"
once_cell = "1.20.2"
regex = "1.11.1"
rustc-hash = "2.1.1"
Expand Down
2 changes: 2 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ export default tseslint.config(
'packages/react/transform/tests/__swc_snapshots__/**',
'packages/react/transform/__test__/**/__snapshots__/**',

'packages/react/transform/**/tests/__swc_snapshots__/**',

// Configs
'eslint.config.js',
'vitest.config.ts',
Expand Down
1 change: 1 addition & 0 deletions packages/react/.dprint.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

// Snapshot
"transform/tests/__swc_snapshots__/**",
"transform/**/tests/__swc_snapshots__/**",
"transform/__test__/**/__snapshots__/**",
],
}
6 changes: 4 additions & 2 deletions packages/react/transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ crate-type = ["cdylib"]
convert_case = { workspace = true }
hex = { workspace = true }
indexmap = { workspace = true }
napi = { version = "2.7.0", default-features = false, features = ["napi4"] }
napi-derive = "2.7.0"
napi = { workspace = true }
napi-derive = { workspace = true }
once_cell = { workspace = true }
regex = { workspace = true }
rustc-hash = { 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", "css_parser", "css_ast", "css_visit", "css_codegen", "__visit", "__testing_transform"] }
swc_plugin_css_scope = { path = "./crates/swc_plugin_css_scope" }
swc_plugins_shared = { path = './crates/swc_plugins_shared' }
version-compare = { workspace = true }

[target."cfg(all(target_arch = \"wasm32\", target_os = \"unknown\"))".dependencies.getrandom]
Expand Down
14 changes: 14 additions & 0 deletions packages/react/transform/crates/swc_plugin_css_scope/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "swc_plugin_css_scope"
version = "0.1.0"
edition = "2021"

[lib]
path = "lib.rs"

[dependencies]
napi = { workspace = true }
napi-derive = { workspace = true }
regex = { 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", "css_parser", "css_ast", "css_visit", "css_codegen", "__visit", "__testing_transform"] }
swc_plugins_shared = { path = "../swc_plugins_shared" }
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use crate::calc_hash;
use napi_derive::napi;
use regex::Regex;
use swc_core::{
common::{
Expand All @@ -11,6 +9,9 @@ use swc_core::{
visit::{VisitMut, VisitMutWith},
},
};
use swc_plugins_shared::utils::calc_hash;

pub mod napi;

/// CSSScope refers to the
///
Expand All @@ -31,50 +32,8 @@ pub enum CSSScope {
Modules,
}

impl napi::bindgen_prelude::FromNapiValue for CSSScope {
unsafe fn from_napi_value(
env: napi::bindgen_prelude::sys::napi_env,
napi_val: napi::bindgen_prelude::sys::napi_value,
) -> napi::bindgen_prelude::Result<Self> {
let val = <&str>::from_napi_value(env, napi_val).map_err(|e| {
napi::bindgen_prelude::error!(
e.status,
"Failed to convert napi value into enum `{}`. {}",
"RemoveCSSScope",
e,
)
})?;
match val {
"all" => Ok(CSSScope::All),
"none" => Ok(CSSScope::None),
"modules" => Ok(CSSScope::Modules),
_ => Err(napi::bindgen_prelude::error!(
napi::bindgen_prelude::Status::InvalidArg,
"value `{}` does not match any variant of enum `{}`",
val,
"RemoveCSSScope"
)),
}
}
}

impl napi::bindgen_prelude::ToNapiValue for CSSScope {
unsafe fn to_napi_value(
env: napi::bindgen_prelude::sys::napi_env,
val: Self,
) -> napi::bindgen_prelude::Result<napi::bindgen_prelude::sys::napi_value> {
match val {
CSSScope::All => <&str>::to_napi_value(env, "all"),
CSSScope::None => <&str>::to_napi_value(env, "none"),
CSSScope::Modules => <&str>::to_napi_value(env, "modules"),
}
}
}

#[napi(object)]
#[derive(Clone, Debug)]
pub struct CSSScopeVisitorConfig {
#[napi(ts_type = "'all' | 'none' | 'modules'")]
/// @public
pub mode: CSSScope,

Expand Down
144 changes: 144 additions & 0 deletions packages/react/transform/crates/swc_plugin_css_scope/napi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
use crate::{
CSSScope as CoreCSSScope, CSSScopeVisitor as CoreVisitor, CSSScopeVisitorConfig as CoreConfig,
};
use napi_derive::napi;
use swc_core::{
common::comments::Comments,
ecma::{ast::*, visit::VisitMut},
};

#[derive(Clone, Copy, Debug)]
pub enum CSSScope {
All,
None,
Modules,
}

impl napi::bindgen_prelude::FromNapiValue for CSSScope {
unsafe fn from_napi_value(
env: napi::bindgen_prelude::sys::napi_env,
napi_val: napi::bindgen_prelude::sys::napi_value,
) -> napi::bindgen_prelude::Result<Self> {
let val = <&str>::from_napi_value(env, napi_val).map_err(|e| {
napi::bindgen_prelude::error!(
e.status,
"Failed to convert napi value into enum `{}`. {}",
"CSSScope",
e,
)
})?;
match val {
"all" => Ok(CSSScope::All),
"none" => Ok(CSSScope::None),
"modules" => Ok(CSSScope::Modules),
_ => Err(napi::bindgen_prelude::error!(
napi::bindgen_prelude::Status::InvalidArg,
"value `{}` does not match any variant of enum `{}`",
val,
"CSSScope"
)),
}
}
}

impl napi::bindgen_prelude::ToNapiValue for CSSScope {
unsafe fn to_napi_value(
env: napi::bindgen_prelude::sys::napi_env,
val: Self,
) -> napi::bindgen_prelude::Result<napi::bindgen_prelude::sys::napi_value> {
match val {
CSSScope::All => <&str>::to_napi_value(env, "all"),
CSSScope::None => <&str>::to_napi_value(env, "none"),
CSSScope::Modules => <&str>::to_napi_value(env, "modules"),
}
}
}

impl From<CSSScope> for CoreCSSScope {
fn from(val: CSSScope) -> Self {
match val {
CSSScope::All => CoreCSSScope::All,
CSSScope::None => CoreCSSScope::None,
CSSScope::Modules => CoreCSSScope::Modules,
}
}
}

impl From<CoreCSSScope> for CSSScope {
fn from(val: CoreCSSScope) -> Self {
match val {
CoreCSSScope::All => CSSScope::All,
CoreCSSScope::None => CSSScope::None,
CoreCSSScope::Modules => CSSScope::Modules,
}
}
}

#[napi(object)]
#[derive(Clone, Debug)]
pub struct CSSScopeVisitorConfig {
#[napi(ts_type = "'all' | 'none' | 'modules'")]
/// @public
pub mode: CSSScope,

/// @public
pub filename: String,
}

impl Default for CSSScopeVisitorConfig {
fn default() -> Self {
CSSScopeVisitorConfig {
mode: CSSScope::None,
filename: "index.jsx".into(),
}
}
}

impl From<CSSScopeVisitorConfig> for CoreConfig {
fn from(val: CSSScopeVisitorConfig) -> Self {
CoreConfig {
mode: val.mode.into(),
filename: val.filename,
}
}
}

impl From<CoreConfig> for CSSScopeVisitorConfig {
fn from(val: CoreConfig) -> Self {
CSSScopeVisitorConfig {
mode: val.mode.into(),
filename: val.filename,
}
}
}

pub struct CSSScopeVisitor<C>
where
C: Comments,
{
inner: CoreVisitor<C>,
}

impl<C> CSSScopeVisitor<C>
where
C: Comments,
{
pub fn new(cfg: CSSScopeVisitorConfig, comments: Option<C>) -> Self {
Self {
inner: CoreVisitor::new(cfg.into(), comments),
}
}
}

impl<C> VisitMut for CSSScopeVisitor<C>
where
C: Comments,
{
fn visit_mut_expr(&mut self, n: &mut Expr) {
self.inner.visit_mut_expr(n);
}

fn visit_mut_module(&mut self, n: &mut Module) {
self.inner.visit_mut_module(n);
}
}
14 changes: 14 additions & 0 deletions packages/react/transform/crates/swc_plugins_shared/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "swc_plugins_shared"
version = "0.1.0"
edition = "2021"

[lib]
path = "lib.rs"

[dependencies]
hex = { 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"] }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod utils;
2 changes: 1 addition & 1 deletion packages/react/transform/src/css.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use swc_core::{
ecma::{ast::Expr, utils::is_literal},
};

use crate::utils::jsonify;
use swc_plugins_shared::utils::jsonify;

static EXTRACT_CSS_DIAGNOSTIC_ID: &str = "react-lynx-extract-css";

Expand Down
6 changes: 2 additions & 4 deletions packages/react/transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ mod css;
mod esbuild;
mod swc_plugin_compat;
mod swc_plugin_compat_post;
mod swc_plugin_css_scope;
mod swc_plugin_define_dce;
mod swc_plugin_directive_dce;
mod swc_plugin_dynamic_import;
Expand All @@ -21,7 +20,6 @@ mod swc_plugin_snapshot;
mod swc_plugin_worklet;
mod swc_plugin_worklet_post_process;
mod target;
mod utils;

use std::vec;

Expand Down Expand Up @@ -64,7 +62,7 @@ use swc_core::{
// So we have to use different name
use swc_plugin_compat::{CompatVisitor, CompatVisitorConfig};
use swc_plugin_compat_post::CompatPostVisitor;
use swc_plugin_css_scope::{CSSScopeVisitor, CSSScopeVisitorConfig};
use swc_plugin_css_scope::napi::{CSSScopeVisitor, CSSScopeVisitorConfig};
use swc_plugin_define_dce::DefineDCEVisitorConfig;
use swc_plugin_directive_dce::{DirectiveDCEVisitor, DirectiveDCEVisitorConfig};
use swc_plugin_dynamic_import::{DynamicImportVisitor, DynamicImportVisitorConfig};
Expand All @@ -73,7 +71,7 @@ use swc_plugin_refresh::{RefreshVisitor, RefreshVisitorConfig};
use swc_plugin_shake::{ShakeVisitor, ShakeVisitorConfig};
use swc_plugin_snapshot::{JSXTransformer, JSXTransformerConfig};
use swc_plugin_worklet::{WorkletVisitor, WorkletVisitorConfig};
use utils::calc_hash;
use swc_plugins_shared::utils::calc_hash;

#[derive(Debug, PartialEq, Clone, Copy)]
pub enum TransformMode {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use swc_core::{
},
};

use crate::utils::jsonify;
use swc_plugins_shared::utils::jsonify;

#[napi(object)]
#[derive(Clone, Debug)]
Expand Down
Loading
Loading