Skip to content

Commit

Permalink
feat: add strip functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabh3112 authored Dec 12, 2023
2 parents a43a69a + 472421b commit 3dba0d9
Show file tree
Hide file tree
Showing 19 changed files with 111 additions and 54 deletions.
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.3",
"version": "0.1.4",
"description": "SWC plugin to expand gql tags at build time",
"author": "rishabh3112 <rishabh31121999@gmail.com>",
"license": "ISC",
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ use graphql_tag::structs::{GraphQLTagConfig, TransformVisitor};
pub struct Config {
pub import_sources: Option<Vec<String>>,
pub gql_tag_identifiers: Option<Vec<String>>,
pub strip: Option<bool>,
}

#[plugin_transform]
pub fn process_transform(program: Program, data: TransformPluginProgramMetadata) -> Program {
let default_config = GraphQLTagConfig {
import_sources: vec!["@apollo/client".to_string(), "graphql-tag".into()],
gql_tag_identifiers: vec!["gql".to_string()],
strip: false,
};

let config = match data.get_transform_plugin_config() {
Expand All @@ -32,6 +34,7 @@ pub fn process_transform(program: Program, data: TransformPluginProgramMetadata)
gql_tag_identifiers: config
.gql_tag_identifiers
.unwrap_or(default_config.gql_tag_identifiers),
strip: config.strip.unwrap_or(false),
},
Err(_) => {
println!("Got invalid config for graphql-tag-swc-plugin, using default config instead");
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ fn graphql_tag_fixture(input: PathBuf) {
as_folder(TransformVisitor::new(GraphQLTagConfig {
import_sources: vec!["@apollo/client".to_string(), "graphql-tag".into()],
gql_tag_identifiers: vec!["gql".to_string()],
strip: true,
}))
},
&input,
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/definations/fragment/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ const FRAGMENT = {
],
"loc": {
"start": 0,
"end": 57,
"end": 40,
"source": {
"body": "\n fragment TestFragment on Entity {\n id\n name\n }\n"
"body": "fragment TestFragment on Entity{id name}"
}
}
};
4 changes: 2 additions & 2 deletions tests/fixtures/definations/operation/mutation/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ const MUTATION = {
],
"loc": {
"start": 0,
"end": 82,
"end": 67,
"source": {
"body": "\n mutation testMutation($entity: String) {\n updateEntity(entity: $entity)\n }\n"
"body": "mutation testMutation($entity:String){updateEntity(entity:$entity)}"
}
}
};
4 changes: 2 additions & 2 deletions tests/fixtures/definations/operation/query/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ const QUERY = {
],
"loc": {
"start": 0,
"end": 67,
"end": 35,
"source": {
"body": "\n query testQuery {\n getEntity {\n id\n name\n }\n }\n"
"body": "query testQuery{getEntity{id name}}"
}
}
};
12 changes: 6 additions & 6 deletions tests/fixtures/directives/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ const SIMPLE_DIRECTIVE = {
],
"loc": {
"start": 0,
"end": 67,
"end": 40,
"source": {
"body": "\n query testQuery {\n getEntity {\n id @directive\n }\n }\n"
"body": "query testQuery{getEntity{id@directive}}"
}
}
};
Expand Down Expand Up @@ -133,9 +133,9 @@ const ARGUMENTS_DIRECTIVE = {
],
"loc": {
"start": 0,
"end": 93,
"end": 59,
"source": {
"body": '\n query testQuery {\n getEntity {\n id\n name @directive(arg: "argVal")\n }\n }\n'
"body": 'query testQuery{getEntity{id name@directive(arg:"argVal")}}'
}
}
};
Expand Down Expand Up @@ -237,9 +237,9 @@ const DIRECTIVES_SELECTION_SET = {
],
"loc": {
"start": 0,
"end": 127,
"end": 68,
"source": {
"body": '\n query testQuery {\n getEntity {\n id\n user @directive(arg: "argVal") {\n id\n name\n }\n }\n }\n'
"body": 'query testQuery{getEntity{id user@directive(arg:"argVal"){id name}}}'
}
}
};
4 changes: 2 additions & 2 deletions tests/fixtures/dynamicSegments/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ const STATIC_QUERY = {
].concat(DYNAMIC_FRAGMENT.definitions),
"loc": {
"start": 0,
"end": 90,
"end": 42,
"source": {
"body": "\n query testQuery {\n getEntity {\n ... on LOL {\n lol\n }\n }\n }\n\n \n"
"body": "query testQuery{getEntity{...on LOL{lol}}}"
}
}
};
8 changes: 4 additions & 4 deletions tests/fixtures/importSources/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ const POSITIVE_CASE_1 = {
],
"loc": {
"start": 0,
"end": 39,
"end": 26,
"source": {
"body": "\n query testQuery {\n getEntity\n }\n"
"body": "query testQuery{getEntity}"
}
}
};
Expand Down Expand Up @@ -75,9 +75,9 @@ const POSITIVE_CASE_2 = {
],
"loc": {
"start": 0,
"end": 39,
"end": 26,
"source": {
"body": "\n query testQuery {\n getEntity\n }\n"
"body": "query testQuery{getEntity}"
}
}
};
20 changes: 10 additions & 10 deletions tests/fixtures/selections/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ const NORMAL_SELECTION = {
],
"loc": {
"start": 0,
"end": 67,
"end": 35,
"source": {
"body": "\n query testQuery {\n getEntity {\n id\n name\n }\n }\n"
"body": "query testQuery{getEntity{id name}}"
}
}
};
Expand Down Expand Up @@ -170,9 +170,9 @@ const INLINE_FRAGMENT_SELECTION = {
],
"loc": {
"start": 0,
"end": 176,
"end": 84,
"source": {
"body": "\n query testQuery {\n getEntity {\n id\n ... on User {\n name\n address\n }\n ... on Baby {\n name\n parentAddress\n }\n }\n }\n"
"body": "query testQuery{getEntity{id...on User{name address}...on Baby{name parentAddress}}}"
}
}
};
Expand Down Expand Up @@ -229,9 +229,9 @@ const USER_FRAGMENT = {
],
"loc": {
"start": 0,
"end": 67,
"end": 46,
"source": {
"body": "\n fragment UserFragment on User {\n id\n name\n address\n }\n"
"body": "fragment UserFragment on User{id name address}"
}
}
};
Expand Down Expand Up @@ -288,9 +288,9 @@ const BABY_FRAGMENT = {
],
"loc": {
"start": 0,
"end": 73,
"end": 52,
"source": {
"body": "\n fragment BabyFragment on Baby {\n id\n name\n parentAddress\n }\n"
"body": "fragment BabyFragment on Baby{id name parentAddress}"
}
}
};
Expand Down Expand Up @@ -345,9 +345,9 @@ const FRAGMENT_SPREAD_SELECTION = {
],
"loc": {
"start": 0,
"end": 91,
"end": 58,
"source": {
"body": "\n query testQuery {\n getEntity {\n ...UserFragment\n ...BabyFragment\n }\n }\n"
"body": "query testQuery{getEntity{...UserFragment...BabyFragment}}"
}
}
};
16 changes: 8 additions & 8 deletions tests/fixtures/type/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ const NAMED_TYPE = {
],
"loc": {
"start": 0,
"end": 63,
"end": 47,
"source": {
"body": '\n query testQuery($one: String = "apple") {\n getEntity\n }\n'
"body": 'query testQuery($one:String="apple"){getEntity}'
}
}
};
Expand Down Expand Up @@ -110,9 +110,9 @@ const NAMED_TYPE_NOT_NULL = {
],
"loc": {
"start": 0,
"end": 54,
"end": 40,
"source": {
"body": "\n query testQuery($one: String!) {\n getEntity\n }\n"
"body": "query testQuery($one:String!){getEntity}"
}
}
};
Expand Down Expand Up @@ -168,9 +168,9 @@ const LIST_TYPE = {
],
"loc": {
"start": 0,
"end": 55,
"end": 41,
"source": {
"body": "\n query testQuery($one: [String]) {\n getEntity\n }\n"
"body": "query testQuery($one:[String]){getEntity}"
}
}
};
Expand Down Expand Up @@ -229,9 +229,9 @@ const LIST_TYPE_NOT_NULL = {
],
"loc": {
"start": 0,
"end": 56,
"end": 42,
"source": {
"body": "\n query testQuery($one: [String!]) {\n getEntity\n }\n"
"body": "query testQuery($one:[String!]){getEntity}"
}
}
};
4 changes: 2 additions & 2 deletions tests/fixtures/usage/defaultExport/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ export default {
],
"loc": {
"start": 0,
"end": 102,
"end": 61,
"source": {
"body": "\n query testQuery($a: String!) {\n testQueryName(a: $a) @apple {\n a\n b\n c\n }\n }\n"
"body": "query testQuery($a:String!){testQueryName(a:$a)@apple{a b c}}"
}
}
};
4 changes: 2 additions & 2 deletions tests/fixtures/usage/returnFromArrowFunction/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ const getQuery = ()=>({
],
"loc": {
"start": 0,
"end": 102,
"end": 61,
"source": {
"body": "\n query testQuery($a: String!) {\n testQueryName(a: $a) @apple {\n a\n b\n c\n }\n }\n"
"body": "query testQuery($a:String!){testQueryName(a:$a)@apple{a b c}}"
}
}
});
4 changes: 2 additions & 2 deletions tests/fixtures/usage/returnFromFunctionExpression/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ const getQuery = function() {
],
"loc": {
"start": 0,
"end": 119,
"end": 61,
"source": {
"body": "\n query testQuery($a: String!) {\n testQueryName(a: $a) @apple {\n a\n b\n c\n }\n }\n \n"
"body": "query testQuery($a:String!){testQueryName(a:$a)@apple{a b c}}"
}
}
};
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/values/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,9 @@ const ALL_BASIC_VALUES = {
],
"loc": {
"start": 0,
"end": 328,
"end": 222,
"source": {
"body": '\n query testQuery($var: String) {\n getEntity(\n var: $var\n string: "Hello"\n float: 2.2\n int: 1\n bool: true\n nullVal: null\n enumVal: TestEnumValue\n objectValue: { a: "a", b: 1, c: { a: 1.1, b: [1, "a"] } }\n list: ["string", 1.1, 1, true, null, TestEnumValue, { a: "a" }]\n )\n }\n'
"body": 'query testQuery($var:String){getEntity(var:$var string:"Hello" float:2.2 int:1 bool:true nullVal:null enumVal:TestEnumValue objectValue:{a:"a",b:1,c:{a:1.1,b:[1,"a"]}}list:["string",1.1,1,true,null,TestEnumValue,{a:"a"}])}'
}
}
};
16 changes: 9 additions & 7 deletions transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
use std::collections::HashMap;

