vite-plugin-graphql-loader 1.0.5 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +37 -0
- package/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/dist/snippets.d.ts +2 -2
- package/dist/snippets.d.ts.map +1 -1
- package/dist/snippets.js +15 -15
- package/package.json +6 -5
package/README.md
CHANGED
|
@@ -30,3 +30,40 @@ export default defineConfig({
|
|
|
30
30
|
plugins: [graphqlLoader()],
|
|
31
31
|
});
|
|
32
32
|
```
|
|
33
|
+
|
|
34
|
+
Now you can import queries from `.gql` or `.graphql` files.
|
|
35
|
+
|
|
36
|
+
`example.graphql`:
|
|
37
|
+
|
|
38
|
+
```graphql
|
|
39
|
+
fragment ExampleFragment on example {
|
|
40
|
+
id
|
|
41
|
+
name
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
query ExampleQuery {
|
|
45
|
+
example {
|
|
46
|
+
...ExampleFragment
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
`example.js`:
|
|
52
|
+
|
|
53
|
+
```javascript
|
|
54
|
+
import ExampleQuery, { ExampleFragment } from "./example.graphql";
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
If you are using TypeScript, you will have to declare `.gql` or `.graphql` files:
|
|
58
|
+
|
|
59
|
+
`graphql.d.ts`:
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
declare module "*.gql";
|
|
63
|
+
|
|
64
|
+
// Or if you aren't using fragments:
|
|
65
|
+
// declare module "*.gql" {
|
|
66
|
+
// const Query: import("graphql").DocumentNode;
|
|
67
|
+
// export default Query;
|
|
68
|
+
// }
|
|
69
|
+
```
|
package/dist/index.js
CHANGED
|
@@ -35,7 +35,7 @@ const vitePluginGraphqlLoader = () => {
|
|
|
35
35
|
${source}
|
|
36
36
|
`;
|
|
37
37
|
const headerCode = `
|
|
38
|
-
|
|
38
|
+
const doc = ${JSON.stringify(documentNode)};
|
|
39
39
|
doc.loc.source = ${JSON.stringify(documentNode.loc.source)};
|
|
40
40
|
`;
|
|
41
41
|
let outputCode = "";
|
|
@@ -48,7 +48,7 @@ doc.loc.source = ${JSON.stringify(documentNode.loc.source)};
|
|
|
48
48
|
}, 0);
|
|
49
49
|
if (operationCount < 1) {
|
|
50
50
|
outputCode += `
|
|
51
|
-
|
|
51
|
+
export default doc;
|
|
52
52
|
`;
|
|
53
53
|
}
|
|
54
54
|
else {
|
|
@@ -66,7 +66,7 @@ doc.loc.source = ${JSON.stringify(documentNode.loc.source)};
|
|
|
66
66
|
}
|
|
67
67
|
const opName = op.name.value;
|
|
68
68
|
outputCode += `
|
|
69
|
-
|
|
69
|
+
export const ${opName} = oneQuery(doc, "${opName}");
|
|
70
70
|
`;
|
|
71
71
|
}
|
|
72
72
|
}
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;AAAA,4CAAoB;AACpB,8DAA8B;AAC9B,yCAA+C;AAK/C,MAAM,aAAa,GAAG,CAAC,MAAc,EAAE,EAAE;IACrC,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;IACzC,IAAI,UAAU,GAAG,iBAAM,CAAC;IAExB,KAAK,CAAC,IAAI,CAAC,CAAC,IAAY,EAAE,EAAE;QACxB,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,mBAAmB,CAAC,CAAC;QAC/C,IAAI,MAAM,EAAE;YACR,MAAM,CAAC,CAAC,EAAE,UAAU,CAAC,GAAG,MAAM,CAAC;YAC/B,MAAM,aAAa,GAAG,iBAAiB,UAAU,YAAY,CAAC;YAC9D,MAAM,gBAAgB,GAAG,mDAAmD,aAAa,iBAAiB,CAAC;YAC3G,UAAU,IAAI,gBAAgB,GAAG,YAAE,CAAC,GAAG,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,IAAI,KAAK,CACX,4EAA4E,CAC/E,CAAC;6BACL;iCAAM;gCACH,SAAS;6BACZ;yBACJ;wBAED,MAAM,MAAM,GAAG,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC;wBAC7B,UAAU,IAAI;eACvB,MAAM,qBAAqB,MAAM;iBAC/B,CAAC;qBACG;iBACJ;aACJ;YAED,MAAM,gBAAgB,GAAG,aAAa,CAAC,MAAM,CAAC,CAAC;YAC/C,MAAM,OAAO,GACT,UAAU;gBACV,YAAE,CAAC,GAAG;gBACN,gBAAgB;gBAChB,YAAE,CAAC,GAAG;gBACN,UAAU;gBACV,YAAE,CAAC,GAAG,CAAC;YAEX,OAAO,OAAO,CAAC;QACnB,CAAC;KACJ,CAAC;AACN,CAAC,CAAC;AAhFW,QAAA,uBAAuB,2BAgFlC;AAEF,kBAAe,+BAAuB,CAAC"}
|
package/dist/snippets.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export declare const UNIQUE = "\
|
|
2
|
-
export declare const ONE_QUERY = "\n// Collect any fragment/type references from a node, adding them to the refs Set\nfunction collectFragmentReferences(node, refs) {\n if (node.kind === \"FragmentSpread\") {\n refs.add(node.name.value);\n } else if (node.kind === \"VariableDefinition\") {\n
|
|
1
|
+
export declare const UNIQUE = "\nconst names = {};\nfunction unique(defs) {\n return defs.filter(\n function(def) {\n if (def.kind !== 'FragmentDefinition') return true;\n const name = def.name.value\n if (names[name]) {\n return false;\n } else {\n names[name] = true;\n return true;\n }\n }\n )\n}\n";
|
|
2
|
+
export declare const ONE_QUERY = "\n// Collect any fragment/type references from a node, adding them to the refs Set\nfunction collectFragmentReferences(node, refs) {\n if (node.kind === \"FragmentSpread\") {\n refs.add(node.name.value);\n } else if (node.kind === \"VariableDefinition\") {\n const type = node.type;\n if (type.kind === \"NamedType\") {\n refs.add(type.name.value);\n }\n }\n if (node.selectionSet) {\n node.selectionSet.selections.forEach(function(selection) {\n collectFragmentReferences(selection, refs);\n });\n }\n if (node.variableDefinitions) {\n node.variableDefinitions.forEach(function(def) {\n collectFragmentReferences(def, refs);\n });\n }\n if (node.definitions) {\n node.definitions.forEach(function(def) {\n collectFragmentReferences(def, refs);\n });\n }\n}\nconst definitionRefs = {};\n(function extractReferences() {\n doc.definitions.forEach(function(def) {\n if (def.name) {\n const refs = new Set();\n collectFragmentReferences(def, refs);\n definitionRefs[def.name.value] = refs;\n }\n });\n})();\nfunction findOperation(doc, name) {\n for (let i = 0; i < doc.definitions.length; i++) {\n const element = doc.definitions[i];\n if (element.name && element.name.value == name) {\n return element;\n }\n }\n}\nfunction oneQuery(doc, operationName) {\n // Copy the DocumentNode, but clear out the definitions\n const newDoc = {\n kind: doc.kind,\n definitions: [findOperation(doc, operationName)]\n };\n if (doc.hasOwnProperty(\"loc\")) {\n newDoc.loc = doc.loc;\n }\n // Now, for the operation we're running, find any fragments referenced by\n // it or the fragments it references\n const opRefs = definitionRefs[operationName] || new Set();\n const allRefs = new Set();\n let newRefs = new Set();\n // IE 11 doesn't support \"new Set(iterable)\", so we add the members of opRefs to newRefs one by one\n opRefs.forEach(function(refName) {\n newRefs.add(refName);\n });\n while (newRefs.size > 0) {\n const prevRefs = newRefs;\n newRefs = new Set();\n prevRefs.forEach(function(refName) {\n if (!allRefs.has(refName)) {\n allRefs.add(refName);\n const childRefs = definitionRefs[refName] || new Set();\n childRefs.forEach(function(childRef) {\n newRefs.add(childRef);\n });\n }\n });\n }\n allRefs.forEach(function(refName) {\n const op = findOperation(doc, refName);\n if (op) {\n newDoc.definitions.push(op);\n }\n });\n return newDoc;\n}\n\nexport default doc;\n";
|
|
3
3
|
//# sourceMappingURL=snippets.d.ts.map
|
package/dist/snippets.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"snippets.d.ts","sourceRoot":"","sources":["../src/snippets.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,MAAM,
|
|
1
|
+
{"version":3,"file":"snippets.d.ts","sourceRoot":"","sources":["../src/snippets.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,MAAM,yUAgBlB,CAAC;AAEF,eAAO,MAAM,SAAS,6gFAsFrB,CAAC"}
|
package/dist/snippets.js
CHANGED
|
@@ -2,12 +2,12 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ONE_QUERY = exports.UNIQUE = void 0;
|
|
4
4
|
exports.UNIQUE = `
|
|
5
|
-
|
|
5
|
+
const names = {};
|
|
6
6
|
function unique(defs) {
|
|
7
7
|
return defs.filter(
|
|
8
8
|
function(def) {
|
|
9
9
|
if (def.kind !== 'FragmentDefinition') return true;
|
|
10
|
-
|
|
10
|
+
const name = def.name.value
|
|
11
11
|
if (names[name]) {
|
|
12
12
|
return false;
|
|
13
13
|
} else {
|
|
@@ -24,7 +24,7 @@ function collectFragmentReferences(node, refs) {
|
|
|
24
24
|
if (node.kind === "FragmentSpread") {
|
|
25
25
|
refs.add(node.name.value);
|
|
26
26
|
} else if (node.kind === "VariableDefinition") {
|
|
27
|
-
|
|
27
|
+
const type = node.type;
|
|
28
28
|
if (type.kind === "NamedType") {
|
|
29
29
|
refs.add(type.name.value);
|
|
30
30
|
}
|
|
@@ -45,19 +45,19 @@ function collectFragmentReferences(node, refs) {
|
|
|
45
45
|
});
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
|
-
|
|
48
|
+
const definitionRefs = {};
|
|
49
49
|
(function extractReferences() {
|
|
50
50
|
doc.definitions.forEach(function(def) {
|
|
51
51
|
if (def.name) {
|
|
52
|
-
|
|
52
|
+
const refs = new Set();
|
|
53
53
|
collectFragmentReferences(def, refs);
|
|
54
54
|
definitionRefs[def.name.value] = refs;
|
|
55
55
|
}
|
|
56
56
|
});
|
|
57
57
|
})();
|
|
58
58
|
function findOperation(doc, name) {
|
|
59
|
-
for (
|
|
60
|
-
|
|
59
|
+
for (let i = 0; i < doc.definitions.length; i++) {
|
|
60
|
+
const element = doc.definitions[i];
|
|
61
61
|
if (element.name && element.name.value == name) {
|
|
62
62
|
return element;
|
|
63
63
|
}
|
|
@@ -65,7 +65,7 @@ function findOperation(doc, name) {
|
|
|
65
65
|
}
|
|
66
66
|
function oneQuery(doc, operationName) {
|
|
67
67
|
// Copy the DocumentNode, but clear out the definitions
|
|
68
|
-
|
|
68
|
+
const newDoc = {
|
|
69
69
|
kind: doc.kind,
|
|
70
70
|
definitions: [findOperation(doc, operationName)]
|
|
71
71
|
};
|
|
@@ -74,20 +74,20 @@ function oneQuery(doc, operationName) {
|
|
|
74
74
|
}
|
|
75
75
|
// Now, for the operation we're running, find any fragments referenced by
|
|
76
76
|
// it or the fragments it references
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
77
|
+
const opRefs = definitionRefs[operationName] || new Set();
|
|
78
|
+
const allRefs = new Set();
|
|
79
|
+
let newRefs = new Set();
|
|
80
80
|
// IE 11 doesn't support "new Set(iterable)", so we add the members of opRefs to newRefs one by one
|
|
81
81
|
opRefs.forEach(function(refName) {
|
|
82
82
|
newRefs.add(refName);
|
|
83
83
|
});
|
|
84
84
|
while (newRefs.size > 0) {
|
|
85
|
-
|
|
85
|
+
const prevRefs = newRefs;
|
|
86
86
|
newRefs = new Set();
|
|
87
87
|
prevRefs.forEach(function(refName) {
|
|
88
88
|
if (!allRefs.has(refName)) {
|
|
89
89
|
allRefs.add(refName);
|
|
90
|
-
|
|
90
|
+
const childRefs = definitionRefs[refName] || new Set();
|
|
91
91
|
childRefs.forEach(function(childRef) {
|
|
92
92
|
newRefs.add(childRef);
|
|
93
93
|
});
|
|
@@ -95,7 +95,7 @@ function oneQuery(doc, operationName) {
|
|
|
95
95
|
});
|
|
96
96
|
}
|
|
97
97
|
allRefs.forEach(function(refName) {
|
|
98
|
-
|
|
98
|
+
const op = findOperation(doc, refName);
|
|
99
99
|
if (op) {
|
|
100
100
|
newDoc.definitions.push(op);
|
|
101
101
|
}
|
|
@@ -103,6 +103,6 @@ function oneQuery(doc, operationName) {
|
|
|
103
103
|
return newDoc;
|
|
104
104
|
}
|
|
105
105
|
|
|
106
|
-
|
|
106
|
+
export default doc;
|
|
107
107
|
`;
|
|
108
108
|
//# sourceMappingURL=snippets.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-graphql-loader",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "A Vite plugin for loading GraphQL files.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
"package.json"
|
|
10
10
|
],
|
|
11
11
|
"scripts": {
|
|
12
|
-
"test": "
|
|
13
|
-
"build": "
|
|
12
|
+
"test": "vitest",
|
|
13
|
+
"build": "rimraf ./dist && tsc",
|
|
14
14
|
"package:bump": "yarn version --patch",
|
|
15
15
|
"package:publish": "yarn build && yarn package:bump && yarn publish"
|
|
16
16
|
},
|
|
@@ -38,8 +38,9 @@
|
|
|
38
38
|
"@types/glob": "^8.0.1",
|
|
39
39
|
"@types/node": "^16",
|
|
40
40
|
"glob": "^8.1.0",
|
|
41
|
-
"
|
|
41
|
+
"rimraf": "^4.3.1",
|
|
42
42
|
"typescript": "^4.9.5",
|
|
43
|
-
"vite": "^4.1.1"
|
|
43
|
+
"vite": "^4.1.1",
|
|
44
|
+
"vitest": "^0.28.5"
|
|
44
45
|
}
|
|
45
46
|
}
|