Skip to content

Commit

Permalink
feat: unique fragment definition handling init
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabh3112 authored Dec 15, 2023
2 parents fe86616 + ada9d05 commit 18a4de3
Show file tree
Hide file tree
Showing 30 changed files with 4,347 additions and 840 deletions.
70 changes: 38 additions & 32 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ crate-type = ["cdylib"]
swc_core = { version = "0.86.*", features = ["ecma_plugin_transform"] }
swc_ecma_ast = "0.110.9"
swc_ecma_visit = "0.96.9"
graphql-tag = { path = "./transform" }
graphql_tag = { path = "./transforms/graphql_tag" }
unique_identifier = { path = "./transforms/unique_identifier" }
serde = "1.0.193"
serde_json = "1.0.108"

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "graphql-tag-swc-plugin",
"version": "0.1.5",
"version": "0.1.6",
"description": "SWC plugin to expand gql tags at build time",
"author": "rishabh3112 <rishabh31121999@gmail.com>",
"license": "ISC",
Expand Down
34 changes: 29 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,43 @@
// libs
use serde::Deserialize;
use swc_core::plugin::{plugin_transform, proxies::TransformPluginProgramMetadata};
use swc_core::plugin::{
plugin_transform,
proxies::{PluginCommentsProxy, TransformPluginProgramMetadata},
};
use swc_ecma_ast::Program;
use swc_ecma_visit::{as_folder, FoldWith};

// structs
use graphql_tag::structs::{GraphQLTagConfig, TransformVisitor};
use unique_identifier::UniqueIdentifierVisitor;

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Config {
pub import_sources: Option<Vec<String>>,
pub gql_tag_identifiers: Option<Vec<String>>,
pub strip: Option<bool>,
import_sources: Option<Vec<String>>,
gql_tag_identifiers: Option<Vec<String>>,
strip: Option<bool>,
}

#[plugin_transform]
pub fn process_transform(program: Program, data: TransformPluginProgramMetadata) -> Program {
let mut program = program;
let mut unique_visitor = UniqueIdentifierVisitor::new();

program = program.fold_with(&mut as_folder(&mut unique_visitor));

let unique_fn_name = if unique_visitor.count > 0 {
format!("{}{}", unique_visitor.identifier, unique_visitor.count)
} else {
unique_visitor.identifier
};

let default_config = GraphQLTagConfig {
import_sources: vec!["@apollo/client".to_string(), "graphql-tag".into()],
gql_tag_identifiers: vec!["gql".to_string()],
strip: false,
unique_fn_name: unique_fn_name.clone(),
unique_fn_used: false,
};

let config = match data.get_transform_plugin_config() {
Expand All @@ -35,6 +52,8 @@ pub fn process_transform(program: Program, data: TransformPluginProgramMetadata)
.gql_tag_identifiers
.unwrap_or(default_config.gql_tag_identifiers),
strip: config.strip.unwrap_or(false),
unique_fn_name,
unique_fn_used: false,
},
Err(_) => {
println!("Got invalid config for graphql-tag-swc-plugin, using default config instead");
Expand All @@ -45,5 +64,10 @@ pub fn process_transform(program: Program, data: TransformPluginProgramMetadata)
None => default_config,
};

program.fold_with(&mut as_folder(TransformVisitor::new(config)))
program = program.fold_with(&mut as_folder(TransformVisitor::new(
config,
PluginCommentsProxy,
)));

program
}
15 changes: 10 additions & 5 deletions tests/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@ fn graphql_tag_fixture(input: PathBuf) {
test_fixture(
syntax(),
&|_tr| {
as_folder(TransformVisitor::new(GraphQLTagConfig {
import_sources: vec!["@apollo/client".to_string(), "graphql-tag".into()],
gql_tag_identifiers: vec!["gql".to_string()],
strip: true,
}))
as_folder(TransformVisitor::new(
GraphQLTagConfig {
import_sources: vec!["@apollo/client".to_string(), "graphql-tag".into()],
gql_tag_identifiers: vec!["gql".to_string()],
strip: true,
unique_fn_name: "unique".into(),
unique_fn_used: false,
},
_tr.comments.clone(),
))
},
&input,
&output,
Expand Down
19 changes: 17 additions & 2 deletions tests/fixtures/dynamicSegments/output.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
import { gql } from "@apollo/client";
const unique = (definitions)=>{
const names = {};
return definitions.filter((definition)=>{
if (definition.kind !== 'FragmentDefinition') {
return true;
}
const name = definition.name.value;
if (names[name]) {
return false;
} else {
names[name] = true;
return true;
}
});
};
const NAME = "LOL";
const DYNAMIC_FRAGMENT = gql`
fragment name on ${NAME} {
Expand All @@ -18,7 +33,7 @@ const QUERY_WITH_DYNAMIC_SEGMENT = gql`
`;
const STATIC_QUERY = {
"kind": "Document",
"definitions": [
"definitions": /*#__PURE__*/ unique(/*#__PURE__*/ [
{
"kind": "OperationDefinition",
"name": {
Expand Down Expand Up @@ -73,7 +88,7 @@ const STATIC_QUERY = {
]
}
}
].concat(DYNAMIC_FRAGMENT.definitions),
].concat(DYNAMIC_FRAGMENT.definitions)),
"loc": {
"start": 0,
"end": 42,
Expand Down
Loading

0 comments on commit 18a4de3

Please sign in to comment.