// libs
use regex::Regex;
use swc_ecma_ast::*;
use swc_ecma_visit::{VisitMut, VisitMutWith};

// modules
pub mod parser;
pub mod structs;

// helpers
use parser::utils::strip_ignored_characters;

// structs
use structs::{GraphQLTagConfig, TransformVisitor};

Expand Down Expand Up @@ -95,7 +98,6 @@ impl VisitMut for TransformVisitor {

let template = &mut tag_tpl.tpl;
let expressions = template.exprs.clone();
let no_gql_line_regex = Regex::new(r#"(^\$\{.*\}$)"#).unwrap();

let mut data: String = "".into();
for quasi in &mut template.quasis {
Expand All @@ -104,11 +106,11 @@ impl VisitMut for TransformVisitor {

let gql_raw_string = data.to_string();

let gql_text = gql_raw_string
.lines()
.filter(|line| !no_gql_line_regex.is_match(line.trim()))
.map(|line| String::from(line) + "\n")
.collect();
let gql_text = if self.config.strip {
strip_ignored_characters(gql_raw_string)
} else {
gql_raw_string
};

let gql_swc_ast_result = parser::parse_graphql_tag(
gql_text,
Expand Down
2 changes: 1 addition & 1 deletion transform/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use swc_ecma_ast::*;

// modules
mod nodes;
mod utils;
pub mod utils;

// helpers
use nodes::document::create_document;
Expand Down
Loading

0 comments on commit 3dba0d9

Please sign in to comment.