vite-plugin-graphql-loader 1.0.5 → 3.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 CHANGED
@@ -6,6 +6,8 @@
6
6
 
7
7
  A Vite plugin for loading GraphQL .gql and .graphql files, based on [graphql-tag/loader](https://github.com/apollographql/graphql-tag)
8
8
 
9
+ If you are using TypeScript, I recommend using GraphQL Codegen and [vite-plugin-graphql-codegen](https://www.npmjs.com/package/vite-plugin-graphql-codegen) instead to generate TypesScript interfaces for your queries and fragments.
10
+
9
11
  ## Install
10
12
 
11
13
  ```bash
@@ -30,3 +32,45 @@ export default defineConfig({
30
32
  plugins: [graphqlLoader()],
31
33
  });
32
34
  ```
35
+
36
+ Now you can import queries from `.gql` or `.graphql` files.
37
+
38
+ `example.graphql`:
39
+
40
+ ```graphql
41
+ fragment ExampleFragment on example {
42
+ id
43
+ name
44
+ }
45
+
46
+ query ExampleQuery {
47
+ example {
48
+ ...ExampleFragment
49
+ }
50
+ }
51
+ ```
52
+
53
+ `example.js`:
54
+
55
+ ```javascript
56
+ import ExampleQuery, { ExampleFragment } from "./example.graphql";
57
+ ```
58
+
59
+ If you are using TypeScript, you will have to declare `.gql` or `.graphql` files:
60
+
61
+ `graphql.d.ts`:
62
+
63
+ ```typescript
64
+ declare module "*.gql";
65
+ declare module "*.graphql";
66
+
67
+ // Or if you aren't using fragments:
68
+ // declare module "*.gql" {
69
+ // const Query: import("graphql").DocumentNode;
70
+ // export default Query;
71
+ // }
72
+ ```
73
+
74
+ ## Changelog
75
+
76
+ **_v3.0.0_**: Moved from CJS to ESM, inline with Vite 5.0's CJS deprecation. If you are using CommonJS, continue using v2.0 of this package.
package/dist/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ declare module "*.graphql";
2
+ declare module "*.gql";
1
3
  export declare const vitePluginGraphqlLoader: () => {
2
4
  name: string;
3
5
  enforce: "pre";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AA0BA,eAAO,MAAM,uBAAuB;;;sBAOV,MAAM,MAAM,MAAM;CAyE3C,CAAC;AAEF,eAAe,uBAAuB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,OAAO,QAAQ,WAAW,CAAC;AAC3B,OAAO,QAAQ,OAAO,CAAC;AAwBvB,eAAO,MAAM,uBAAuB;;;sBAOV,MAAM,MAAM,MAAM;CAoE3C,CAAC;AAEF,eAAe,uBAAuB,CAAC"}
package/dist/index.js CHANGED
@@ -1,28 +1,22 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.vitePluginGraphqlLoader = void 0;
7
- const os_1 = __importDefault(require("os"));
8
- const graphql_tag_1 = __importDefault(require("graphql-tag"));
9
- const snippets_1 = require("./snippets");
1
+ import { EOL } from "os";
2
+ import { gql } from "graphql-tag";
3
+ import { ONE_QUERY, UNIQUE } from "./snippets.js";
10
4
  const expandImports = (source) => {
11
5
  const lines = source.split(/\r\n|\r|\n/);
12
- let outputCode = snippets_1.UNIQUE;
6
+ let outputCode = UNIQUE;
13
7
  lines.some((line) => {
14
8
  const result = line.match(/^#\s?import (.+)$/);
15
9
  if (result) {
16
10
  const [_, importFile] = result;
17
11
  const parseDocument = `(await import(${importFile})).default`;
18
12
  const appendDefinition = `doc.definitions = doc.definitions.concat(unique(${parseDocument}.definitions));`;
19
- outputCode += appendDefinition + os_1.default.EOL;
13
+ outputCode += appendDefinition + EOL;
20
14
  }
21
15
  return line.length > 0 && line[0] !== "#";
22
16
  });
23
17
  return outputCode;
24
18
  };
25
- const vitePluginGraphqlLoader = () => {
19
+ export const vitePluginGraphqlLoader = () => {
26
20
  const graphqlRegex = /\.(?:gql|graphql)$/;
27
21
  return {
28
22
  name: "graphql-loader",
@@ -31,11 +25,11 @@ const vitePluginGraphqlLoader = () => {
31
25
  if (!graphqlRegex.test(id)) {
32
26
  return;
33
27
  }
34
- const documentNode = (0, graphql_tag_1.default) `
28
+ const documentNode = gql `
35
29
  ${source}
36
30
  `;
37
31
  const headerCode = `
38
- var doc = ${JSON.stringify(documentNode)};
32
+ const doc = ${JSON.stringify(documentNode)};
39
33
  doc.loc.source = ${JSON.stringify(documentNode.loc.source)};
40
34
  `;
41
35
  let outputCode = "";
@@ -48,11 +42,11 @@ doc.loc.source = ${JSON.stringify(documentNode.loc.source)};
48
42
  }, 0);
49
43
  if (operationCount < 1) {
50
44
  outputCode += `
51
- module.exports = doc;
45
+ export default doc;
52
46
  `;
53
47
  }
54
48
  else {
55
- outputCode += snippets_1.ONE_QUERY;
49
+ outputCode += ONE_QUERY;
56
50
  for (const op of documentNode.definitions) {
57
51
  if (op.kind === "OperationDefinition" ||
58
52
  op.kind === "FragmentDefinition") {
@@ -66,22 +60,16 @@ doc.loc.source = ${JSON.stringify(documentNode.loc.source)};
66
60
  }
67
61
  const opName = op.name.value;
68
62
  outputCode += `
69
- module.exports["${opName}"] = oneQuery(doc, "${opName}");
63
+ export const ${opName} = oneQuery(doc, "${opName}");
70
64
  `;
71
65
  }
72
66
  }
73
67
  }
74
68
  const importOutputCode = expandImports(source);
75
- const allCode = headerCode +
76
- os_1.default.EOL +
77
- importOutputCode +
78
- os_1.default.EOL +
79
- outputCode +
80
- os_1.default.EOL;
69
+ const allCode = headerCode + EOL + importOutputCode + EOL + outputCode + EOL;
81
70
  return allCode;
82
71
  },
83
72
  };
84
73
  };
85
- exports.vitePluginGraphqlLoader = vitePluginGraphqlLoader;
86
- exports.default = exports.vitePluginGraphqlLoader;
74
+ export default vitePluginGraphqlLoader;
87
75
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;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GAAG,GAAG,CAAC;YAEjE,OAAO,OAAO,CAAC;QACnB,CAAC;KACJ,CAAC;AACN,CAAC,CAAC;AAEF,eAAe,uBAAuB,CAAC"}
@@ -1,3 +1,3 @@
1
- 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";
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 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";
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
@@ -1 +1 @@
1
- {"version":3,"file":"snippets.d.ts","sourceRoot":"","sources":["../src/snippets.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,MAAM,qUAgBlB,CAAC;AAEF,eAAO,MAAM,SAAS,2/EAsFrB,CAAC"}
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
@@ -1,13 +1,10 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ONE_QUERY = exports.UNIQUE = void 0;
4
- exports.UNIQUE = `
5
- var names = {};
1
+ export const UNIQUE = `
2
+ const names = {};
6
3
  function unique(defs) {
7
4
  return defs.filter(
8
5
  function(def) {
9
6
  if (def.kind !== 'FragmentDefinition') return true;
10
- var name = def.name.value
7
+ const name = def.name.value
11
8
  if (names[name]) {
12
9
  return false;
13
10
  } else {
@@ -18,13 +15,13 @@ function unique(defs) {
18
15
  )
19
16
  }
20
17
  `;
21
- exports.ONE_QUERY = `
18
+ export const ONE_QUERY = `
22
19
  // Collect any fragment/type references from a node, adding them to the refs Set
23
20
  function collectFragmentReferences(node, refs) {
24
21
  if (node.kind === "FragmentSpread") {
25
22
  refs.add(node.name.value);
26
23
  } else if (node.kind === "VariableDefinition") {
27
- var type = node.type;
24
+ const type = node.type;
28
25
  if (type.kind === "NamedType") {
29
26
  refs.add(type.name.value);
30
27
  }
@@ -45,19 +42,19 @@ function collectFragmentReferences(node, refs) {
45
42
  });
46
43
  }
47
44
  }
48
- var definitionRefs = {};
45
+ const definitionRefs = {};
49
46
  (function extractReferences() {
50
47
  doc.definitions.forEach(function(def) {
51
48
  if (def.name) {
52
- var refs = new Set();
49
+ const refs = new Set();
53
50
  collectFragmentReferences(def, refs);
54
51
  definitionRefs[def.name.value] = refs;
55
52
  }
56
53
  });
57
54
  })();
58
55
  function findOperation(doc, name) {
59
- for (var i = 0; i < doc.definitions.length; i++) {
60
- var element = doc.definitions[i];
56
+ for (let i = 0; i < doc.definitions.length; i++) {
57
+ const element = doc.definitions[i];
61
58
  if (element.name && element.name.value == name) {
62
59
  return element;
63
60
  }
@@ -65,7 +62,7 @@ function findOperation(doc, name) {
65
62
  }
66
63
  function oneQuery(doc, operationName) {
67
64
  // Copy the DocumentNode, but clear out the definitions
68
- var newDoc = {
65
+ const newDoc = {
69
66
  kind: doc.kind,
70
67
  definitions: [findOperation(doc, operationName)]
71
68
  };
@@ -74,20 +71,20 @@ function oneQuery(doc, operationName) {
74
71
  }
75
72
  // Now, for the operation we're running, find any fragments referenced by
76
73
  // it or the fragments it references
77
- var opRefs = definitionRefs[operationName] || new Set();
78
- var allRefs = new Set();
79
- var newRefs = new Set();
74
+ const opRefs = definitionRefs[operationName] || new Set();
75
+ const allRefs = new Set();
76
+ let newRefs = new Set();
80
77
  // IE 11 doesn't support "new Set(iterable)", so we add the members of opRefs to newRefs one by one
81
78
  opRefs.forEach(function(refName) {
82
79
  newRefs.add(refName);
83
80
  });
84
81
  while (newRefs.size > 0) {
85
- var prevRefs = newRefs;
82
+ const prevRefs = newRefs;
86
83
  newRefs = new Set();
87
84
  prevRefs.forEach(function(refName) {
88
85
  if (!allRefs.has(refName)) {
89
86
  allRefs.add(refName);
90
- var childRefs = definitionRefs[refName] || new Set();
87
+ const childRefs = definitionRefs[refName] || new Set();
91
88
  childRefs.forEach(function(childRef) {
92
89
  newRefs.add(childRef);
93
90
  });
@@ -95,7 +92,7 @@ function oneQuery(doc, operationName) {
95
92
  });
96
93
  }
97
94
  allRefs.forEach(function(refName) {
98
- var op = findOperation(doc, refName);
95
+ const op = findOperation(doc, refName);
99
96
  if (op) {
100
97
  newDoc.definitions.push(op);
101
98
  }
@@ -103,6 +100,6 @@ function oneQuery(doc, operationName) {
103
100
  return newDoc;
104
101
  }
105
102
 
106
- module.exports = doc;
103
+ export default doc;
107
104
  `;
108
105
  //# sourceMappingURL=snippets.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"snippets.js","sourceRoot":"","sources":["../src/snippets.ts"],"names":[],"mappings":";;;AAAa,QAAA,MAAM,GAAG;;;;;;;;;;;;;;;;CAgBrB,CAAC;AAEW,QAAA,SAAS,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAsFxB,CAAC"}
1
+ {"version":3,"file":"snippets.js","sourceRoot":"","sources":["../src/snippets.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,MAAM,GAAG;;;;;;;;;;;;;;;;CAgBrB,CAAC;AAEF,MAAM,CAAC,MAAM,SAAS,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAsFxB,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "vite-plugin-graphql-loader",
3
- "version": "1.0.5",
3
+ "version": "3.0.0",
4
4
  "description": "A Vite plugin for loading GraphQL files.",
5
+ "type": "module",
5
6
  "main": "dist/index.js",
6
7
  "types": "dist/index.d.ts",
7
8
  "files": [
@@ -9,10 +10,11 @@
9
10
  "package.json"
10
11
  ],
11
12
  "scripts": {
12
- "test": "ts-node __test__/index.ts",
13
- "build": "rm -rf dist && tsc",
13
+ "test": "vitest",
14
+ "build": "rimraf ./dist && tsc",
14
15
  "package:bump": "yarn version --patch",
15
- "package:publish": "yarn build && yarn package:bump && yarn publish"
16
+ "package:publish": "yarn build && yarn package:bump && yarn publish",
17
+ "lint": "yarn prettier -w src tests/**/*.ts && yarn eslint --fix"
16
18
  },
17
19
  "repository": {
18
20
  "type": "git",
@@ -31,15 +33,20 @@
31
33
  },
32
34
  "homepage": "https://github.com/noiach/vite-plugin-graphql-loader#readme",
33
35
  "dependencies": {
34
- "graphql": "^16.6.0",
36
+ "graphql": "^16.8.1",
35
37
  "graphql-tag": "^2.12.6"
36
38
  },
37
39
  "devDependencies": {
38
- "@types/glob": "^8.0.1",
39
- "@types/node": "^16",
40
- "glob": "^8.1.0",
41
- "ts-node": "^10.9.1",
42
- "typescript": "^4.9.5",
43
- "vite": "^4.1.1"
40
+ "@types/node": "^20",
41
+ "eslint": "^8.56.0",
42
+ "glob": "^10.3.10",
43
+ "prettier": "^3.2.4",
44
+ "rimraf": "^5.0.5",
45
+ "typescript": "^5.3.3",
46
+ "vite": "^5.0.12",
47
+ "vitest": "^1.2.1"
48
+ },
49
+ "prettier": {
50
+ "tabWidth": 4
44
51
  }
45
52
  }