Skip to content

Commit

Permalink
Merge pull request #81 from farm-fe/chore-react-components-dts
Browse files Browse the repository at this point in the history
chore(rust-plugins): 🤖 react-components option dts to filename
  • Loading branch information
CCherry07 authored Oct 20, 2024
2 parents b31e6f7 + ae85d09 commit 9c1ceca
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 23 deletions.
6 changes: 6 additions & 0 deletions rust-plugins/react-components/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @farmfe/plugin-react-components

## 1.0.9

### Patch Changes

- chore: dts to filename

## 1.0.8

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion rust-plugins/react-components/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@farmfe/plugin-react-components",
"version": "1.0.8",
"version": "1.0.9",
"private": false,
"main": "scripts/index.js",
"types": "scripts/index.d.ts",
Expand Down
20 changes: 14 additions & 6 deletions rust-plugins/react-components/src/finish_components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ use farmfe_core::config::config_regex::ConfigRegex;
use crate::find_local_components::{find_local_components, ComponentInfo};
use crate::generate_dts::{generate_dts, GenerateDtsOption};
use crate::resolvers::{get_resolvers_result, ResolverOption};

use crate::Dts;
pub struct FinishComponentsParams<'a> {
pub root_path: String,
pub resolvers: Vec<ResolverOption>,
pub dirs: Vec<ConfigRegex>,
pub filename: String,
pub local: bool,
pub dts: bool,
pub dts: Dts,
pub context_components: &'a Arc<Mutex<HashSet<ComponentInfo>>>,
}

Expand Down Expand Up @@ -43,7 +42,6 @@ pub fn finish_components(params: FinishComponentsParams) {
root_path,
resolvers,
dirs,
filename,
local,
dts,
context_components,
Expand All @@ -60,10 +58,20 @@ pub fn finish_components(params: FinishComponentsParams) {
&local_components,
&resolvers_components,
);
if has_new_or_removed_components && dts {
let filename = match dts {
Dts::Filename(filename) => filename,
Dts::Bool(b) => {
if b {
"components.d.ts".to_string()
} else {
String::new()
}
}
};
if has_new_or_removed_components && !filename.is_empty() {
let generate_dts_option = GenerateDtsOption {
filename,
root_path,
filename,
local,
components: &local_components.iter().collect::<Vec<_>>(),
resolvers_components: &resolvers_components.iter().collect::<Vec<_>>(),
Expand Down
78 changes: 62 additions & 16 deletions rust-plugins/react-components/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,21 @@ pub mod resolvers;

use std::{
collections::HashSet,
fmt,
path::PathBuf,
sync::{Arc, Mutex},
};

use serde::{
de::{self, Visitor},
Deserialize, Deserializer, Serialize, Serializer,
};

use farmfe_core::{
config::{config_regex::ConfigRegex, Config},
module::ModuleType,
plugin::{Plugin, PluginTransformHookResult},
serde_json,
serde_json::{self},
swc_ecma_parser::{Syntax, TsSyntax},
};

Expand All @@ -38,12 +44,63 @@ pub enum ImportMode {
Absolute,
}

#[derive(Clone, Debug)]
pub enum Dts {
Bool(bool),
Filename(String),
}

impl Serialize for Dts {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match *self {
Dts::Bool(ref b) => serializer.serialize_bool(*b),
Dts::Filename(ref s) => serializer.serialize_str(s),
}
}
}

impl<'de> Deserialize<'de> for Dts {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct StringOrBoolVisitor;
impl<'de> Visitor<'de> for StringOrBoolVisitor {
type Value = Dts;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> fmt::Result {
formatter.write_str("a boolean or a string")
}
fn visit_bool<E>(self, value: bool) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(Dts::Bool(value))
}
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(Dts::Filename(value.to_owned()))
}
}
deserializer.deserialize_any(StringOrBoolVisitor)
}
}

impl Default for Dts {
fn default() -> Self {
Dts::Bool(true)
}
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Options {
pub dirs: Option<Vec<ConfigRegex>>,
pub filename: Option<String>,
pub dts: Option<bool>,
pub dts: Option<Dts>,
pub local: Option<bool>,
pub import_mode: Option<ImportMode>,
pub include: Option<Vec<ConfigRegex>>,
Expand All @@ -62,20 +119,15 @@ impl FarmPluginReactComponents {
pub fn new(config: &Config, options: String) -> Self {
let options: Options = serde_json::from_str(&options).unwrap();
let resolvers = options.resolvers.clone().unwrap_or(vec![]);
let filename = options
.filename
.clone()
.unwrap_or("components.d.ts".to_string());
let dirs = options.dirs.clone().unwrap_or(vec![]);
let root_path = config.root.clone();
let components = Arc::new(Mutex::new(HashSet::new()));
finish_components(FinishComponentsParams {
root_path: normalize_path(&root_path),
resolvers,
dirs,
filename,
local: options.local.unwrap_or(true),
dts: options.dts.unwrap_or(true),
dts: options.dts.clone().unwrap_or_default(),
context_components: &components,
});
Self {
Expand Down Expand Up @@ -173,20 +225,14 @@ impl Plugin for FarmPluginReactComponents {
context: &Arc<farmfe_core::context::CompilationContext>,
) -> farmfe_core::error::Result<Option<()>> {
let resolvers = self.options.resolvers.clone().unwrap_or(vec![]);
let filename = self
.options
.filename
.clone()
.unwrap_or("components.d.ts".to_string());
let dirs = self.options.dirs.clone().unwrap_or(vec![]);
let root_path = context.config.root.clone();
finish_components(FinishComponentsParams {
root_path: normalize_path(&root_path),
resolvers,
dirs,
filename,
local: self.options.local.unwrap_or(true),
dts: self.options.dts.unwrap_or(true),
dts: self.options.dts.clone().unwrap_or_default(),
context_components: &self.components,
});

Expand Down

0 comments on commit 9c1ceca

Please sign in to comment.