vite-plugin-graphql-loader 2.0.0 → 3.0.1

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
+ This package _doesn't_ generate TypeScript definitions from the queries and fragments - see [vite-plugin-graphql-codegen](https://www.npmjs.com/package/vite-plugin-graphql-codegen) if you require this.
10
+
9
11
  ## Install
10
12
 
11
13
  ```bash
@@ -54,16 +56,43 @@ query ExampleQuery {
54
56
  import ExampleQuery, { ExampleFragment } from "./example.graphql";
55
57
  ```
56
58
 
57
- If you are using TypeScript, you will have to declare `.gql` or `.graphql` files:
59
+ If you have multiple queries in the same file, import them like this:
60
+
61
+ ```javascript
62
+ import { FirstQuery, SecondQuery } from "./example.graphql";
63
+ ```
64
+
65
+ ## TypeScript
66
+
67
+ If you are using TypeScript, you will have to declare `.gql` or `.graphql` files.
58
68
 
59
- `graphql.d.ts`:
69
+ Create `graphql.d.ts` anywhere in your source directory and
60
70
 
61
71
  ```typescript
62
72
  declare module "*.gql";
73
+ declare module "*.graphql";
74
+ ```
75
+
76
+ **_Alternatively_**, change it to this (replacing .gql with .graphql depending on what you use):
63
77
 
64
- // Or if you aren't using fragments:
65
- // declare module "*.gql" {
66
- // const Query: import("graphql").DocumentNode;
67
- // export default Query;
68
- // }
78
+ ```typescript
79
+ declare module "*.gql" {
80
+ const Query: import("graphql").DocumentNode;
81
+ export default Query;
82
+ export const _queries: Record<string, import("graphql").DocumentNode>;
83
+ export const _fragments: Record<
84
+ string,
85
+ import("graphql").FragmentDefinitionNode
86
+ >;
87
+ }
88
+ ```
89
+
90
+ And then import fragments and queries like so in order to type them as `DocumentNode` and `FragmentDefinitionNode` objects.
91
+
92
+ ```typescript
93
+ import Document, { _queries, _fragments } from "./example.graphql";
69
94
  ```
95
+
96
+ ## Changelog
97
+
98
+ **_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.
@@ -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":"AAoCA,eAAO,MAAM,uBAAuB;;;sBAOV,MAAM,MAAM,MAAM;CAqE3C,CAAC;AAEF,eAAe,uBAAuB,CAAC"}
package/dist/index.js CHANGED
@@ -1,28 +1,28 @@
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_FUNCTION_TEMPLATE, UNIQUE_FUNCTION_TEMPLATE, } 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;
13
7
  lines.some((line) => {
14
8
  const result = line.match(/^#\s?import (.+)$/);
15
9
  if (result) {
16
10
  const [_, importFile] = result;
17
- const parseDocument = `(await import(${importFile})).default`;
18
- const appendDefinition = `doc.definitions = doc.definitions.concat(unique(${parseDocument}.definitions));`;
19
- outputCode += appendDefinition + os_1.default.EOL;
11
+ const importName = "Import_" + importFile.replace(/[^a-z0-9]/gi, "_");
12
+ const importStatement = `import ${importName} from ${importFile};`;
13
+ const appendDefinition = `doc.definitions = doc.definitions.concat(unique(${importName}.definitions));`;
14
+ outputCode =
15
+ (outputCode ?? UNIQUE_FUNCTION_TEMPLATE) +
16
+ importStatement +
17
+ EOL +
18
+ appendDefinition +
19
+ EOL;
20
20
  }
21
21
  return line.length > 0 && line[0] !== "#";
22
22
  });
23
- return outputCode;
23
+ return outputCode ?? "";
24
24
  };
