@shopify/hydrogen-codegen 0.2.0 → 0.2.2

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.
@@ -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
- };