vite-plugin-graphql-loader
Advanced tools
Comparing version 1.0.5 to 2.0.0
@@ -38,3 +38,3 @@ "use strict"; | ||
const headerCode = ` | ||
var doc = ${JSON.stringify(documentNode)}; | ||
const doc = ${JSON.stringify(documentNode)}; | ||
doc.loc.source = ${JSON.stringify(documentNode.loc.source)}; | ||
@@ -52,3 +52,3 @@ `; | ||
outputCode += ` | ||
module.exports = doc; | ||
export default doc; | ||
`; | ||
@@ -71,3 +71,3 @@ } | ||
outputCode += ` | ||
module.exports["${opName}"] = oneQuery(doc, "${opName}"); | ||
export const ${opName} = oneQuery(doc, "${opName}"); | ||
`; | ||
@@ -74,0 +74,0 @@ } |
@@ -1,3 +0,3 @@ | ||
export declare const UNIQUE = "\nvar names = {};\nfunction unique(defs) {\n return defs.filter(\n function(def) {\n if (def.kind !== 'FragmentDefinition') return true;\n var 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"; | ||
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 var 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}\nvar definitionRefs = {};\n(function extractReferences() {\n doc.definitions.forEach(function(def) {\n if (def.name) {\n var refs = new Set();\n collectFragmentReferences(def, refs);\n definitionRefs[def.name.value] = refs;\n }\n });\n})();\nfunction findOperation(doc, name) {\n for (var i = 0; i < doc.definitions.length; i++) {\n var 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 var 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 var opRefs = definitionRefs[operationName] || new Set();\n var allRefs = new Set();\n var 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 var prevRefs = newRefs;\n newRefs = new Set();\n prevRefs.forEach(function(refName) {\n if (!allRefs.has(refName)) {\n allRefs.add(refName);\n var childRefs = definitionRefs[refName] || new Set();\n childRefs.forEach(function(childRef) {\n newRefs.add(childRef);\n });\n }\n });\n }\n allRefs.forEach(function(refName) {\n var op = findOperation(doc, refName);\n if (op) {\n newDoc.definitions.push(op);\n }\n });\n return newDoc;\n}\n\nmodule.exports = doc;\n"; | ||
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"; | ||
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"; | ||
//# sourceMappingURL=snippets.d.ts.map |
@@ -5,3 +5,3 @@ "use strict"; | ||
exports.UNIQUE = ` | ||
var names = {}; | ||
const names = {}; | ||
function unique(defs) { | ||
@@ -11,3 +11,3 @@ return defs.filter( | ||
if (def.kind !== 'FragmentDefinition') return true; | ||
var name = def.name.value | ||
const name = def.name.value | ||
if (names[name]) { | ||
@@ -29,3 +29,3 @@ return false; | ||
} else if (node.kind === "VariableDefinition") { | ||
var type = node.type; | ||
const type = node.type; | ||
if (type.kind === "NamedType") { | ||
@@ -51,7 +51,7 @@ refs.add(type.name.value); | ||
} | ||
var definitionRefs = {}; | ||
const definitionRefs = {}; | ||
(function extractReferences() { | ||
doc.definitions.forEach(function(def) { | ||
if (def.name) { | ||
var refs = new Set(); | ||
const refs = new Set(); | ||
collectFragmentReferences(def, refs); | ||
@@ -63,4 +63,4 @@ definitionRefs[def.name.value] = refs; | ||
function findOperation(doc, name) { | ||
for (var i = 0; i < doc.definitions.length; i++) { | ||
var element = doc.definitions[i]; | ||
for (let i = 0; i < doc.definitions.length; i++) { | ||
const element = doc.definitions[i]; | ||
if (element.name && element.name.value == name) { | ||
@@ -73,3 +73,3 @@ return element; | ||
// Copy the DocumentNode, but clear out the definitions | ||
var newDoc = { | ||
const newDoc = { | ||
kind: doc.kind, | ||
@@ -83,5 +83,5 @@ definitions: [findOperation(doc, operationName)] | ||
// it or the fragments it references | ||
var opRefs = definitionRefs[operationName] || new Set(); | ||
var allRefs = new Set(); | ||
var newRefs = new Set(); | ||
const opRefs = definitionRefs[operationName] || new Set(); | ||
const allRefs = new Set(); | ||
let newRefs = new Set(); | ||
// IE 11 doesn't support "new Set(iterable)", so we add the members of opRefs to newRefs one by one | ||
@@ -92,3 +92,3 @@ opRefs.forEach(function(refName) { | ||
while (newRefs.size > 0) { | ||
var prevRefs = newRefs; | ||
const prevRefs = newRefs; | ||
newRefs = new Set(); | ||
@@ -98,3 +98,3 @@ prevRefs.forEach(function(refName) { | ||
allRefs.add(refName); | ||
var childRefs = definitionRefs[refName] || new Set(); | ||
const childRefs = definitionRefs[refName] || new Set(); | ||
childRefs.forEach(function(childRef) { | ||
@@ -107,3 +107,3 @@ newRefs.add(childRef); | ||
allRefs.forEach(function(refName) { | ||
var op = findOperation(doc, refName); | ||
const op = findOperation(doc, refName); | ||
if (op) { | ||
@@ -116,4 +116,4 @@ newDoc.definitions.push(op); | ||
module.exports = doc; | ||
export default doc; | ||
`; | ||
//# sourceMappingURL=snippets.js.map |
{ | ||
"name": "vite-plugin-graphql-loader", | ||
"version": "1.0.5", | ||
"version": "2.0.0", | ||
"description": "A Vite plugin for loading GraphQL files.", | ||
@@ -12,4 +12,4 @@ "main": "dist/index.js", | ||
"scripts": { | ||
"test": "ts-node __test__/index.ts", | ||
"build": "rm -rf dist && tsc", | ||
"test": "vitest", | ||
"build": "rimraf ./dist && tsc", | ||
"package:bump": "yarn version --patch", | ||
@@ -42,6 +42,7 @@ "package:publish": "yarn build && yarn package:bump && yarn publish" | ||
"glob": "^8.1.0", | ||
"ts-node": "^10.9.1", | ||
"rimraf": "^4.3.1", | ||
"typescript": "^4.9.5", | ||
"vite": "^4.1.1" | ||
"vite": "^4.1.1", | ||
"vitest": "^0.28.5" | ||
} | ||
} |
@@ -33,1 +33,38 @@ # vite-plugin-graphql-loader | ||
``` | ||
Now you can import queries from `.gql` or `.graphql` files. | ||
`example.graphql`: | ||
```graphql | ||
fragment ExampleFragment on example { | ||
id | ||
name | ||
} | ||
query ExampleQuery { | ||
example { | ||
...ExampleFragment | ||
} | ||
} | ||
``` | ||
`example.js`: | ||
```javascript | ||
import ExampleQuery, { ExampleFragment } from "./example.graphql"; | ||
``` | ||
If you are using TypeScript, you will have to declare `.gql` or `.graphql` files: | ||
`graphql.d.ts`: | ||
```typescript | ||
declare module "*.gql"; | ||
// Or if you aren't using fragments: | ||
// declare module "*.gql" { | ||
// const Query: import("graphql").DocumentNode; | ||
// export default Query; | ||
// } | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15033
70
7