25
- const vitePluginGraphqlLoader = () => {
25
+ export const vitePluginGraphqlLoader = () => {
26
26
  const graphqlRegex = /\.(?:gql|graphql)$/;
27
27
  return {
28
28
  name: "graphql-loader",
@@ -31,7 +31,7 @@ const vitePluginGraphqlLoader = () => {
31
31
  if (!graphqlRegex.test(id)) {
32
32
  return;
33
33
  }
34
- const documentNode = (0, graphql_tag_1.default) `
34
+ const documentNode = gql `
35
35
  ${source}
36
36
  `;
37
37
  const headerCode = `
@@ -39,20 +39,16 @@ const doc = ${JSON.stringify(documentNode)};
39
39
  doc.loc.source = ${JSON.stringify(documentNode.loc.source)};
40
40
  `;
41
41
  let outputCode = "";
42
- const operationCount = documentNode.definitions.reduce((accum, op) => {
43
- if (op.kind === "OperationDefinition" ||
44
- op.kind === "FragmentDefinition") {
45
- return accum + 1;
46
- }
47
- return accum;
48
- }, 0);
42
+ const operationCount = documentNode.definitions.filter((op) => (op.kind === "OperationDefinition" ||
43
+ op.kind === "FragmentDefinition") &&
44
+ op.name).length;
49
45
  if (operationCount < 1) {
50
46
  outputCode += `
51
- export default doc;
47
+ export default doc;
52
48
  `;
53
49
  }
54
50
  else {
55
- outputCode += snippets_1.ONE_QUERY;
51
+ outputCode += ONE_QUERY_FUNCTION_TEMPLATE;
56
52
  for (const op of documentNode.definitions) {
57
53
  if (op.kind === "OperationDefinition" ||
58
54
  op.kind === "FragmentDefinition") {
@@ -66,22 +62,23 @@ doc.loc.source = ${JSON.stringify(documentNode.loc.source)};
66
62
  }
67
63
  const opName = op.name.value;
68
64
  outputCode += `
69
- export const ${opName} = oneQuery(doc, "${opName}");
70
- `;
65
+ export const ${opName} = oneQuery(doc, "${opName}");`;
66
+ if (op.kind === "OperationDefinition") {
67
+ outputCode += `
68
+ _queries["${opName}"] = ${opName};`;
69
+ }
70
+ else {
71
+ outputCode += `
72
+ _fragments["${opName}"] = ${opName};`;
73
+ }
71
74
  }
72
75
  }
73
76
  }
74
77
  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;
78
+ const allCode = headerCode + EOL + importOutputCode + EOL + outputCode + EOL;
81
79
  return allCode;
82
80
  },
83
81
  };
84
82
  };
85
- exports.vitePluginGraphqlLoader = vitePluginGraphqlLoader;
86
- exports.default = exports.vitePluginGraphqlLoader;
83
+ export default vitePluginGraphqlLoader;
87
84
  //# 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":";;;;;;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,CAAC;SAC3C;QACD,OAAO,IAAI,CAAC,MAAM,GAAG,CAAC,IAAI,IAAI,CA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AC;gBACJ,UAAU,IAAI,2BAA2B,CAAC;gBAE1C,KAAK,MAAM,EAAE,IAAI,YAAY,CAAC,WAAW,EAAE,CAAC;oBACxC,IACI,EAAE,CAAC,IAAI,KAAK,qBAAqB;wBACjC,EAAE,CAAC,IAAI,KAAK,oBAAoB,EAClC,CAAC;wBACC,IAAI,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC;4BACX,IAAI,cAAc,GAAG,CAAC,EAAE,CAAC;gCACrB,MAAM,IAAI,KAAK,CACX,4EAA4E,CAC/E,CAAC;4BACN,CAAC;iCAAM,CAAC;gCACJ,SAAS;4BACb,CAAC;wBACL,CAAC;wBAED,MAAM,MAAM,GAAG,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC;wBAC7B,UAAU,IAAI;eACvB,MAAM,qBAAqB,MAAM,KAAK,CAAC;wBAE9B,IAAI,EAAE,CAAC,IAAI,KAAK,qBAAqB,EAAE,CAAC;4BACpC,UAAU,IAAI;YAC9B,MAAM,QAAQ,MAAM,GAAG,CAAC;wBACZ,CAAC;6BAAM,CAAC;4BACJ,UAAU,IAAI;cAC5B,MAAM,QAAQ,MAAM,GAAG,CAAC;wBACd,CAAC;oBACL,CAAC;gBACL,CAAC;YACL,CAAC;YAED,MAAM,gBAAgB,GAAG,aAAa,CAAC,MAAM,CAAC,CAAC;YAC/C,MAAM,OAAO,GACT,UAAU,GAAG,GAAG,GAAG,gBAAgB,GAAG,GAAG,GAAG,UAAU,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 = "\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";
