Skip to content

Commit

Permalink
feat: init
Browse files Browse the repository at this point in the history
  • Loading branch information
ZEJIA-LIU committed Sep 2, 2024
1 parent eefb302 commit 62c23b9
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

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

16 changes: 16 additions & 0 deletions crates/swc_plugin_compile_mode_pre_process/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "swc_plugin_compile_mode_pre_process"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]

[dependencies]
serde = { workspace = true }
serde_json = { workspace = true }
swc_core = { workspace = true }
regex = "1.10.2"

[dev-dependencies]
swc_core = { workspace = true, features = ["ecma_parser", "ecma_codegen"] }
42 changes: 42 additions & 0 deletions crates/swc_plugin_compile_mode_pre_process/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use swc_core::{
ecma::{
ast::Program,
visit::{as_folder, FoldWith, VisitMut},
},
plugin::{
plugin_transform,
proxies::TransformPluginProgramMetadata
}
};
use serde::{Deserialize};
use std::collections::HashMap;

mod transform;
mod tests;
struct SerdeDefault;
impl SerdeDefault {
fn platform_default () -> String {
String::from("WEAPP")
}
}


#[derive(Deserialize, Debug)]
pub struct PluginConfig {
#[serde(default = "SerdeDefault::platform_default")]
pub platform: String,
}

#[plugin_transform]
pub fn process_transform(program: Program, metadata: TransformPluginProgramMetadata) -> Program {
let config = serde_json::from_str::<PluginConfig>(
&metadata
.get_transform_plugin_config()
.unwrap()
)
.unwrap();

let visitor: Box<dyn VisitMut> = Box::new(transform::TransformVisitor::new(config));

program.fold_with(&mut as_folder(visitor))
}
21 changes: 21 additions & 0 deletions crates/swc_plugin_compile_mode_pre_process/src/tests/init.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use swc_core::ecma::transforms::testing::test;

use super::{get_syntax_config, tr};

test!(
get_syntax_config(),
|_| tr(),
should_do_nothing,
r#"
function Index () {
return (
<View>
<Image src={mySrc} />
<View>
<Text>{myText}</Text>
</View>
</View>
)
}
"#
);
28 changes: 28 additions & 0 deletions crates/swc_plugin_compile_mode_pre_process/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use swc_core::ecma::{
parser,
visit::{as_folder, Fold},
};
use crate::{
PluginConfig,
transform::*,
};

mod init;

pub fn tr () -> impl Fold {
let config = serde_json::from_str::<PluginConfig>(
r#"
{
}"#
)
.unwrap();
let visitor = TransformVisitor::new(config);
as_folder(visitor)
}

pub fn get_syntax_config () -> parser::Syntax {
parser::Syntax::Es(parser::EsConfig {
jsx: true,
..Default::default()
})
}
36 changes: 36 additions & 0 deletions crates/swc_plugin_compile_mode_pre_process/src/transform.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use swc_core::{
ecma::{
ast::*,
visit::{VisitMutWith, VisitMut},
},
plugin::{
plugin_transform,
proxies::TransformPluginProgramMetadata
}
};
use crate::PluginConfig;
pub struct TransformVisitor {

}

impl TransformVisitor {
pub fn new(config: PluginConfig) -> Self {
TransformVisitor {

}
}
}

impl VisitMut for TransformVisitor {
fn visit_mut_jsx_element (&mut self, el: &mut JSXElement) {
match &el.opening.name {
JSXElementName::Ident(ident) => {
println!("TagName {}", ident.sym);
},
_ => {
println!("??");
}
}
el.visit_mut_children_with(self)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function Index() {
return <View>

<Image src={mySrc}/>

<View>

<Text>{myText}</Text>

</View>

</View>;
}

0 comments on commit 62c23b9

Please sign in to comment.