@mo36924/babel-plugin-graphql-tagged-template 5.1.14 → 5.3.3

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.cjs CHANGED
@@ -5,20 +5,18 @@ var node_path = require('node:path');
5
5
  var node_url = require('node:url');
6
6
  var core = require('@babel/core');
7
7
  var helperPluginUtils = require('@babel/helper-plugin-utils');
8
- var basex = require('base-x');
9
8
  var graphql = require('graphql');
9
+ var packages_babelPluginGraphqlTaggedTemplate_dist_queries = require('./queries.cjs');
10
10
 
11
11
  var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
12
- const queriesCache = [];
13
12
  const queriesDir = node_path.dirname(node_url.fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('packages/babel-plugin-graphql-tagged-template/dist/index.cjs', document.baseURI).href))));
14
13
  const queriesPaths = [
15
14
  "js",
16
15
  "cjs",
17
16
  "ts"
18
17
  ].map((extname)=>node_path.join(queriesDir, `queries.${extname}`));
19
- const base52 = basex("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
20
- const hash = (data)=>base52.encode(node_crypto.createHash("sha256").update(data).digest());
21
- var index = helperPluginUtils.declare((_, { schema, development, queries = queriesCache })=>{
18
+ const hash = (data)=>node_crypto.createHash("sha256").update(data).digest("base64url");
19
+ var index = helperPluginUtils.declare((_api, { schema, development, queries = packages_babelPluginGraphqlTaggedTemplate_dist_queries.queries })=>{
22
20
  return {
23
21
  name: "babel-plugin-graphql-tagged-template",
24
22
  visitor: {
@@ -26,14 +24,17 @@ var index = helperPluginUtils.declare((_, { schema, development, queries = queri
26
24
  VariableDeclarator (path, { filename = "" }) {
27
25
  if (!(queriesPaths.includes(filename) && path.get("id").isIdentifier({
28
26
  name: "queries"
29
- }) && path.get("init").isObjectExpression())) {
27
+ }))) {
30
28
  return;
31
29
  }
32
- path.get("init").replaceWith(core.types.objectExpression(queries.sort().map((query)=>core.types.objectProperty(core.types.identifier(hash(query)), core.types.callExpression(core.types.memberExpression(core.types.identifier("JSON"), core.types.identifier("parse")), [
33
- core.types.stringLiteral(JSON.stringify(graphql.parse(query, {
34
- noLocation: true
35
- })))
36
- ])))));
30
+ path.get("init").replaceWith(core.types.callExpression(core.types.memberExpression(core.types.identifier("Object"), core.types.identifier("assign")), [
31
+ core.types.callExpression(core.types.memberExpression(core.types.identifier("Object"), core.types.identifier("create")), [
32
+ core.types.nullLiteral()
33
+ ]),
34
+ core.types.objectExpression(Object.entries(queries).sort(([a], [b])=>a.localeCompare(b)).map(([key, documentNode])=>core.types.objectProperty(core.types.stringLiteral(key), core.types.callExpression(core.types.memberExpression(core.types.identifier("JSON"), core.types.identifier("parse")), [
35
+ core.types.stringLiteral(JSON.stringify(documentNode))
36
+ ]))))
37
+ ]));
37
38
  }
38
39
  },
39
40
  TaggedTemplateExpression (path) {
@@ -70,13 +71,14 @@ var index = helperPluginUtils.declare((_, { schema, development, queries = queri
70
71
  if (field.kind !== "Field") {
71
72
  throw path.buildCodeFrameError("Only a field is allowed");
72
73
  }
73
- const isMutation = !!schema.getMutationType()?.getFields()[field.name.value];
74
- const operation = isMutation ? "mutation" : "query";
75
- // @ts-expect-error ignore readonly
76
- definition.operation = operation;
77
74
  const values = [];
78
75
  const typeInfo = new graphql.TypeInfo(schema);
79
- graphql.visit(definition, graphql.visitWithTypeInfo(typeInfo, {
76
+ const isMutation = !!schema.getMutationType()?.getFields()[field.name.value];
77
+ const operation = isMutation ? graphql.OperationTypeNode.MUTATION : graphql.OperationTypeNode.QUERY;
78
+ graphql.visit({
79
+ ...definition,
80
+ operation
81
+ }, graphql.visitWithTypeInfo(typeInfo, {
80
82
  Variable () {
81
83
  values.push(typeInfo.getInputType());
82
84
  }
@@ -99,12 +101,10 @@ var index = helperPluginUtils.declare((_, { schema, development, queries = queri
99
101
  throw path.buildCodeFrameError(errors[0].message);
100
102
  }
101
103
  query = graphql.stripIgnoredCharacters(query);
102
- if (!development && !queries.includes(query)) {
103
- queries.push(query);
104
- }
105
- const id = development ? query : hash(query);
104
+ const key = hash(query);
105
+ queries[key] = documentNode;
106
106
  const properties = [
107
- core.types.objectProperty(core.types.identifier("query"), core.types.stringLiteral(id))
107
+ core.types.objectProperty(core.types.identifier("query"), core.types.stringLiteral(key))
108
108
  ];
109
109
  if (expressions.length) {
110
110
  properties.push(core.types.objectProperty(core.types.identifier("variables"), core.types.objectExpression(expressions.map((expression, i)=>core.types.objectProperty(core.types.identifier(`_${i}`), expression)))));
package/dist/index.d.ts CHANGED
@@ -1,11 +1,12 @@
1
1
  import * as _babel_core from '@babel/core';
2
2
  import { PluginObj } from '@babel/core';
3
3
  import { GraphQLSchema } from 'graphql';
4
+ import { Queries } from './queries.js';
4
5
 
5
6
  type Options = {
6
7
  schema: GraphQLSchema;
7
8
  development?: boolean;
8
- queries?: string[];
9
+ queries?: Queries;
9
10
  };
10
11
  declare const _default: (api: object, options: Options | null | undefined, dirname: string) => PluginObj<_babel_core.PluginPass>;
11
12
 
package/dist/index.js CHANGED
@@ -3,19 +3,17 @@ import { dirname, join } from 'node:path';
3
3
  import { fileURLToPath } from 'node:url';
4
4
  import { types } from '@babel/core';
5
5
  import { declare } from '@babel/helper-plugin-utils';
6
- import basex from 'base-x';
7
- import { parse, TypeInfo, visit, visitWithTypeInfo, validate, stripIgnoredCharacters } from 'graphql';
6
+ import { parse, TypeInfo, OperationTypeNode, visit, visitWithTypeInfo, validate, stripIgnoredCharacters } from 'graphql';
7
+ import { queries } from './queries.js';
8
8
 
9
- const queriesCache = [];
10
9
  const queriesDir = dirname(fileURLToPath(import.meta.url));
11
10
  const queriesPaths = [
12
11
  "js",
13
12
  "cjs",
14
13
  "ts"
15
14
  ].map((extname)=>join(queriesDir, `queries.${extname}`));
16
- const base52 = basex("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
17
- const hash = (data)=>base52.encode(createHash("sha256").update(data).digest());
18
- var index = declare((_, { schema, development, queries = queriesCache })=>{
15
+ const hash = (data)=>createHash("sha256").update(data).digest("base64url");
16
+ var index = declare((_api, { schema, development, queries: queries$1 = queries })=>{
19
17
  return {
20
18
  name: "babel-plugin-graphql-tagged-template",
21
19
  visitor: {
@@ -23,14 +21,17 @@ var index = declare((_, { schema, development, queries = queriesCache })=>{
23
21
  VariableDeclarator (path, { filename = "" }) {
24
22
  if (!(queriesPaths.includes(filename) && path.get("id").isIdentifier({
25
23
  name: "queries"
26
- }) && path.get("init").isObjectExpression())) {
24
+ }))) {
27
25
  return;
28
26
  }
29
- path.get("init").replaceWith(types.objectExpression(queries.sort().map((query)=>types.objectProperty(types.identifier(hash(query)), types.callExpression(types.memberExpression(types.identifier("JSON"), types.identifier("parse")), [
30
- types.stringLiteral(JSON.stringify(parse(query, {
31
- noLocation: true
32
- })))
33
- ])))));
27
+ path.get("init").replaceWith(types.callExpression(types.memberExpression(types.identifier("Object"), types.identifier("assign")), [
28
+ types.callExpression(types.memberExpression(types.identifier("Object"), types.identifier("create")), [
29
+ types.nullLiteral()
30
+ ]),
31
+ types.objectExpression(Object.entries(queries$1).sort(([a], [b])=>a.localeCompare(b)).map(([key, documentNode])=>types.objectProperty(types.stringLiteral(key), types.callExpression(types.memberExpression(types.identifier("JSON"), types.identifier("parse")), [
32
+ types.stringLiteral(JSON.stringify(documentNode))
33
+ ]))))
34
+ ]));
34
35
  }
35
36
  },
36
37
  TaggedTemplateExpression (path) {
@@ -67,13 +68,14 @@ var index = declare((_, { schema, development, queries = queriesCache })=>{
67
68
  if (field.kind !== "Field") {
68
69
  throw path.buildCodeFrameError("Only a field is allowed");
69
70
  }
70
- const isMutation = !!schema.getMutationType()?.getFields()[field.name.value];
71
- const operation = isMutation ? "mutation" : "query";
72
- // @ts-expect-error ignore readonly
73
- definition.operation = operation;
74
71
  const values = [];
75
72
  const typeInfo = new TypeInfo(schema);
76
- visit(definition, visitWithTypeInfo(typeInfo, {
73
+ const isMutation = !!schema.getMutationType()?.getFields()[field.name.value];
74
+ const operation = isMutation ? OperationTypeNode.MUTATION : OperationTypeNode.QUERY;
75
+ visit({
76
+ ...definition,
77
+ operation
78
+ }, visitWithTypeInfo(typeInfo, {
77
79
  Variable () {
78
80
  values.push(typeInfo.getInputType());
79
81
  }
@@ -96,12 +98,10 @@ var index = declare((_, { schema, development, queries = queriesCache })=>{
96
98
  throw path.buildCodeFrameError(errors[0].message);
97
99
  }
98
100
  query = stripIgnoredCharacters(query);
99
- if (!development && !queries.includes(query)) {
100
- queries.push(query);
101
- }
102
- const id = development ? query : hash(query);
101
+ const key = hash(query);
102
+ queries$1[key] = documentNode;
103
103
  const properties = [
104
- types.objectProperty(types.identifier("query"), types.stringLiteral(id))
104
+ types.objectProperty(types.identifier("query"), types.stringLiteral(key))
105
105
  ];
106
106
  if (expressions.length) {
107
107
  properties.push(types.objectProperty(types.identifier("variables"), types.objectExpression(expressions.map((expression, i)=>types.objectProperty(types.identifier(`_${i}`), expression)))));
package/dist/queries.cjs CHANGED
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
2
 
3
- const queries = {};
3
+ var _globalThis;
4
+ // @ts-expect-error queries is set to a value by Babel
5
+ const queries = (_globalThis = globalThis).__GRAPHQL_QUERIES__ ?? (_globalThis.__GRAPHQL_QUERIES__ = Object.create(null));
4
6
 
5
7
  exports.queries = queries;
package/dist/queries.d.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  import { DocumentNode } from 'graphql';
2
2
 
3
- declare const queries: {
3
+ type Queries = {
4
4
  [key: string]: DocumentNode;
5
5
  };
6
+ declare const queries: Queries;
6
7
 
7
- export { queries };
8
+ export { type Queries, queries };
package/dist/queries.js CHANGED
@@ -1,3 +1,5 @@
1
- const queries = {};
1
+ var _globalThis;
2
+ // @ts-expect-error queries is set to a value by Babel
3
+ const queries = (_globalThis = globalThis).__GRAPHQL_QUERIES__ ?? (_globalThis.__GRAPHQL_QUERIES__ = Object.create(null));
2
4
 
3
5
  export { queries };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mo36924/babel-plugin-graphql-tagged-template",
3
3
  "type": "module",
4
- "version": "5.1.14",
4
+ "version": "5.3.3",
5
5
  "description": "babel-plugin-graphql-tagged-template",
6
6
  "author": "mo36924",
7
7
  "license": "MIT",
@@ -51,8 +51,7 @@
51
51
  "@babel/helper-plugin-utils": "^7.25.9",
52
52
  "@types/babel__core": "^7.20.5",
53
53
  "@types/babel__helper-plugin-utils": "^7.10.3",
54
- "base-x": "^5.0.0",
55
54
  "graphql": "^16.9.0"
56
55
  },
57
- "gitHead": "39dbb35229f5ea671f0c356000d63b81eba59f92"
56
+ "gitHead": "2398045d669322b77121c2ef7522538fe790979e"
58
57
  }