vite-plugin-graphql-loader 3.0.1 → 4.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 +24 -3
- package/dist/index.d.ts +8 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +66 -38
- package/dist/index.js.map +1 -1
- package/dist/snippets.d.ts +5 -2
- package/dist/snippets.d.ts.map +1 -1
- package/dist/snippets.js +50 -67
- package/dist/snippets.js.map +1 -1
- package/package.json +12 -9
package/README.md
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
# vite-plugin-graphql-loader
|
|
2
2
|
|
|
3
|
-
[](https://
|
|
3
|
+
[](https://www.npmjs.com/package/vite-plugin-graphql-loader/)
|
|
4
4
|
|
|
5
|
-
[](https://www.npmjs.com/package/bun-graphql-loader)
|
|
6
|
+

|
|
7
|
+

|
|
8
|
+

|
|
9
|
+

|
|
6
10
|
|
|
7
11
|
A Vite plugin for loading GraphQL .gql and .graphql files, based on [graphql-tag/loader](https://github.com/apollographql/graphql-tag)
|
|
8
12
|
|
|
@@ -91,8 +95,25 @@ And then import fragments and queries like so in order to type them as `Document
|
|
|
91
95
|
|
|
92
96
|
```typescript
|
|
93
97
|
import Document, { _queries, _fragments } from "./example.graphql";
|
|
98
|
+
console.log(Document); // Has type `DocumentNode`
|
|
99
|
+
console.log(_queries.ExampleQuery); // Has type `DocumentNode`
|
|
100
|
+
console.log(_fragments.ExampleFragment); // Has type `FragmentDefinitionNode`
|
|
94
101
|
```
|
|
95
102
|
|
|
96
103
|
## Changelog
|
|
97
104
|
|
|
98
|
-
**
|
|
105
|
+
**_v4.0.0_**:
|
|
106
|
+
|
|
107
|
+
- Added source-map generation. Can by disabled by initializing with `graphqlLoader({noSourceMap: true})`.
|
|
108
|
+
- Refactored code generation to be more maintainable, added more test cases.
|
|
109
|
+
- Migrated from `yarn` to `bun`.
|
|
110
|
+
|
|
111
|
+
**_v3.0.1_**:
|
|
112
|
+
|
|
113
|
+
- Switched `await import` statements to top-level `import` statements (fixes #5 - `Top-level await is not available` error).
|
|
114
|
+
- Added `_queries` and `_fragments` for improved module declaration types.
|
|
115
|
+
- Updated snippets to be defined in TypeScript and then stringified.
|
|
116
|
+
|
|
117
|
+
**_v3.0.0_**:
|
|
118
|
+
|
|
119
|
+
- [Moved from CJS to ESM](https://github.com/0x31/vite-plugin-graphql-loader/commit/0e0b37cfcb0ecbdf28e985aeca3454137b4b73e3), inline with Vite 5.0's CJS deprecation. If you are using CommonJS, continue using v2.0 of this package. If you have `"type": "module"`, in your `package.json` then it should work as expected.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
|
-
|
|
1
|
+
import { SourceMap } from "magic-string";
|
|
2
|
+
export declare const vitePluginGraphqlLoader: (options?: {
|
|
3
|
+
noSourceMap?: boolean;
|
|
4
|
+
}) => {
|
|
2
5
|
name: string;
|
|
3
6
|
enforce: "pre";
|
|
4
|
-
transform(source: string, id: string):
|
|
7
|
+
transform(source: string, id: string): {
|
|
8
|
+
code: string;
|
|
9
|
+
map: SourceMap;
|
|
10
|
+
};
|
|
5
11
|
};
|
|
6
12
|
export default vitePluginGraphqlLoader;
|
|
7
13
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAoB,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAkDtD,eAAO,MAAM,uBAAuB,aAAc;IAC9C,WAAW,CAAC,EAAE,OAAO,CAAC;CACzB;;;sBAUyB,MAAM,MAAM,MAAM;;;;CAgI3C,CAAC;AAEF,eAAe,uBAAuB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,28 +1,30 @@
|
|
|
1
1
|
import { EOL } from "os";
|
|
2
2
|
import { gql } from "graphql-tag";
|
|
3
|
-
import
|
|
3
|
+
import MagicString from "magic-string";
|
|
4
|
+
import { vitePluginGraphqlLoaderUniqueChecker, vitePluginGraphqlLoaderExtractQuery, } from "./snippets.js";
|
|
5
|
+
const DOC_NAME = "_gql_doc";
|
|
4
6
|
const expandImports = (source) => {
|
|
5
7
|
const lines = source.split(/\r\n|\r|\n/);
|
|
6
|
-
|
|
8
|
+
const importNames = new Set();
|
|
9
|
+
const imports = [];
|
|
10
|
+
const importAppends = [];
|
|
7
11
|
lines.some((line) => {
|
|
8
12
|
const result = line.match(/^#\s?import (.+)$/);
|
|
9
13
|
if (result) {
|
|
10
14
|
const [_, importFile] = result;
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
appendDefinition +
|
|
19
|
-
EOL;
|
|
15
|
+
let importName = "Import_" + importFile.replace(/[^a-z0-9]/gi, "_");
|
|
16
|
+
while (importNames.has(importName)) {
|
|
17
|
+
importName = importName + "_";
|
|
18
|
+
}
|
|
19
|
+
importNames.add(importName);
|
|
20
|
+
imports.push(`import ${importName} from ${importFile};\n`);
|
|
21
|
+
importAppends.push(`${DOC_NAME}.definitions = ${DOC_NAME}.definitions.concat(${vitePluginGraphqlLoaderUniqueChecker.name}(${importName}.definitions));\n`);
|
|
20
22
|
}
|
|
21
|
-
return line.length
|
|
23
|
+
return line.length !== 0 && line[0] !== "#";
|
|
22
24
|
});
|
|
23
|
-
return
|
|
25
|
+
return { imports, importAppends };
|
|
24
26
|
};
|
|
25
|
-
export const vitePluginGraphqlLoader = () => {
|
|
27
|
+
export const vitePluginGraphqlLoader = (options) => {
|
|
26
28
|
const graphqlRegex = /\.(?:gql|graphql)$/;
|
|
27
29
|
return {
|
|
28
30
|
name: "graphql-loader",
|
|
@@ -31,24 +33,46 @@ export const vitePluginGraphqlLoader = () => {
|
|
|
31
33
|
if (!graphqlRegex.test(id)) {
|
|
32
34
|
return;
|
|
33
35
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
36
|
+
let documentNode;
|
|
37
|
+
try {
|
|
38
|
+
documentNode = gql `
|
|
39
|
+
${source}
|
|
40
|
+
`;
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
throw new Error(String(error));
|
|
44
|
+
}
|
|
45
|
+
let outputCode = new MagicString(source).replaceAll("`", "\\`");
|
|
46
|
+
outputCode.prepend(`const _gql_source = \``);
|
|
47
|
+
outputCode.append(`\`;\n`);
|
|
48
|
+
const plainDocument = Object.assign({}, documentNode);
|
|
49
|
+
Object.assign(plainDocument.loc, documentNode.loc);
|
|
50
|
+
const documentObject = JSON.parse(JSON.stringify(documentNode));
|
|
51
|
+
const sourceBodyIdentifier = `_gql_uuid_${new Date().getTime()}`;
|
|
52
|
+
if (documentNode.loc && documentNode.loc.source) {
|
|
53
|
+
documentObject.loc.source = {
|
|
54
|
+
name: documentNode.loc.source.name,
|
|
55
|
+
locationOffset: documentNode.loc.source.locationOffset,
|
|
56
|
+
body: sourceBodyIdentifier,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
outputCode.append(`const ${DOC_NAME} = ${JSON.stringify(documentObject).replace(`"${sourceBodyIdentifier}"`, "_gql_source")};\n`);
|
|
60
|
+
const { imports, importAppends } = expandImports(source);
|
|
61
|
+
if (imports.length) {
|
|
62
|
+
outputCode.prepend(imports.join(""));
|
|
63
|
+
outputCode.append(`const ${vitePluginGraphqlLoaderUniqueChecker.name} = ${vitePluginGraphqlLoaderUniqueChecker.toString()};\n`);
|
|
64
|
+
outputCode.append(importAppends.join(""));
|
|
65
|
+
}
|
|
42
66
|
const operationCount = documentNode.definitions.filter((op) => (op.kind === "OperationDefinition" ||
|
|
43
67
|
op.kind === "FragmentDefinition") &&
|
|
44
68
|
op.name).length;
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
69
|
+
const queryNames = [];
|
|
70
|
+
const fragmentNames = [];
|
|
71
|
+
if (operationCount >= 1) {
|
|
72
|
+
const extractQueries = operationCount > 1 || imports.length > 0;
|
|
73
|
+
if (extractQueries) {
|
|
74
|
+
outputCode.append(`const ${vitePluginGraphqlLoaderExtractQuery.name} = ${vitePluginGraphqlLoaderExtractQuery.toString()};\n`);
|
|
75
|
+
}
|
|
52
76
|
for (const op of documentNode.definitions) {
|
|
53
77
|
if (op.kind === "OperationDefinition" ||
|
|
54
78
|
op.kind === "FragmentDefinition") {
|
|
@@ -61,22 +85,26 @@ export default doc;
|
|
|
61
85
|
}
|
|
62
86
|
}
|
|
63
87
|
const opName = op.name.value;
|
|
64
|
-
outputCode
|
|
65
|
-
export const ${opName} = oneQuery(doc, "${opName}");`;
|
|
88
|
+
outputCode.append(`export const ${opName} = ${extractQueries ? `${vitePluginGraphqlLoaderExtractQuery.name}(${DOC_NAME}, "${opName}")` : DOC_NAME};\n`);
|
|
66
89
|
if (op.kind === "OperationDefinition") {
|
|
67
|
-
|
|
68
|
-
_queries["${opName}"] = ${opName};`;
|
|
90
|
+
queryNames.push(opName);
|
|
69
91
|
}
|
|
70
92
|
else {
|
|
71
|
-
|
|
72
|
-
_fragments["${opName}"] = ${opName};`;
|
|
93
|
+
fragmentNames.push(opName);
|
|
73
94
|
}
|
|
74
95
|
}
|
|
75
96
|
}
|
|
76
97
|
}
|
|
77
|
-
const
|
|
78
|
-
const
|
|
79
|
-
|
|
98
|
+
outputCode.append(`export const _queries = {${queryNames.join(",")}};\n`);
|
|
99
|
+
outputCode.append(`export const _fragments = {${fragmentNames.join(",")}};\n`);
|
|
100
|
+
outputCode.append(`export default ${DOC_NAME};\n`);
|
|
101
|
+
outputCode.replaceAll("\n", EOL);
|
|
102
|
+
return {
|
|
103
|
+
code: outputCode.toString(),
|
|
104
|
+
map: options?.noSourceMap
|
|
105
|
+
? { mappings: "" }
|
|
106
|
+
: outputCode.generateMap(),
|
|
107
|
+
};
|
|
80
108
|
},
|
|
81
109
|
};
|
|
82
110
|
};
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,IAAI,CAAC;AACzB,OAAO,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAClC,OAAO,EACH,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,IAAI,CAAC;AACzB,OAAO,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAClC,OAAO,WAA0B,MAAM,cAAc,CAAC;AACtD,OAAO,EACH,oCAAoC,EACpC,mCAAmC,GACtC,MAAM,eAAe,CAAC;AAGvB,MAAM,QAAQ,GAAG,UAAU,CAAC;AAG5B,MAAM,aAAa,GAAG,CAClB,MAAc,EACgC,EAAE;IAChD,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;IAEzC,MAAM,WAAW,GAAG,IAAI,GAAG,EAAU,CAAC;IACtC,MAAM,OAAO,GAAG,EAAE,CAAC;IACnB,MAAM,aAAa,GAAG,EAAE,CAAC;IAIzB,KAAK,CAAC,IAAI,CAAC,CAAC,IAAY,EAAE,EAAE;QACxB,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,mBAAmB,CAAC,CAAC;QAG/C,IAAI,MAAM,EAAE,CAAC;YACT,MAAM,CAAC,CAAC,EAAE,UAAU,CAAC,GAAG,MAAM,CAAC;YAG/B,IAAI,UAAU,GAAG,SAAS,GAAG,UAAU,CAAC,OAAO,CAAC,aAAa,EAAE,GAAG,CAAC,CAAC;YAEpE,OAAO,WAA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,IAAI,CAAC,MAAM,CAAC,CAAC;wBAC5B,CAAC;6BAAM,CAAC;4BACJ,aAAa,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;wBAC/B,CAAC;oBACL,CAAC;gBACL,CAAC;YACL,CAAC;YAED,UAAU,CAAC,MAAM,CACb,4BAA4B,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,CACzD,CAAC;YACF,UAAU,CAAC,MAAM,CACb,8BAA8B,aAAa,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,CAC9D,CAAC;YAEF,UAAU,CAAC,MAAM,CAAC,kBAAkB,QAAQ,KAAK,CAAC,CAAC;YAEnD,UAAU,CAAC,UAAU,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;YAEjC,OAAO;gBACH,IAAI,EAAE,UAAU,CAAC,QAAQ,EAAE;gBAC3B,GAAG,EAAE,OAAO,EAAE,WAAW;oBACrB,CAAC,CAAE,EAAE,QAAQ,EAAE,EAAE,EAAgB;oBACjC,CAAC,CAAC,UAAU,CAAC,WAAW,EAAE;aACjC,CAAC;QACN,CAAC;KACJ,CAAC;AACN,CAAC,CAAC;AAEF,eAAe,uBAAuB,CAAC"}
|
package/dist/snippets.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
export declare const
|
|
1
|
+
import type { DefinitionNode, DocumentNode } from "graphql";
|
|
2
|
+
export declare const vitePluginGraphqlLoaderUniqueChecker: (defs: DefinitionNode[]) => DefinitionNode[];
|
|
3
|
+
export declare const vitePluginGraphqlLoaderExtractQuery: (doc: DocumentNode, operationName: string) => DocumentNode & {
|
|
4
|
+
definitions: (import("graphql").OperationDefinitionNode | import("graphql").FragmentDefinitionNode | import("graphql").ScalarTypeDefinitionNode | import("graphql").ObjectTypeDefinitionNode | import("graphql").InterfaceTypeDefinitionNode | import("graphql").UnionTypeDefinitionNode | import("graphql").EnumTypeDefinitionNode | import("graphql").InputObjectTypeDefinitionNode | import("graphql").DirectiveDefinitionNode | import("graphql").ScalarTypeExtensionNode | import("graphql").ObjectTypeExtensionNode | import("graphql").InterfaceTypeExtensionNode | import("graphql").UnionTypeExtensionNode | import("graphql").EnumTypeExtensionNode | import("graphql").InputObjectTypeExtensionNode)[];
|
|
5
|
+
};
|
|
3
6
|
//# 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":"
|
|
1
|
+
{"version":3,"file":"snippets.d.ts","sourceRoot":"","sources":["../src/snippets.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAW,cAAc,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAMrE,eAAO,MAAM,oCAAoC,SACvC,cAAc,EAAE,qBAazB,CAAC;AAEF,eAAO,MAAM,mCAAmC,QACvC,YAAY,iBACF,MAAM;;CAoGxB,CAAC"}
|
package/dist/snippets.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
const
|
|
2
|
-
const
|
|
1
|
+
export const vitePluginGraphqlLoaderUniqueChecker = (defs) => {
|
|
2
|
+
const names = {};
|
|
3
3
|
return defs.filter(function (def) {
|
|
4
4
|
if (def.kind !== "FragmentDefinition")
|
|
5
5
|
return true;
|
|
@@ -13,62 +13,58 @@ const unique = (defs) => {
|
|
|
13
13
|
}
|
|
14
14
|
});
|
|
15
15
|
};
|
|
16
|
-
export const
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
const collectFragmentReferences = (node, refs) => {
|
|
21
|
-
if (node.kind === "FragmentSpread") {
|
|
22
|
-
refs.add(node.name.value);
|
|
23
|
-
}
|
|
24
|
-
else if (node.kind === "VariableDefinition") {
|
|
25
|
-
const type = node.type;
|
|
26
|
-
if (type.kind === "NamedType") {
|
|
27
|
-
refs.add(type.name.value);
|
|
16
|
+
export const vitePluginGraphqlLoaderExtractQuery = (doc, operationName) => {
|
|
17
|
+
const collectFragmentReferences = (node, refs) => {
|
|
18
|
+
if (node.kind === "FragmentSpread") {
|
|
19
|
+
refs.add(node.name.value);
|
|
28
20
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
}
|
|
35
|
-
if (node.variableDefinitions) {
|
|
36
|
-
node.variableDefinitions.forEach(function (def) {
|
|
37
|
-
collectFragmentReferences(def, refs);
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
if (node.definitions) {
|
|
41
|
-
node.definitions.forEach(function (def) {
|
|
42
|
-
collectFragmentReferences(def, refs);
|
|
43
|
-
});
|
|
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;
|
|
21
|
+
else if (node.kind === "VariableDefinition") {
|
|
22
|
+
const type = node.type;
|
|
23
|
+
if (type.kind === "NamedType") {
|
|
24
|
+
refs.add(type.name.value);
|
|
25
|
+
}
|
|
53
26
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
const element = doc.definitions[i];
|
|
59
|
-
if (element.name && element.name.value == name) {
|
|
60
|
-
return element;
|
|
27
|
+
if (node && "selectionSet" in node && node.selectionSet) {
|
|
28
|
+
node.selectionSet.selections.forEach((selection) => {
|
|
29
|
+
collectFragmentReferences(selection, refs);
|
|
30
|
+
});
|
|
61
31
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
32
|
+
if (node && "variableDefinitions" in node && node.variableDefinitions) {
|
|
33
|
+
node.variableDefinitions.forEach((def) => {
|
|
34
|
+
collectFragmentReferences(def, refs);
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
if (node && "definitions" in node && node.definitions) {
|
|
38
|
+
node.definitions.forEach((def) => {
|
|
39
|
+
collectFragmentReferences(def, refs);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
return refs;
|
|
68
43
|
};
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
44
|
+
const extractReferences = (doc) => {
|
|
45
|
+
const definitionRefs = {};
|
|
46
|
+
doc.definitions.forEach(function (def) {
|
|
47
|
+
if ("name" in def && def.name) {
|
|
48
|
+
definitionRefs[def.name.value] = collectFragmentReferences(def, new Set());
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
return definitionRefs;
|
|
52
|
+
};
|
|
53
|
+
const findOperation = (doc, name) => {
|
|
54
|
+
for (let i = 0; i < doc.definitions.length; i++) {
|
|
55
|
+
const element = doc.definitions[i];
|
|
56
|
+
if (element &&
|
|
57
|
+
"name" in element &&
|
|
58
|
+
element.name &&
|
|
59
|
+
element.name.value == name) {
|
|
60
|
+
return element;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
const definitionRefs = extractReferences(doc);
|
|
65
|
+
const newDoc = Object.assign({}, doc, {
|
|
66
|
+
definitions: [findOperation(doc, operationName)],
|
|
67
|
+
});
|
|
72
68
|
const opRefs = definitionRefs[operationName] || new Set();
|
|
73
69
|
const allRefs = new Set();
|
|
74
70
|
let newRefs = new Set();
|
|
@@ -96,17 +92,4 @@ const oneQuery = (doc, operationName) => {
|
|
|
96
92
|
});
|
|
97
93
|
return newDoc;
|
|
98
94
|
};
|
|
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 = {};
|
|
109
|
-
|
|
110
|
-
export default doc;
|
|
111
|
-
`;
|
|
112
95
|
//# sourceMappingURL=snippets.js.map
|
package/dist/snippets.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"snippets.js","sourceRoot":"","sources":["../src/snippets.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"snippets.js","sourceRoot":"","sources":["../src/snippets.ts"],"names":[],"mappings":"AAcA,MAAM,CAAC,MAAM,oCAAoC,GAAG,CAChD,IAAsB,EACxB,EAAE;IACA,MAAM,KAAK,GAAG,EAAE,CAAC;IACjB,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,mCAAmC,GAAG,CAC/C,GAAiB,EACjB,aAAqB,EACvB,EAAE;IAEA,MAAM,yBAAyB,GAAG,CAAC,IAAa,EAAE,IAAiB,EAAE,EAAE;QACnE,IAAI,IAAI,CAAC,IAAI,KAAK,gBAAgB,EAAE,CAAC;YACjC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAC9B,CAAC;aAAM,IAAI,IAAI,CAAC,IAAI,KAAK,oBAAoB,EAAE,CAAC;YAC5C,MAAM,IAAI,GAAG,IAAI,CAAC,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;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;IAED,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;IAEH,OAAO,MAAM,CAAC;AAClB,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-graphql-loader",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.1",
|
|
4
4
|
"description": "A Vite plugin for loading GraphQL files.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -13,12 +13,12 @@
|
|
|
13
13
|
"test": "vitest",
|
|
14
14
|
"build": "rimraf ./dist && tsc",
|
|
15
15
|
"package:bump": "yarn version --patch",
|
|
16
|
-
"package:publish": "
|
|
17
|
-
"lint": "
|
|
16
|
+
"package:publish": "bun run build && bun run package:bump && npm publish",
|
|
17
|
+
"lint": "prettier -w src tests/**/*.ts && eslint --fix"
|
|
18
18
|
},
|
|
19
19
|
"repository": {
|
|
20
20
|
"type": "git",
|
|
21
|
-
"url": "git+https://github.com/
|
|
21
|
+
"url": "git+https://github.com/0x31/vite-plugin-graphql-loader.git"
|
|
22
22
|
},
|
|
23
23
|
"keywords": [
|
|
24
24
|
"vite",
|
|
@@ -26,20 +26,23 @@
|
|
|
26
26
|
"graphql",
|
|
27
27
|
"graphql-tag"
|
|
28
28
|
],
|
|
29
|
-
"author": "
|
|
29
|
+
"author": "0x31",
|
|
30
30
|
"license": "ISC",
|
|
31
31
|
"bugs": {
|
|
32
|
-
"url": "https://github.com/
|
|
32
|
+
"url": "https://github.com/0x31/vite-plugin-graphql-loader/issues"
|
|
33
33
|
},
|
|
34
|
-
"homepage": "https://github.com/
|
|
34
|
+
"homepage": "https://github.com/0x31/vite-plugin-graphql-loader#readme",
|
|
35
35
|
"dependencies": {
|
|
36
36
|
"graphql": "^16.8.1",
|
|
37
|
-
"graphql-tag": "^2.12.6"
|
|
37
|
+
"graphql-tag": "^2.12.6",
|
|
38
|
+
"magic-string": "^0.30.10"
|
|
38
39
|
},
|
|
39
40
|
"devDependencies": {
|
|
41
|
+
"@babel/parser": "^7.24.8",
|
|
42
|
+
"@babel/traverse": "^7.24.8",
|
|
43
|
+
"@types/babel__traverse": "^7.20.6",
|
|
40
44
|
"@types/node": "^20",
|
|
41
45
|
"eslint": "^8.56.0",
|
|
42
|
-
"glob": "^10.3.10",
|
|
43
46
|
"prettier": "^3.2.4",
|
|
44
47
|
"rimraf": "^5.0.5",
|
|
45
48
|
"typescript": "^5.3.3",
|