@mo36924/babel-plugin-graphql-tagged-template 5.0.47 → 5.0.56

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.cjs CHANGED
@@ -1,13 +1,41 @@
1
1
  'use strict';
2
2
 
3
+ var node_crypto = require('node:crypto');
4
+ var node_path = require('node:path');
5
+ var node_url = require('node:url');
3
6
  var core = require('@babel/core');
4
7
  var helperPluginUtils = require('@babel/helper-plugin-utils');
8
+ var basex = require('base-x');
5
9
  var graphql = require('graphql');
6
10
 
7
- var index = helperPluginUtils.declare((_, { schema })=>{
11
+ var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
12
+ const queriesCache = [];
13
+ 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
+ const queriesPaths = [
15
+ "js",
16
+ "cjs",
17
+ "ts"
18
+ ].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 })=>{
8
22
  return {
9
23
  name: "babel-plugin-graphql-tagged-template",
10
24
  visitor: {
25
+ VariableDeclarator (path, { filename = "" }) {
26
+ if (!(queriesPaths.includes(filename) && path.get("id").isIdentifier({
27
+ name: "queries"
28
+ }) && path.get("init").isObjectExpression())) {
29
+ return;
30
+ }
31
+ path.get("init").replaceWith(core.types.objectExpression([
32
+ ...new Set(queries)
33
+ ].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")), [
34
+ core.types.stringLiteral(JSON.stringify(graphql.parse(query, {
35
+ noLocation: true
36
+ })))
37
+ ])))));
38
+ },
11
39
  TaggedTemplateExpression (path) {
12
40
  const { tag, quasi: { quasis, expressions } } = path.node;
13
41
  if (!core.types.isIdentifier(tag)) {
@@ -70,15 +98,16 @@ var index = helperPluginUtils.declare((_, { schema })=>{
70
98
  if (errors.length) {
71
99
  throw path.buildCodeFrameError(errors[0].message);
72
100
  }
101
+ query = graphql.stripIgnoredCharacters(query);
102
+ queries.push(query);
103
+ const id = development ? query : hash(query);
73
104
  const properties = [
74
- core.types.objectProperty(core.types.identifier("query"), core.types.stringLiteral(graphql.stripIgnoredCharacters(query)))
105
+ core.types.objectProperty(core.types.identifier("query"), core.types.stringLiteral(id))
75
106
  ];
76
107
  if (expressions.length) {
77
108
  properties.push(core.types.objectProperty(core.types.identifier("variables"), core.types.objectExpression(expressions.map((expression, i)=>core.types.objectProperty(core.types.identifier(`_${i}`), expression)))));
78
109
  }
79
- path.replaceWith(core.types.callExpression(core.types.identifier(name), [
80
- core.types.objectExpression(properties)
81
- ]));
110
+ path.replaceWith(core.types.objectExpression(properties));
82
111
  }
83
112
  }
84
113
  };
package/dist/index.d.ts CHANGED
@@ -1,9 +1,12 @@
1
1
  import * as _babel_core from '@babel/core';
2
+ import { PluginObj } from '@babel/core';
2
3
  import { GraphQLSchema } from 'graphql';
3
4
 
4
5
  type Options = {
5
6
  schema: GraphQLSchema;
7
+ development?: boolean;
8
+ queries?: string[];
6
9
  };
7
- declare const _default: (api: object, options: Options | null | undefined, dirname: string) => _babel_core.PluginObj<_babel_core.PluginPass>;
10
+ declare const _default: (api: object, options: Options | null | undefined, dirname: string) => PluginObj<_babel_core.PluginPass>;
8
11
 
9
12
  export { type Options, _default as default };
package/dist/index.js CHANGED
@@ -1,11 +1,38 @@
1
+ import { createHash } from 'node:crypto';
2
+ import { dirname, join } from 'node:path';
3
+ import { fileURLToPath } from 'node:url';
1
4
  import { types } from '@babel/core';
2
5
  import { declare } from '@babel/helper-plugin-utils';
6
+ import basex from 'base-x';
3
7
  import { parse, TypeInfo, visit, visitWithTypeInfo, validate, stripIgnoredCharacters } from 'graphql';
4
8
 
5
- var index = declare((_, { schema })=>{
9
+ const queriesCache = [];
10
+ const queriesDir = dirname(fileURLToPath(import.meta.url));
11
+ const queriesPaths = [
12
+ "js",
13
+ "cjs",
14
+ "ts"
15
+ ].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 })=>{
6
19
  return {
7
20
  name: "babel-plugin-graphql-tagged-template",
8
21
  visitor: {
22
+ VariableDeclarator (path, { filename = "" }) {
23
+ if (!(queriesPaths.includes(filename) && path.get("id").isIdentifier({
24
+ name: "queries"
25
+ }) && path.get("init").isObjectExpression())) {
26
+ return;
27
+ }
28
+ path.get("init").replaceWith(types.objectExpression([
29
+ ...new Set(queries)
30
+ ].sort().map((query)=>types.objectProperty(types.identifier(hash(query)), types.callExpression(types.memberExpression(types.identifier("JSON"), types.identifier("parse")), [
31
+ types.stringLiteral(JSON.stringify(parse(query, {
32
+ noLocation: true
33
+ })))
34
+ ])))));
35
+ },
9
36
  TaggedTemplateExpression (path) {
10
37
  const { tag, quasi: { quasis, expressions } } = path.node;
11
38
  if (!types.isIdentifier(tag)) {
@@ -68,15 +95,16 @@ var index = declare((_, { schema })=>{
68
95
  if (errors.length) {
69
96
  throw path.buildCodeFrameError(errors[0].message);
70
97
  }
98
+ query = stripIgnoredCharacters(query);
99
+ queries.push(query);
100
+ const id = development ? query : hash(query);
71
101
  const properties = [
72
- types.objectProperty(types.identifier("query"), types.stringLiteral(stripIgnoredCharacters(query)))
102
+ types.objectProperty(types.identifier("query"), types.stringLiteral(id))
73
103
  ];
74
104
  if (expressions.length) {
75
105
  properties.push(types.objectProperty(types.identifier("variables"), types.objectExpression(expressions.map((expression, i)=>types.objectProperty(types.identifier(`_${i}`), expression)))));
76
106
  }
77
- path.replaceWith(types.callExpression(types.identifier(name), [
78
- types.objectExpression(properties)
79
- ]));
107
+ path.replaceWith(types.objectExpression(properties));
80
108
  }
81
109
  }
82
110
  };
@@ -0,0 +1,5 @@
1
+ 'use strict';
2
+
3
+ const queries = {};
4
+
5
+ exports.queries = queries;
@@ -0,0 +1,7 @@
1
+ import { DocumentNode } from 'graphql';
2
+
3
+ declare const queries: {
4
+ [key: string]: DocumentNode;
5
+ };
6
+
7
+ export { queries };
@@ -0,0 +1,3 @@
1
+ const queries = {};
2
+
3
+ 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.0.47",
4
+ "version": "5.0.56",
5
5
  "description": "babel-plugin-graphql-tagged-template",
6
6
  "author": "mo36924",
7
7
  "license": "MIT",
@@ -21,6 +21,12 @@
21
21
  "import": "./dist/index.js",
22
22
  "require": "./dist/index.cjs",
23
23
  "default": "./dist/index.js"
24
+ },
25
+ "./queries": {
26
+ "types": "./dist/queries.d.ts",
27
+ "import": "./dist/queries.js",
28
+ "require": "./dist/queries.cjs",
29
+ "default": "./dist/queries.js"
24
30
  }
25
31
  },
26
32
  "main": "./dist/index.js",
@@ -45,7 +51,8 @@
45
51
  "@babel/helper-plugin-utils": "^7.24.8",
46
52
  "@types/babel__core": "^7.20.5",
47
53
  "@types/babel__helper-plugin-utils": "^7.10.3",
54
+ "base-x": "^5.0.0",
48
55
  "graphql": "^16.9.0"
49
56
  },
50
- "gitHead": "e8a97dd132ef7998c0ef5d305d3b8d93c983ff32"
57
+ "gitHead": "25d1f49663a624870a44bbc9ca3aa78bea0f06b6"
51
58
  }