-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
crates/swc_plugin_compile_mode_pre_process/src/tests/init.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
crates/swc_plugin_compile_mode_pre_process/src/tests/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
36
crates/swc_plugin_compile_mode_pre_process/src/transform.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...n_compile_mode_pre_process/tests/__swc_snapshots__/src/tests/init.rs/should_do_nothing.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |