vite-plugin-graphql-loader 1.0.0 → 1.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/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +87 -0
- package/dist/index.js.map +1 -0
- package/dist/snippets.d.ts +3 -0
- package/dist/snippets.d.ts.map +1 -0
- package/dist/snippets.js +108 -0
- package/dist/snippets.js.map +1 -0
- package/package.json +1 -1
package/dist/index.d.ts
ADDED
|
@@ -0,0 +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"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
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");
|
|
10
|
+
const expandImports = (source) => {
|
|
11
|
+
const lines = source.split(/\r\n|\r|\n/);
|
|
12
|
+
let outputCode = snippets_1.UNIQUE;
|
|
13
|
+
lines.some((line) => {
|
|
14
|
+
const result = line.match(/^#\s?import (.+)$/);
|
|
15
|
+
if (result) {
|
|
16
|
+
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;
|
|
20
|
+
}
|
|
21
|
+
return line.length > 0 && line[0] !== "#";
|
|
22
|
+
});
|
|
23
|
+
return outputCode;
|
|
24
|
+
};
|
|
25
|
+
const vitePluginGraphqlLoader = () => {
|
|
26
|
+
const graphqlRegex = /\.(?:gql|graphql)$/;
|
|
27
|
+
return {
|
|
28
|
+
name: "graphql-loader",
|
|
29
|
+
enforce: "pre",
|
|
30
|
+
transform(source, id) {
|
|
31
|
+
if (!graphqlRegex.test(id)) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
const documentNode = (0, graphql_tag_1.default) `
|
|
35
|
+
${source}
|
|
36
|
+
`;
|
|
37
|
+
const headerCode = `
|
|
38
|
+
var doc = ${JSON.stringify(documentNode)};
|
|
39
|
+
doc.loc.source = ${JSON.stringify(documentNode.loc.source)};
|
|
40
|
+
`;
|
|
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);
|
|
49
|
+
if (operationCount < 1) {
|
|
50
|
+
outputCode += `
|
|
51
|
+
module.exports = doc;
|
|
52
|
+
`;
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
outputCode += snippets_1.ONE_QUERY;
|
|
56
|
+
for (const op of documentNode.definitions) {
|
|
57
|
+
if (op.kind === "OperationDefinition" ||
|
|
58
|
+
op.kind === "FragmentDefinition") {
|
|
59
|
+
if (!op.name) {
|
|
60
|
+
if (operationCount > 1) {
|
|
61
|
+
throw new Error("Query/mutation names are required for a document with multiple definitions");
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
const opName = op.name.value;
|
|
68
|
+
outputCode += `
|
|
69
|
+
module.exports["${opName}"] = oneQuery(doc, "${opName}");
|
|
70
|
+
`;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
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;
|
|
81
|
+
return allCode;
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
exports.vitePluginGraphqlLoader = vitePluginGraphqlLoader;
|
|
86
|
+
exports.default = exports.vitePluginGraphqlLoader;
|
|
87
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +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,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC;IAC9C,CAAC,CAAC,CAAC;IAEH,OAAO,UAAU,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Z;yBACJ;wBAED,MAAM,MAAM,GAAG,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC;wBAC7B,UAAU,IAAI;kBACpB,MAAM,uBAAuB,MAAM;iBACpC,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"}
|
|
@@ -0,0 +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";
|
|
3
|
+
//# sourceMappingURL=snippets.d.ts.map
|
|
@@ -0,0 +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"}
|
package/dist/snippets.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ONE_QUERY = exports.UNIQUE = void 0;
|
|
4
|
+
exports.UNIQUE = `
|
|
5
|
+
var names = {};
|
|
6
|
+
function unique(defs) {
|
|
7
|
+
return defs.filter(
|
|
8
|
+
function(def) {
|
|
9
|
+
if (def.kind !== 'FragmentDefinition') return true;
|
|
10
|
+
var name = def.name.value
|
|
11
|
+
if (names[name]) {
|
|
12
|
+
return false;
|
|
13
|
+
} else {
|
|
14
|
+
names[name] = true;
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
)
|
|
19
|
+
}
|
|
20
|
+
`;
|
|
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
|
+
var type = node.type;
|
|
28
|
+
if (type.kind === "NamedType") {
|
|
29
|
+
refs.add(type.name.value);
|
|
30
|
+
}
|
|
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
|
+
var definitionRefs = {};
|
|
49
|
+
(function extractReferences() {
|
|
50
|
+
doc.definitions.forEach(function(def) {
|
|
51
|
+
if (def.name) {
|
|
52
|
+
var refs = new Set();
|
|
53
|
+
collectFragmentReferences(def, refs);
|
|
54
|
+
definitionRefs[def.name.value] = refs;
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
})();
|
|
58
|
+
function findOperation(doc, name) {
|
|
59
|
+
for (var i = 0; i < doc.definitions.length; i++) {
|
|
60
|
+
var element = doc.definitions[i];
|
|
61
|
+
if (element.name && element.name.value == name) {
|
|
62
|
+
return element;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
function oneQuery(doc, operationName) {
|
|
67
|
+
// Copy the DocumentNode, but clear out the definitions
|
|
68
|
+
var 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
|
+
var opRefs = definitionRefs[operationName] || new Set();
|
|
78
|
+
var allRefs = new Set();
|
|
79
|
+
var 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
|
+
var prevRefs = newRefs;
|
|
86
|
+
newRefs = new Set();
|
|
87
|
+
prevRefs.forEach(function(refName) {
|
|
88
|
+
if (!allRefs.has(refName)) {
|
|
89
|
+
allRefs.add(refName);
|
|
90
|
+
var childRefs = definitionRefs[refName] || new Set();
|
|
91
|
+
childRefs.forEach(function(childRef) {
|
|
92
|
+
newRefs.add(childRef);
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
allRefs.forEach(function(refName) {
|
|
98
|
+
var op = findOperation(doc, refName);
|
|
99
|
+
if (op) {
|
|
100
|
+
newDoc.definitions.push(op);
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
return newDoc;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
module.exports = doc;
|
|
107
|
+
`;
|
|
108
|
+
//# sourceMappingURL=snippets.js.map
|
|
@@ -0,0 +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"}
|