@shopify/hydrogen-codegen 0.2.0 → 0.2.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/cjs/patch.cjs
CHANGED
|
@@ -1,53 +1,4 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var path = require('node:path');
|
|
4
|
-
var fs = require('node:fs');
|
|
5
|
-
var node_module = require('node:module');
|
|
6
|
-
var node_url = require('node:url');
|
|
7
|
-
|
|
8
|
-
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
|
|
9
|
-
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
10
|
-
|
|
11
|
-
var path__default = /*#__PURE__*/_interopDefault(path);
|
|
12
|
-
var fs__default = /*#__PURE__*/_interopDefault(fs);
|
|
13
|
-
|
|
14
|
-
const require2 = node_module.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.src || new URL('out.js', document.baseURI).href)));
|
|
15
|
-
const realGqlTagPluck = require2.resolve("@graphql-tools/graphql-tag-pluck");
|
|
16
|
-
const depth = path__default.default.extname((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.src || new URL('out.js', document.baseURI).href))) === ".ts" ? "../" : "../../";
|
|
17
|
-
const vendorGqlTagPluck = node_url.fileURLToPath(
|
|
18
|
-
new URL(depth + "/vendor/graphql-tag-pluck", (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.src || new URL('out.js', document.baseURI).href)))
|
|
19
|
-
);
|
|
20
|
-
fs__default.default.copyFileSync(
|
|
21
|
-
path__default.default.join(vendorGqlTagPluck, "visitor.cjs"),
|
|
22
|
-
realGqlTagPluck.replace(/index\.js$/, "visitor.js")
|
|
23
|
-
);
|
|
24
|
-
fs__default.default.copyFileSync(
|
|
25
|
-
path__default.default.join(vendorGqlTagPluck, "visitor.mjs"),
|
|
26
|
-
realGqlTagPluck.replace("cjs", "esm").replace(/index\.js$/, "visitor.js")
|
|
27
|
-
);
|
|
28
|
-
const visitorPluginCommon = require2.resolve(
|
|
29
|
-
"@graphql-codegen/visitor-plugin-common"
|
|
30
|
-
);
|
|
31
|
-
const selectionSetToObjectFileCJS = visitorPluginCommon.replace(
|
|
32
|
-
"index.js",
|
|
33
|
-
"selection-set-to-object.js"
|
|
34
|
-
);
|
|
35
|
-
const selectionSetToObjectFileESM = selectionSetToObjectFileCJS.replace(
|
|
36
|
-
"cjs",
|
|
37
|
-
"esm"
|
|
38
|
-
);
|
|
39
|
-
fs__default.default.writeFileSync(
|
|
40
|
-
selectionSetToObjectFileCJS,
|
|
41
|
-
patchSelectionSet(fs__default.default.readFileSync(selectionSetToObjectFileCJS, "utf-8")),
|
|
42
|
-
"utf-8"
|
|
43
|
-
);
|
|
44
|
-
fs__default.default.writeFileSync(
|
|
45
|
-
selectionSetToObjectFileESM,
|
|
46
|
-
patchSelectionSet(fs__default.default.readFileSync(selectionSetToObjectFileESM, "utf-8")),
|
|
47
|
-
"utf-8"
|
|
48
|
-
);
|
|
49
|
-
function patchSelectionSet(content) {
|
|
50
|
-
return content.replace("&& s.union", "&& s?.union");
|
|
51
|
-
}
|
|
52
3
|
//# sourceMappingURL=out.js.map
|
|
53
4
|
//# sourceMappingURL=patch.cjs.map
|
package/dist/cjs/patch.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":""}
|
package/dist/esm/patch.js
CHANGED
|
@@ -1,45 +1,3 @@
|
|
|
1
|
-
import path from 'node:path';
|
|
2
|
-
import fs from 'node:fs';
|
|
3
|
-
import { createRequire } from 'node:module';
|
|
4
|
-
import { fileURLToPath } from 'node:url';
|
|
5
1
|
|
|
6
|
-
const require2 = createRequire(import.meta.url);
|
|
7
|
-
const realGqlTagPluck = require2.resolve("@graphql-tools/graphql-tag-pluck");
|
|
8
|
-
const depth = path.extname(import.meta.url) === ".ts" ? "../" : "../../";
|
|
9
|
-
const vendorGqlTagPluck = fileURLToPath(
|
|
10
|
-
new URL(depth + "/vendor/graphql-tag-pluck", import.meta.url)
|
|
11
|
-
);
|
|
12
|
-
fs.copyFileSync(
|
|
13
|
-
path.join(vendorGqlTagPluck, "visitor.cjs"),
|
|
14
|
-
realGqlTagPluck.replace(/index\.js$/, "visitor.js")
|
|
15
|
-
);
|
|
16
|
-
fs.copyFileSync(
|
|
17
|
-
path.join(vendorGqlTagPluck, "visitor.mjs"),
|
|
18
|
-
realGqlTagPluck.replace("cjs", "esm").replace(/index\.js$/, "visitor.js")
|
|
19
|
-
);
|
|
20
|
-
const visitorPluginCommon = require2.resolve(
|
|
21
|
-
"@graphql-codegen/visitor-plugin-common"
|
|
22
|
-
);
|
|
23
|
-
const selectionSetToObjectFileCJS = visitorPluginCommon.replace(
|
|
24
|
-
"index.js",
|
|
25
|
-
"selection-set-to-object.js"
|
|
26
|
-
);
|
|
27
|
-
const selectionSetToObjectFileESM = selectionSetToObjectFileCJS.replace(
|
|
28
|
-
"cjs",
|
|
29
|
-
"esm"
|
|
30
|
-
);
|
|
31
|
-
fs.writeFileSync(
|
|
32
|
-
selectionSetToObjectFileCJS,
|
|
33
|
-
patchSelectionSet(fs.readFileSync(selectionSetToObjectFileCJS, "utf-8")),
|
|
34
|
-
"utf-8"
|
|
35
|
-
);
|
|
36
|
-
fs.writeFileSync(
|
|
37
|
-
selectionSetToObjectFileESM,
|
|
38
|
-
patchSelectionSet(fs.readFileSync(selectionSetToObjectFileESM, "utf-8")),
|
|
39
|
-
"utf-8"
|
|
40
|
-
);
|
|
41
|
-
function patchSelectionSet(content) {
|
|
42
|
-
return content.replace("&& s.union", "&& s?.union");
|
|
43
|
-
}
|
|
44
2
|
//# sourceMappingURL=out.js.map
|
|
45
3
|
//# sourceMappingURL=patch.js.map
|
package/dist/esm/patch.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"access": "public",
|
|
5
5
|
"@shopify:registry": "https://registry.npmjs.org"
|
|
6
6
|
},
|
|
7
|
-
"version": "0.2.
|
|
7
|
+
"version": "0.2.1",
|
|
8
8
|
"license": "MIT",
|
|
9
9
|
"type": "module",
|
|
10
10
|
"main": "dist/cjs/index.cjs",
|
|
@@ -39,19 +39,18 @@
|
|
|
39
39
|
"directory": "packages/hydrogen-codegen"
|
|
40
40
|
},
|
|
41
41
|
"files": [
|
|
42
|
-
"dist"
|
|
43
|
-
"vendor"
|
|
42
|
+
"dist"
|
|
44
43
|
],
|
|
45
44
|
"devDependencies": {
|
|
46
|
-
"@graphql-codegen/cli": "^5.0.
|
|
47
|
-
"@graphql-codegen/plugin-helpers": "^5.0.
|
|
48
|
-
"@graphql-tools/utils": "^10.0.
|
|
45
|
+
"@graphql-codegen/cli": "^5.0.1",
|
|
46
|
+
"@graphql-codegen/plugin-helpers": "^5.0.2",
|
|
47
|
+
"@graphql-tools/utils": "^10.0.13",
|
|
49
48
|
"vitest": "^1.0.4"
|
|
50
49
|
},
|
|
51
50
|
"dependencies": {
|
|
52
|
-
"@graphql-codegen/add": "^5.0.
|
|
53
|
-
"@graphql-codegen/typescript": "^4.0.
|
|
54
|
-
"@graphql-codegen/typescript-operations": "^4.0
|
|
51
|
+
"@graphql-codegen/add": "^5.0.1",
|
|
52
|
+
"@graphql-codegen/typescript": "^4.0.2",
|
|
53
|
+
"@graphql-codegen/typescript-operations": "^4.1.0",
|
|
55
54
|
"type-fest": "^4.5.0"
|
|
56
55
|
},
|
|
57
56
|
"peerDependencies": {
|
|
@@ -1,377 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
Object.defineProperty(exports, '__esModule', {value: true});
|
|
3
|
-
const types_1 = require('@babel/types');
|
|
4
|
-
const utils_1 = require('@graphql-tools/utils');
|
|
5
|
-
const utils_js_1 = require('./utils.js');
|
|
6
|
-
const defaults = {
|
|
7
|
-
modules: [
|
|
8
|
-
{
|
|
9
|
-
name: 'graphql-tag',
|
|
10
|
-
},
|
|
11
|
-
{
|
|
12
|
-
name: 'graphql-tag.macro',
|
|
13
|
-
},
|
|
14
|
-
{
|
|
15
|
-
name: '@apollo/client',
|
|
16
|
-
identifier: 'gql',
|
|
17
|
-
},
|
|
18
|
-
{
|
|
19
|
-
name: '@apollo/client/core',
|
|
20
|
-
identifier: 'gql',
|
|
21
|
-
},
|
|
22
|
-
{
|
|
23
|
-
name: 'apollo-angular',
|
|
24
|
-
identifier: 'gql',
|
|
25
|
-
},
|
|
26
|
-
{
|
|
27
|
-
name: 'gatsby',
|
|
28
|
-
identifier: 'graphql',
|
|
29
|
-
},
|
|
30
|
-
{
|
|
31
|
-
name: 'apollo-server-express',
|
|
32
|
-
identifier: 'gql',
|
|
33
|
-
},
|
|
34
|
-
{
|
|
35
|
-
name: 'apollo-server',
|
|
36
|
-
identifier: 'gql',
|
|
37
|
-
},
|
|
38
|
-
{
|
|
39
|
-
name: 'react-relay',
|
|
40
|
-
identifier: 'graphql',
|
|
41
|
-
},
|
|
42
|
-
{
|
|
43
|
-
name: 'react-relay/hooks',
|
|
44
|
-
identifier: 'graphql',
|
|
45
|
-
},
|
|
46
|
-
{
|
|
47
|
-
name: 'relay-runtime',
|
|
48
|
-
identifier: 'graphql',
|
|
49
|
-
},
|
|
50
|
-
{
|
|
51
|
-
name: 'babel-plugin-relay/macro',
|
|
52
|
-
identifier: 'graphql',
|
|
53
|
-
},
|
|
54
|
-
{
|
|
55
|
-
name: 'apollo-boost',
|
|
56
|
-
identifier: 'gql',
|
|
57
|
-
},
|
|
58
|
-
{
|
|
59
|
-
name: 'apollo-server-koa',
|
|
60
|
-
identifier: 'gql',
|
|
61
|
-
},
|
|
62
|
-
{
|
|
63
|
-
name: 'apollo-server-hapi',
|
|
64
|
-
identifier: 'gql',
|
|
65
|
-
},
|
|
66
|
-
{
|
|
67
|
-
name: 'apollo-server-fastify',
|
|
68
|
-
identifier: 'gql',
|
|
69
|
-
},
|
|
70
|
-
{
|
|
71
|
-
name: ' apollo-server-lambda',
|
|
72
|
-
identifier: 'gql',
|
|
73
|
-
},
|
|
74
|
-
{
|
|
75
|
-
name: 'apollo-server-micro',
|
|
76
|
-
identifier: 'gql',
|
|
77
|
-
},
|
|
78
|
-
{
|
|
79
|
-
name: 'apollo-server-azure-functions',
|
|
80
|
-
identifier: 'gql',
|
|
81
|
-
},
|
|
82
|
-
{
|
|
83
|
-
name: 'apollo-server-cloud-functions',
|
|
84
|
-
identifier: 'gql',
|
|
85
|
-
},
|
|
86
|
-
{
|
|
87
|
-
name: 'apollo-server-cloudflare',
|
|
88
|
-
identifier: 'gql',
|
|
89
|
-
},
|
|
90
|
-
{
|
|
91
|
-
name: 'graphql.macro',
|
|
92
|
-
identifier: 'gql',
|
|
93
|
-
},
|
|
94
|
-
{
|
|
95
|
-
name: '@urql/core',
|
|
96
|
-
identifier: 'gql',
|
|
97
|
-
},
|
|
98
|
-
{
|
|
99
|
-
name: 'urql',
|
|
100
|
-
identifier: 'gql',
|
|
101
|
-
},
|
|
102
|
-
{
|
|
103
|
-
name: '@urql/preact',
|
|
104
|
-
identifier: 'gql',
|
|
105
|
-
},
|
|
106
|
-
{
|
|
107
|
-
name: '@urql/svelte',
|
|
108
|
-
identifier: 'gql',
|
|
109
|
-
},
|
|
110
|
-
{
|
|
111
|
-
name: '@urql/vue',
|
|
112
|
-
identifier: 'gql',
|
|
113
|
-
},
|
|
114
|
-
],
|
|
115
|
-
gqlMagicComment: 'graphql',
|
|
116
|
-
globalGqlIdentifierName: ['gql', 'graphql'],
|
|
117
|
-
};
|
|
118
|
-
function defaultPluckStringFromFile(code, {start, end}, options = {}) {
|
|
119
|
-
return (0, utils_js_1.freeText)(
|
|
120
|
-
code
|
|
121
|
-
// Slice quotes
|
|
122
|
-
.slice(start + 1, end - 1)
|
|
123
|
-
// Erase string interpolations as we gonna export everything as a single
|
|
124
|
-
// string anyway
|
|
125
|
-
.replace(/\$\{[^}]*\}/g, '')
|
|
126
|
-
.split('\\`')
|
|
127
|
-
.join('`'),
|
|
128
|
-
options.skipIndent,
|
|
129
|
-
);
|
|
130
|
-
}
|
|
131
|
-
function defaultIsGqlTemplateLiteral(node, options) {
|
|
132
|
-
const leadingComments = node.leadingComments;
|
|
133
|
-
if (!leadingComments) {
|
|
134
|
-
return;
|
|
135
|
-
}
|
|
136
|
-
if (!leadingComments.length) {
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
const leadingComment = leadingComments[leadingComments.length - 1];
|
|
140
|
-
const leadingCommentValue = leadingComment.value.trim().toLowerCase();
|
|
141
|
-
if (leadingCommentValue === options.gqlMagicComment) {
|
|
142
|
-
return true;
|
|
143
|
-
}
|
|
144
|
-
return false;
|
|
145
|
-
}
|
|
146
|
-
exports.default = (code, out, options = {}) => {
|
|
147
|
-
// Apply defaults to options
|
|
148
|
-
let {
|
|
149
|
-
modules = [],
|
|
150
|
-
globalGqlIdentifierName,
|
|
151
|
-
gqlMagicComment,
|
|
152
|
-
skipIndent,
|
|
153
|
-
isGqlTemplateLiteral = defaultIsGqlTemplateLiteral,
|
|
154
|
-
pluckStringFromFile = defaultPluckStringFromFile,
|
|
155
|
-
} = {
|
|
156
|
-
...defaults,
|
|
157
|
-
...options,
|
|
158
|
-
};
|
|
159
|
-
// Prevent case related potential errors
|
|
160
|
-
gqlMagicComment = gqlMagicComment.toLowerCase();
|
|
161
|
-
// normalize `name` and `identifier` values
|
|
162
|
-
modules = modules.map((mod) => {
|
|
163
|
-
return {
|
|
164
|
-
name: mod.name,
|
|
165
|
-
identifier: mod.identifier && mod.identifier.toLowerCase(),
|
|
166
|
-
};
|
|
167
|
-
});
|
|
168
|
-
globalGqlIdentifierName = (0, utils_1.asArray)(globalGqlIdentifierName).map(
|
|
169
|
-
(s) => s.toLowerCase(),
|
|
170
|
-
);
|
|
171
|
-
const hooksOptions = {
|
|
172
|
-
skipIndent,
|
|
173
|
-
gqlMagicComment,
|
|
174
|
-
modules,
|
|
175
|
-
globalGqlIdentifierName,
|
|
176
|
-
};
|
|
177
|
-
// Keep imported identifiers
|
|
178
|
-
// import gql from 'graphql-tag' -> gql
|
|
179
|
-
// import { graphql } from 'gatsby' -> graphql
|
|
180
|
-
// Will result with ['gql', 'graphql']
|
|
181
|
-
const definedIdentifierNames = [];
|
|
182
|
-
const alreadyProcessedOperationsCache = new Set();
|
|
183
|
-
// Will accumulate all template literals
|
|
184
|
-
const gqlTemplateLiterals = [];
|
|
185
|
-
// Check if package is registered
|
|
186
|
-
function isValidPackage(name) {
|
|
187
|
-
return modules.some(
|
|
188
|
-
(pkg) =>
|
|
189
|
-
pkg.name && name && pkg.name.toLowerCase() === name.toLowerCase(),
|
|
190
|
-
);
|
|
191
|
-
}
|
|
192
|
-
// Check if identifier is defined and imported from registered packages
|
|
193
|
-
function isValidIdentifier(name) {
|
|
194
|
-
return (
|
|
195
|
-
definedIdentifierNames.some((id) => id === name) ||
|
|
196
|
-
globalGqlIdentifierName.includes(name)
|
|
197
|
-
);
|
|
198
|
-
}
|
|
199
|
-
const addTemplateLiteralToResult = (content) => {
|
|
200
|
-
const cacheKey = `end/${content.end}/start/${content.start}/${content.content}`;
|
|
201
|
-
if (alreadyProcessedOperationsCache.has(cacheKey)) {
|
|
202
|
-
return;
|
|
203
|
-
}
|
|
204
|
-
alreadyProcessedOperationsCache.add(cacheKey);
|
|
205
|
-
gqlTemplateLiterals.push(content);
|
|
206
|
-
};
|
|
207
|
-
// Push all template literals leaded by graphql magic comment
|
|
208
|
-
// e.g. /* GraphQL */ `query myQuery {}` -> query myQuery {}
|
|
209
|
-
const pluckMagicTemplateLiteral = (node, takeExpression = false) => {
|
|
210
|
-
if (!isGqlTemplateLiteral(node, hooksOptions)) {
|
|
211
|
-
return;
|
|
212
|
-
}
|
|
213
|
-
const nodeToUse = takeExpression ? node.expression : node;
|
|
214
|
-
const gqlTemplateLiteral = pluckStringFromFile(
|
|
215
|
-
code,
|
|
216
|
-
nodeToUse,
|
|
217
|
-
hooksOptions,
|
|
218
|
-
);
|
|
219
|
-
if (gqlTemplateLiteral) {
|
|
220
|
-
addTemplateLiteralToResult({
|
|
221
|
-
content: gqlTemplateLiteral,
|
|
222
|
-
loc: node.loc,
|
|
223
|
-
end: node.end,
|
|
224
|
-
start: node.start,
|
|
225
|
-
});
|
|
226
|
-
}
|
|
227
|
-
};
|
|
228
|
-
const visitor = {
|
|
229
|
-
CallExpression: {
|
|
230
|
-
enter(path) {
|
|
231
|
-
// Find the identifier name used from graphql-tag, commonJS
|
|
232
|
-
// e.g. import gql from 'graphql-tag' -> gql
|
|
233
|
-
const arg0 = path.node.arguments[0];
|
|
234
|
-
if (
|
|
235
|
-
'name' in path.node.callee &&
|
|
236
|
-
path.node.callee.name === 'require' &&
|
|
237
|
-
'value' in arg0 &&
|
|
238
|
-
typeof arg0.value === 'string' &&
|
|
239
|
-
isValidPackage(arg0.value)
|
|
240
|
-
) {
|
|
241
|
-
if (!(0, types_1.isVariableDeclarator)(path.parent)) {
|
|
242
|
-
return;
|
|
243
|
-
}
|
|
244
|
-
if (!(0, types_1.isIdentifier)(path.parent.id)) {
|
|
245
|
-
return;
|
|
246
|
-
}
|
|
247
|
-
definedIdentifierNames.push(path.parent.id.name);
|
|
248
|
-
return;
|
|
249
|
-
}
|
|
250
|
-
// Checks to see if a node represents a typescript '<expression> as const' expression
|
|
251
|
-
function isTSAsConstExpression(node) {
|
|
252
|
-
return (
|
|
253
|
-
(0, types_1.isTSAsExpression)(node) &&
|
|
254
|
-
(0, types_1.isTSTypeReference)(node.typeAnnotation) &&
|
|
255
|
-
(0, types_1.isIdentifier)(node.typeAnnotation.typeName) &&
|
|
256
|
-
node.typeAnnotation.typeName.name === 'const'
|
|
257
|
-
);
|
|
258
|
-
}
|
|
259
|
-
// Extract template literal from as const expression if applicable
|
|
260
|
-
// e.g. gql(`query myQuery {}` as const)
|
|
261
|
-
const unwrappedExpression = isTSAsConstExpression(arg0)
|
|
262
|
-
? arg0.expression
|
|
263
|
-
: arg0;
|
|
264
|
-
// Push strings template literals to gql calls
|
|
265
|
-
// e.g. gql(`query myQuery {}`) -> query myQuery {}
|
|
266
|
-
if (
|
|
267
|
-
(0, types_1.isIdentifier)(path.node.callee) &&
|
|
268
|
-
isValidIdentifier(path.node.callee.name) &&
|
|
269
|
-
(0, types_1.isTemplateLiteral)(unwrappedExpression)
|
|
270
|
-
) {
|
|
271
|
-
const {start, end, loc} = unwrappedExpression;
|
|
272
|
-
if (start != null && end != null && start != null && loc != null) {
|
|
273
|
-
const gqlTemplateLiteral = pluckStringFromFile(
|
|
274
|
-
code,
|
|
275
|
-
unwrappedExpression,
|
|
276
|
-
hooksOptions,
|
|
277
|
-
);
|
|
278
|
-
// If the entire template was made out of interpolations it should be an empty
|
|
279
|
-
// string by now and thus should be ignored
|
|
280
|
-
if (gqlTemplateLiteral) {
|
|
281
|
-
addTemplateLiteralToResult({
|
|
282
|
-
content: gqlTemplateLiteral,
|
|
283
|
-
loc,
|
|
284
|
-
end,
|
|
285
|
-
start,
|
|
286
|
-
});
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
},
|
|
291
|
-
},
|
|
292
|
-
ImportDeclaration: {
|
|
293
|
-
enter(path) {
|
|
294
|
-
// Find the identifier name used from graphql-tag, es6
|
|
295
|
-
// e.g. import gql from 'graphql-tag' -> gql
|
|
296
|
-
if (!isValidPackage(path.node.source.value)) {
|
|
297
|
-
return;
|
|
298
|
-
}
|
|
299
|
-
const moduleNode = modules.find(
|
|
300
|
-
(pkg) =>
|
|
301
|
-
pkg.name.toLowerCase() === path.node.source.value.toLowerCase(),
|
|
302
|
-
);
|
|
303
|
-
if (moduleNode == null) {
|
|
304
|
-
return;
|
|
305
|
-
}
|
|
306
|
-
const gqlImportSpecifier = path.node.specifiers.find(
|
|
307
|
-
(importSpecifier) => {
|
|
308
|
-
// When it's a default import and registered package has no named identifier
|
|
309
|
-
if (
|
|
310
|
-
(0, types_1.isImportDefaultSpecifier)(importSpecifier) &&
|
|
311
|
-
!moduleNode.identifier
|
|
312
|
-
) {
|
|
313
|
-
return true;
|
|
314
|
-
}
|
|
315
|
-
// When it's a named import that matches registered package's identifier
|
|
316
|
-
if (
|
|
317
|
-
(0, types_1.isImportSpecifier)(importSpecifier) &&
|
|
318
|
-
'name' in importSpecifier.imported &&
|
|
319
|
-
importSpecifier.imported.name === moduleNode.identifier
|
|
320
|
-
) {
|
|
321
|
-
return true;
|
|
322
|
-
}
|
|
323
|
-
return false;
|
|
324
|
-
},
|
|
325
|
-
);
|
|
326
|
-
if (!gqlImportSpecifier) {
|
|
327
|
-
return;
|
|
328
|
-
}
|
|
329
|
-
definedIdentifierNames.push(gqlImportSpecifier.local.name);
|
|
330
|
-
},
|
|
331
|
-
},
|
|
332
|
-
ExpressionStatement: {
|
|
333
|
-
exit(path) {
|
|
334
|
-
// Push all template literals leaded by graphql magic comment
|
|
335
|
-
// e.g. /* GraphQL */ `query myQuery {}` -> query myQuery {}
|
|
336
|
-
if (!(0, types_1.isTemplateLiteral)(path.node.expression)) {
|
|
337
|
-
return;
|
|
338
|
-
}
|
|
339
|
-
pluckMagicTemplateLiteral(path.node, true);
|
|
340
|
-
},
|
|
341
|
-
},
|
|
342
|
-
TemplateLiteral: {
|
|
343
|
-
exit(path) {
|
|
344
|
-
pluckMagicTemplateLiteral(path.node);
|
|
345
|
-
},
|
|
346
|
-
},
|
|
347
|
-
TaggedTemplateExpression: {
|
|
348
|
-
exit(path) {
|
|
349
|
-
// Push all template literals provided to the found identifier name
|
|
350
|
-
// e.g. gql `query myQuery {}` -> query myQuery {}
|
|
351
|
-
if (
|
|
352
|
-
!(0, types_1.isIdentifier)(path.node.tag) ||
|
|
353
|
-
!isValidIdentifier(path.node.tag.name)
|
|
354
|
-
) {
|
|
355
|
-
return;
|
|
356
|
-
}
|
|
357
|
-
const gqlTemplateLiteral = pluckStringFromFile(
|
|
358
|
-
code,
|
|
359
|
-
path.node.quasi,
|
|
360
|
-
hooksOptions,
|
|
361
|
-
);
|
|
362
|
-
if (gqlTemplateLiteral) {
|
|
363
|
-
addTemplateLiteralToResult({
|
|
364
|
-
content: gqlTemplateLiteral,
|
|
365
|
-
end: path.node.quasi.end,
|
|
366
|
-
start: path.node.quasi.start,
|
|
367
|
-
loc: path.node.quasi.loc,
|
|
368
|
-
});
|
|
369
|
-
}
|
|
370
|
-
},
|
|
371
|
-
},
|
|
372
|
-
exit() {
|
|
373
|
-
out.returnValue = gqlTemplateLiterals;
|
|
374
|
-
},
|
|
375
|
-
};
|
|
376
|
-
return visitor;
|
|
377
|
-
};
|
|
@@ -1,383 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
isIdentifier,
|
|
3
|
-
isImportDefaultSpecifier,
|
|
4
|
-
isImportSpecifier,
|
|
5
|
-
isTemplateLiteral,
|
|
6
|
-
isTSAsExpression,
|
|
7
|
-
isTSTypeReference,
|
|
8
|
-
isVariableDeclarator,
|
|
9
|
-
} from '@babel/types';
|
|
10
|
-
import {asArray} from '@graphql-tools/utils';
|
|
11
|
-
import {freeText} from './utils.js';
|
|
12
|
-
const defaults = {
|
|
13
|
-
modules: [
|
|
14
|
-
{
|
|
15
|
-
name: 'graphql-tag',
|
|
16
|
-
},
|
|
17
|
-
{
|
|
18
|
-
name: 'graphql-tag.macro',
|
|
19
|
-
},
|
|
20
|
-
{
|
|
21
|
-
name: '@apollo/client',
|
|
22
|
-
identifier: 'gql',
|
|
23
|
-
},
|
|
24
|
-
{
|
|
25
|
-
name: '@apollo/client/core',
|
|
26
|
-
identifier: 'gql',
|
|
27
|
-
},
|
|
28
|
-
{
|
|
29
|
-
name: 'apollo-angular',
|
|
30
|
-
identifier: 'gql',
|
|
31
|
-
},
|
|
32
|
-
{
|
|
33
|
-
name: 'gatsby',
|
|
34
|
-
identifier: 'graphql',
|
|
35
|
-
},
|
|
36
|
-
{
|
|
37
|
-
name: 'apollo-server-express',
|
|
38
|
-
identifier: 'gql',
|
|
39
|
-
},
|
|
40
|
-
{
|
|
41
|
-
name: 'apollo-server',
|
|
42
|
-
identifier: 'gql',
|
|
43
|
-
},
|
|
44
|
-
{
|
|
45
|
-
name: 'react-relay',
|
|
46
|
-
identifier: 'graphql',
|
|
47
|
-
},
|
|
48
|
-
{
|
|
49
|
-
name: 'react-relay/hooks',
|
|
50
|
-
identifier: 'graphql',
|
|
51
|
-
},
|
|
52
|
-
{
|
|
53
|
-
name: 'relay-runtime',
|
|
54
|
-
identifier: 'graphql',
|
|
55
|
-
},
|
|
56
|
-
{
|
|
57
|
-
name: 'babel-plugin-relay/macro',
|
|
58
|
-
identifier: 'graphql',
|
|
59
|
-
},
|
|
60
|
-
{
|
|
61
|
-
name: 'apollo-boost',
|
|
62
|
-
identifier: 'gql',
|
|
63
|
-
},
|
|
64
|
-
{
|
|
65
|
-
name: 'apollo-server-koa',
|
|
66
|
-
identifier: 'gql',
|
|
67
|
-
},
|
|
68
|
-
{
|
|
69
|
-
name: 'apollo-server-hapi',
|
|
70
|
-
identifier: 'gql',
|
|
71
|
-
},
|
|
72
|
-
{
|
|
73
|
-
name: 'apollo-server-fastify',
|
|
74
|
-
identifier: 'gql',
|
|
75
|
-
},
|
|
76
|
-
{
|
|
77
|
-
name: ' apollo-server-lambda',
|
|
78
|
-
identifier: 'gql',
|
|
79
|
-
},
|
|
80
|
-
{
|
|
81
|
-
name: 'apollo-server-micro',
|
|
82
|
-
identifier: 'gql',
|
|
83
|
-
},
|
|
84
|
-
{
|
|
85
|
-
name: 'apollo-server-azure-functions',
|
|
86
|
-
identifier: 'gql',
|
|
87
|
-
},
|
|
88
|
-
{
|
|
89
|
-
name: 'apollo-server-cloud-functions',
|
|
90
|
-
identifier: 'gql',
|
|
91
|
-
},
|
|
92
|
-
{
|
|
93
|
-
name: 'apollo-server-cloudflare',
|
|
94
|
-
identifier: 'gql',
|
|
95
|
-
},
|
|
96
|
-
{
|
|
97
|
-
name: 'graphql.macro',
|
|
98
|
-
identifier: 'gql',
|
|
99
|
-
},
|
|
100
|
-
{
|
|
101
|
-
name: '@urql/core',
|
|
102
|
-
identifier: 'gql',
|
|
103
|
-
},
|
|
104
|
-
{
|
|
105
|
-
name: 'urql',
|
|
106
|
-
identifier: 'gql',
|
|
107
|
-
},
|
|
108
|
-
{
|
|
109
|
-
name: '@urql/preact',
|
|
110
|
-
identifier: 'gql',
|
|
111
|
-
},
|
|
112
|
-
{
|
|
113
|
-
name: '@urql/svelte',
|
|
114
|
-
identifier: 'gql',
|
|
115
|
-
},
|
|
116
|
-
{
|
|
117
|
-
name: '@urql/vue',
|
|
118
|
-
identifier: 'gql',
|
|
119
|
-
},
|
|
120
|
-
],
|
|
121
|
-
gqlMagicComment: 'graphql',
|
|
122
|
-
globalGqlIdentifierName: ['gql', 'graphql'],
|
|
123
|
-
};
|
|
124
|
-
function defaultPluckStringFromFile(code, {start, end}, options = {}) {
|
|
125
|
-
return freeText(
|
|
126
|
-
code
|
|
127
|
-
// Slice quotes
|
|
128
|
-
.slice(start + 1, end - 1)
|
|
129
|
-
// Erase string interpolations as we gonna export everything as a single
|
|
130
|
-
// string anyway
|
|
131
|
-
.replace(/\$\{[^}]*\}/g, '')
|
|
132
|
-
.split('\\`')
|
|
133
|
-
.join('`'),
|
|
134
|
-
options.skipIndent,
|
|
135
|
-
);
|
|
136
|
-
}
|
|
137
|
-
function defaultIsGqlTemplateLiteral(node, options) {
|
|
138
|
-
const leadingComments = node.leadingComments;
|
|
139
|
-
if (!leadingComments) {
|
|
140
|
-
return;
|
|
141
|
-
}
|
|
142
|
-
if (!leadingComments.length) {
|
|
143
|
-
return;
|
|
144
|
-
}
|
|
145
|
-
const leadingComment = leadingComments[leadingComments.length - 1];
|
|
146
|
-
const leadingCommentValue = leadingComment.value.trim().toLowerCase();
|
|
147
|
-
if (leadingCommentValue === options.gqlMagicComment) {
|
|
148
|
-
return true;
|
|
149
|
-
}
|
|
150
|
-
return false;
|
|
151
|
-
}
|
|
152
|
-
export default (code, out, options = {}) => {
|
|
153
|
-
// Apply defaults to options
|
|
154
|
-
let {
|
|
155
|
-
modules = [],
|
|
156
|
-
globalGqlIdentifierName,
|
|
157
|
-
gqlMagicComment,
|
|
158
|
-
skipIndent,
|
|
159
|
-
isGqlTemplateLiteral = defaultIsGqlTemplateLiteral,
|
|
160
|
-
pluckStringFromFile = defaultPluckStringFromFile,
|
|
161
|
-
} = {
|
|
162
|
-
...defaults,
|
|
163
|
-
...options,
|
|
164
|
-
};
|
|
165
|
-
// Prevent case related potential errors
|
|
166
|
-
gqlMagicComment = gqlMagicComment.toLowerCase();
|
|
167
|
-
// normalize `name` and `identifier` values
|
|
168
|
-
modules = modules.map((mod) => {
|
|
169
|
-
return {
|
|
170
|
-
name: mod.name,
|
|
171
|
-
identifier: mod.identifier && mod.identifier.toLowerCase(),
|
|
172
|
-
};
|
|
173
|
-
});
|
|
174
|
-
globalGqlIdentifierName = asArray(globalGqlIdentifierName).map((s) =>
|
|
175
|
-
s.toLowerCase(),
|
|
176
|
-
);
|
|
177
|
-
const hooksOptions = {
|
|
178
|
-
skipIndent,
|
|
179
|
-
gqlMagicComment,
|
|
180
|
-
modules,
|
|
181
|
-
globalGqlIdentifierName,
|
|
182
|
-
};
|
|
183
|
-
// Keep imported identifiers
|
|
184
|
-
// import gql from 'graphql-tag' -> gql
|
|
185
|
-
// import { graphql } from 'gatsby' -> graphql
|
|
186
|
-
// Will result with ['gql', 'graphql']
|
|
187
|
-
const definedIdentifierNames = [];
|
|
188
|
-
const alreadyProcessedOperationsCache = new Set();
|
|
189
|
-
// Will accumulate all template literals
|
|
190
|
-
const gqlTemplateLiterals = [];
|
|
191
|
-
// Check if package is registered
|
|
192
|
-
function isValidPackage(name) {
|
|
193
|
-
return modules.some(
|
|
194
|
-
(pkg) =>
|
|
195
|
-
pkg.name && name && pkg.name.toLowerCase() === name.toLowerCase(),
|
|
196
|
-
);
|
|
197
|
-
}
|
|
198
|
-
// Check if identifier is defined and imported from registered packages
|
|
199
|
-
function isValidIdentifier(name) {
|
|
200
|
-
return (
|
|
201
|
-
definedIdentifierNames.some((id) => id === name) ||
|
|
202
|
-
globalGqlIdentifierName.includes(name)
|
|
203
|
-
);
|
|
204
|
-
}
|
|
205
|
-
const addTemplateLiteralToResult = (content) => {
|
|
206
|
-
const cacheKey = `end/${content.end}/start/${content.start}/${content.content}`;
|
|
207
|
-
if (alreadyProcessedOperationsCache.has(cacheKey)) {
|
|
208
|
-
return;
|
|
209
|
-
}
|
|
210
|
-
alreadyProcessedOperationsCache.add(cacheKey);
|
|
211
|
-
gqlTemplateLiterals.push(content);
|
|
212
|
-
};
|
|
213
|
-
// Push all template literals leaded by graphql magic comment
|
|
214
|
-
// e.g. /* GraphQL */ `query myQuery {}` -> query myQuery {}
|
|
215
|
-
const pluckMagicTemplateLiteral = (node, takeExpression = false) => {
|
|
216
|
-
if (!isGqlTemplateLiteral(node, hooksOptions)) {
|
|
217
|
-
return;
|
|
218
|
-
}
|
|
219
|
-
const nodeToUse = takeExpression ? node.expression : node;
|
|
220
|
-
const gqlTemplateLiteral = pluckStringFromFile(
|
|
221
|
-
code,
|
|
222
|
-
nodeToUse,
|
|
223
|
-
hooksOptions,
|
|
224
|
-
);
|
|
225
|
-
if (gqlTemplateLiteral) {
|
|
226
|
-
addTemplateLiteralToResult({
|
|
227
|
-
content: gqlTemplateLiteral,
|
|
228
|
-
loc: node.loc,
|
|
229
|
-
end: node.end,
|
|
230
|
-
start: node.start,
|
|
231
|
-
});
|
|
232
|
-
}
|
|
233
|
-
};
|
|
234
|
-
const visitor = {
|
|
235
|
-
CallExpression: {
|
|
236
|
-
enter(path) {
|
|
237
|
-
// Find the identifier name used from graphql-tag, commonJS
|
|
238
|
-
// e.g. import gql from 'graphql-tag' -> gql
|
|
239
|
-
const arg0 = path.node.arguments[0];
|
|
240
|
-
if (
|
|
241
|
-
'name' in path.node.callee &&
|
|
242
|
-
path.node.callee.name === 'require' &&
|
|
243
|
-
'value' in arg0 &&
|
|
244
|
-
typeof arg0.value === 'string' &&
|
|
245
|
-
isValidPackage(arg0.value)
|
|
246
|
-
) {
|
|
247
|
-
if (!isVariableDeclarator(path.parent)) {
|
|
248
|
-
return;
|
|
249
|
-
}
|
|
250
|
-
if (!isIdentifier(path.parent.id)) {
|
|
251
|
-
return;
|
|
252
|
-
}
|
|
253
|
-
definedIdentifierNames.push(path.parent.id.name);
|
|
254
|
-
return;
|
|
255
|
-
}
|
|
256
|
-
// Checks to see if a node represents a typescript '<expression> as const' expression
|
|
257
|
-
function isTSAsConstExpression(node) {
|
|
258
|
-
return (
|
|
259
|
-
isTSAsExpression(node) &&
|
|
260
|
-
isTSTypeReference(node.typeAnnotation) &&
|
|
261
|
-
isIdentifier(node.typeAnnotation.typeName) &&
|
|
262
|
-
node.typeAnnotation.typeName.name === 'const'
|
|
263
|
-
);
|
|
264
|
-
}
|
|
265
|
-
// Extract template literal from as const expression if applicable
|
|
266
|
-
// e.g. gql(`query myQuery {}` as const)
|
|
267
|
-
const unwrappedExpression = isTSAsConstExpression(arg0)
|
|
268
|
-
? arg0.expression
|
|
269
|
-
: arg0;
|
|
270
|
-
// Push strings template literals to gql calls
|
|
271
|
-
// e.g. gql(`query myQuery {}`) -> query myQuery {}
|
|
272
|
-
if (
|
|
273
|
-
isIdentifier(path.node.callee) &&
|
|
274
|
-
isValidIdentifier(path.node.callee.name) &&
|
|
275
|
-
isTemplateLiteral(unwrappedExpression)
|
|
276
|
-
) {
|
|
277
|
-
const {start, end, loc} = unwrappedExpression;
|
|
278
|
-
if (start != null && end != null && start != null && loc != null) {
|
|
279
|
-
const gqlTemplateLiteral = pluckStringFromFile(
|
|
280
|
-
code,
|
|
281
|
-
unwrappedExpression,
|
|
282
|
-
hooksOptions,
|
|
283
|
-
);
|
|
284
|
-
// If the entire template was made out of interpolations it should be an empty
|
|
285
|
-
// string by now and thus should be ignored
|
|
286
|
-
if (gqlTemplateLiteral) {
|
|
287
|
-
addTemplateLiteralToResult({
|
|
288
|
-
content: gqlTemplateLiteral,
|
|
289
|
-
loc,
|
|
290
|
-
end,
|
|
291
|
-
start,
|
|
292
|
-
});
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
},
|
|
297
|
-
},
|
|
298
|
-
ImportDeclaration: {
|
|
299
|
-
enter(path) {
|
|
300
|
-
// Find the identifier name used from graphql-tag, es6
|
|
301
|
-
// e.g. import gql from 'graphql-tag' -> gql
|
|
302
|
-
if (!isValidPackage(path.node.source.value)) {
|
|
303
|
-
return;
|
|
304
|
-
}
|
|
305
|
-
const moduleNode = modules.find(
|
|
306
|
-
(pkg) =>
|
|
307
|
-
pkg.name.toLowerCase() === path.node.source.value.toLowerCase(),
|
|
308
|
-
);
|
|
309
|
-
if (moduleNode == null) {
|
|
310
|
-
return;
|
|
311
|
-
}
|
|
312
|
-
const gqlImportSpecifier = path.node.specifiers.find(
|
|
313
|
-
(importSpecifier) => {
|
|
314
|
-
// When it's a default import and registered package has no named identifier
|
|
315
|
-
if (
|
|
316
|
-
isImportDefaultSpecifier(importSpecifier) &&
|
|
317
|
-
!moduleNode.identifier
|
|
318
|
-
) {
|
|
319
|
-
return true;
|
|
320
|
-
}
|
|
321
|
-
// When it's a named import that matches registered package's identifier
|
|
322
|
-
if (
|
|
323
|
-
isImportSpecifier(importSpecifier) &&
|
|
324
|
-
'name' in importSpecifier.imported &&
|
|
325
|
-
importSpecifier.imported.name === moduleNode.identifier
|
|
326
|
-
) {
|
|
327
|
-
return true;
|
|
328
|
-
}
|
|
329
|
-
return false;
|
|
330
|
-
},
|
|
331
|
-
);
|
|
332
|
-
if (!gqlImportSpecifier) {
|
|
333
|
-
return;
|
|
334
|
-
}
|
|
335
|
-
definedIdentifierNames.push(gqlImportSpecifier.local.name);
|
|
336
|
-
},
|
|
337
|
-
},
|
|
338
|
-
ExpressionStatement: {
|
|
339
|
-
exit(path) {
|
|
340
|
-
// Push all template literals leaded by graphql magic comment
|
|
341
|
-
// e.g. /* GraphQL */ `query myQuery {}` -> query myQuery {}
|
|
342
|
-
if (!isTemplateLiteral(path.node.expression)) {
|
|
343
|
-
return;
|
|
344
|
-
}
|
|
345
|
-
pluckMagicTemplateLiteral(path.node, true);
|
|
346
|
-
},
|
|
347
|
-
},
|
|
348
|
-
TemplateLiteral: {
|
|
349
|
-
exit(path) {
|
|
350
|
-
pluckMagicTemplateLiteral(path.node);
|
|
351
|
-
},
|
|
352
|
-
},
|
|
353
|
-
TaggedTemplateExpression: {
|
|
354
|
-
exit(path) {
|
|
355
|
-
// Push all template literals provided to the found identifier name
|
|
356
|
-
// e.g. gql `query myQuery {}` -> query myQuery {}
|
|
357
|
-
if (
|
|
358
|
-
!isIdentifier(path.node.tag) ||
|
|
359
|
-
!isValidIdentifier(path.node.tag.name)
|
|
360
|
-
) {
|
|
361
|
-
return;
|
|
362
|
-
}
|
|
363
|
-
const gqlTemplateLiteral = pluckStringFromFile(
|
|
364
|
-
code,
|
|
365
|
-
path.node.quasi,
|
|
366
|
-
hooksOptions,
|
|
367
|
-
);
|
|
368
|
-
if (gqlTemplateLiteral) {
|
|
369
|
-
addTemplateLiteralToResult({
|
|
370
|
-
content: gqlTemplateLiteral,
|
|
371
|
-
end: path.node.quasi.end,
|
|
372
|
-
start: path.node.quasi.start,
|
|
373
|
-
loc: path.node.quasi.loc,
|
|
374
|
-
});
|
|
375
|
-
}
|
|
376
|
-
},
|
|
377
|
-
},
|
|
378
|
-
exit() {
|
|
379
|
-
out.returnValue = gqlTemplateLiterals;
|
|
380
|
-
},
|
|
381
|
-
};
|
|
382
|
-
return visitor;
|
|
383
|
-
};
|