1
+ export declare const UNIQUE_FUNCTION_TEMPLATE: string;
2
+ export declare const ONE_QUERY_FUNCTION_TEMPLATE: string;
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,yUAgBlB,CAAC;AAEF,eAAO,MAAM,SAAS,6gFAsFrB,CAAC"}
1
+ {"version":3,"file":"snippets.d.ts","sourceRoot":"","sources":["../src/snippets.ts"],"names":[],"mappings":"AAcA,eAAO,MAAM,wBAAwB,QAGpC,CAAC;AAuFF,eAAO,MAAM,2BAA2B,QAYvC,CAAC"}
package/dist/snippets.js CHANGED
@@ -1,107 +1,111 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ONE_QUERY = exports.UNIQUE = void 0;
4
- exports.UNIQUE = `
5
1
  const names = {};
6
- function unique(defs) {
7
- return defs.filter(
8
- function(def) {
9
- if (def.kind !== 'FragmentDefinition') return true;
10
- const name = def.name.value
11
- if (names[name]) {
12
- return false;
13
- } else {
14
- names[name] = true;
15
- return true;
16
- }
17
- }
18
- )
19
- }
2
+ const unique = (defs) => {
3
+ return defs.filter(function (def) {
4
+ if (def.kind !== "FragmentDefinition")
5
+ return true;
6
+ const name = def.name.value;
7
+ if (names[name]) {
8
+ return false;
9
+ }
10
+ else {
11
+ names[name] = true;
12
+ return true;
13
+ }
14
+ });
15
+ };
16
+ export const UNIQUE_FUNCTION_TEMPLATE = `
17
+ const names = {};
18
+ const unique = ${unique.toString()};
20
19
  `;
21
- exports.ONE_QUERY = `
22
- // Collect any fragment/type references from a node, adding them to the refs Set
23
- function collectFragmentReferences(node, refs) {
24
- if (node.kind === "FragmentSpread") {
25
- refs.add(node.name.value);
26
- } else if (node.kind === "VariableDefinition") {
27
- const type = node.type;
28
- if (type.kind === "NamedType") {
29
- refs.add(type.name.value);
20
+ const collectFragmentReferences = (node, refs) => {
21
+ if (node.kind === "FragmentSpread") {
22
+ refs.add(node.name.value);
30
23
  }
31
- }
32
- if (node.selectionSet) {
33
- node.selectionSet.selections.forEach(function(selection) {
34
- collectFragmentReferences(selection, refs);
35
- });
36
- }
37
- if (node.variableDefinitions) {
38
- node.variableDefinitions.forEach(function(def) {
39
- collectFragmentReferences(def, refs);
40
- });
41
- }
42
- if (node.definitions) {
43
- node.definitions.forEach(function(def) {
44
- collectFragmentReferences(def, refs);
45
- });
46
- }
47
- }
48
- const definitionRefs = {};
49
- (function extractReferences() {
50
- doc.definitions.forEach(function(def) {
51
- if (def.name) {
52
- const refs = new Set();
53
- collectFragmentReferences(def, refs);
54
- definitionRefs[def.name.value] = refs;
24
+ else if (node.kind === "VariableDefinition") {
25
+ const type = node.type;
26
+ if (type.kind === "NamedType") {
27
+ refs.add(type.name.value);
28
+ }
55
29
  }
56
- });
57
- })();
58
- function findOperation(doc, name) {
59
- for (let i = 0; i < doc.definitions.length; i++) {
60
- const element = doc.definitions[i];
61
- if (element.name && element.name.value == name) {
62
- return element;
30
+ if (node.selectionSet) {
31
+ node.selectionSet.selections.forEach(function (selection) {
32
+ collectFragmentReferences(selection, refs);
33
+ });
34
+ }
35
+ if (node.variableDefinitions) {
36
+ node.variableDefinitions.forEach(function (def) {
37
+ collectFragmentReferences(def, refs);
38
+ });
63
39
  }
64
- }
65
- }
66
- function oneQuery(doc, operationName) {
67
- // Copy the DocumentNode, but clear out the definitions
68
- const newDoc = {
69
- kind: doc.kind,
70
- definitions: [findOperation(doc, operationName)]
71
- };
72
- if (doc.hasOwnProperty("loc")) {
73
- newDoc.loc = doc.loc;
74
- }
75
- // Now, for the operation we're running, find any fragments referenced by
76
- // it or the fragments it references
77
- const opRefs = definitionRefs[operationName] || new Set();
78
- const allRefs = new Set();
79
- let newRefs = new Set();
80
- // IE 11 doesn't support "new Set(iterable)", so we add the members of opRefs to newRefs one by one
81
- opRefs.forEach(function(refName) {
82
- newRefs.add(refName);
83
- });
84
- while (newRefs.size > 0) {
85
- const prevRefs = newRefs;
86
- newRefs = new Set();
87
- prevRefs.forEach(function(refName) {
88
- if (!allRefs.has(refName)) {
89
- allRefs.add(refName);
90
- const childRefs = definitionRefs[refName] || new Set();
91
- childRefs.forEach(function(childRef) {
92
- newRefs.add(childRef);
40
+ if (node.definitions) {
41
+ node.definitions.forEach(function (def) {
42
+ collectFragmentReferences(def, refs);
93
43
  });
94
- }
44
+ }
45
+ };
46
+ const definitionRefs = {};
47
+ const extractReferences = (doc) => {
48
+ doc.definitions.forEach(function (def) {
49
+ if (def.name) {
50
+ const refs = new Set();
51
+ collectFragmentReferences(def, refs);
52
+ definitionRefs[def.name.value] = refs;
53
+ }
95
54
  });
96
- }
97
- allRefs.forEach(function(refName) {
98
- const op = findOperation(doc, refName);
99
- if (op) {
100
- newDoc.definitions.push(op);
55
+ };
56
+ const findOperation = (doc, name) => {
57
+ for (let i = 0; i < doc.definitions.length; i++) {
58
+ const element = doc.definitions[i];
59
+ if (element.name && element.name.value == name) {
60
+ return element;
61
+ }
62
+ }
63
+ };
64
+ const oneQuery = (doc, operationName) => {
65
+ const newDoc = {
66
+ kind: doc.kind,
67
+ definitions: [findOperation(doc, operationName)],
68
+ };
69
+ if (doc.hasOwnProperty("loc")) {
70
+ newDoc.loc = doc.loc;
101
71
  }
102
- });
103
- return newDoc;
104
- }
72
+ const opRefs = definitionRefs[operationName] || new Set();
73
+ const allRefs = new Set();
74
+ let newRefs = new Set();
75
+ opRefs.forEach((refName) => {
76
+ newRefs.add(refName);
77
+ });
78
+ while (newRefs.size > 0) {
79
+ const prevRefs = newRefs;
80
+ newRefs = new Set();
81
+ prevRefs.forEach((refName) => {
82
+ if (!allRefs.has(refName)) {
83
+ allRefs.add(refName);
84
+ const childRefs = definitionRefs[refName] || new Set();
85
+ childRefs.forEach((childRef) => {
86
+ newRefs.add(childRef);
87
+ });
88
+ }
89
+ });
90
+ }
91
+ allRefs.forEach((refName) => {
92
+ const op = findOperation(doc, refName);
93
+ if (op) {
94
+ newDoc.definitions.push(op);
95
+ }
96
+ });
97
+ return newDoc;
98
+ };
99
+ export const ONE_QUERY_FUNCTION_TEMPLATE = `
100
+ const collectFragmentReferences = ${collectFragmentReferences.toString()};
101
+ const definitionRefs = {};
102
+ const extractReferences = ${extractReferences.toString()};
103
+ extractReferences(doc);
104
+ const findOperation = ${findOperation.toString()};
105
+ const oneQuery = ${oneQuery.toString()};
106
+
107
+ export const _queries = {};
108
+ export const _fragments = {};
105
109
 
106
110
  export default doc;
107
111
  `;
@@ -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,KAAK,GAAG,EAAE,CAAC;AACjB,MAAM,MAAM,GAAG,CAAC,IAAI,EAAE,EAAE;IACpB,OAAO,IAAI,CAAC,MAAM,CAAC,UAAU,GAAG;QAC5B,IAAI,GAAG,CAAC,IAAI,KAAK,oBAAoB;YAAE,OAAO,IAAI,CAAC;QACnD,MAAM,IAAI,GAAG,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC;QAC5B,IAAI,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC;YACd,OAAO,KAAK,CAAC;QACjB,CAAC;aAAM,CAAC;YACJ,KAAK,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;YACnB,OAAO,IAAI,CAAC;QAChB,CAAC;IACL,CAAC,CAAC,CAAC;AACP,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,wBAAwB,GAAG;;iBAEvB,MAAM,CAAC,QAAQ,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AC;IACzB,CAAC,CAAC,CAAC;IACH,OAAO,OAAO,CAAC,IAAI,GAAG,CAAC,EAAE,CAAC;QACtB,MAAM,QAAQ,GAAG,OAAO,CAAC;QACzB,OAAO,GAAG,IAAI,GAAG,EAAE,CAAC;QACpB,QAAQ,CAAC,OAAO,CAAC,CAAC,OAAe,EAAE,EAAE;YACjC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,CAAC;gBACxB,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;gBACrB,MAAM,SAAS,GAAG,cAAc,CAAC,OAAO,CAAC,IAAI,IAAI,GAAG,EAAE,CAAC;gBACvD,SAAS,CAAC,OAAO,CAAC,CAAC,QAAgB,EAAE,EAAE;oBACnC,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;gBAC1B,CAAC,CAAC,CAAC;YACP,CAAC;QACL,CAAC,CAAC,CAAC;IACP,CAAC;IACD,OAAO,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;QACxB,MAAM,EAAE,GAAG,aAAa,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;QACvC,IAAI,EAAE,EAAE,CAAC;YACL,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QAChC,CAAC;IACL,CAAC,CAAC,CAAC;IACH,OAAO,MAAM,CAAC;AAClB,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,2BAA2B,GAAG;oCACP,yBAAyB,CAAC,QAAQ,EAAE;;4BAE5C,iBAAiB,CAAC,QAAQ,EAAE;;wBAEhC,aAAa,CAAC,QAAQ,EAAE;mBAC7B,QAAQ,CAAC,QAAQ,EAAE;;;;;;CAMrC,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "vite-plugin-graphql-loader",
3
- "version": "2.0.0",
3
+ "version": "3.0.1",
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": [
@@ -12,7 +13,8 @@
12
13
  "test": "vitest",
13
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,16 +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
- "rimraf": "^4.3.1",
42
- "typescript": "^4.9.5",
43
- "vite": "^4.1.1",
44
- "vitest": "^0.28.5"
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
45
51
  }
46
52
  }