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

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
+ ...development ? {} : {
26
+ VariableDeclarator (path, { filename = "" }) {
27
+ if (!(queriesPaths.includes(filename) && path.get("id").isIdentifier({
28
+ name: "queries"
29
+ }) && path.get("init").isObjectExpression())) {
30
+ return;
31
+ }
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
+ ])))));
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,18 @@ 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
+ if (!development && !queries.includes(query)) {
103
+ queries.push(query);
104
+ }
105
+ const id = development ? query : hash(query);
73
106
  const properties = [
74
- core.types.objectProperty(core.types.identifier("query"), core.types.stringLiteral(graphql.stripIgnoredCharacters(query)))
107
+ core.types.objectProperty(core.types.identifier("query"), core.types.stringLiteral(id))
75
108
  ];
76
109
  if (expressions.length) {
77
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)))));
78
111
  }
79
- path.replaceWith(core.types.callExpression(core.types.identifier(name), [
80
- core.types.objectExpression(properties)
81
- ]));
112
+ path.replaceWith(core.types.objectExpression(properties));
82
113
  }
83
114
  }
84
115
  };
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
+ ...development ? {} : {
23
+ VariableDeclarator (path, { filename = "" }) {
24
+ if (!(queriesPaths.includes(filename) && path.get("id").isIdentifier({
25
+ name: "queries"
26
+ }) && path.get("init").isObjectExpression())) {
27
+ return;
28
+ }
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
+ ])))));
34
+ }
35
+ },
9
36
  TaggedTemplateExpression (path) {
10
37
  const { tag, quasi: { quasis, expressions } } = path.node;
11
38
  if (!types.isIdentifier(tag)) {
@@ -68,15 +95,18 @@ var index = declare((_, { schema })=>{
68
95
  if (errors.length) {
69
96
  throw path.buildCodeFrameError(errors[0].message);
70
97
  }
98
+ query = stripIgnoredCharacters(query);
99
+ if (!development && !queries.includes(query)) {
100
+ queries.push(query);
101
+ }
102
+ const id = development ? query : hash(query);
71
103
  const properties = [
72
- types.objectProperty(types.identifier("query"), types.stringLiteral(stripIgnoredCharacters(query)))
104
+ types.objectProperty(types.identifier("query"), types.stringLiteral(id))
73
105
  ];
74
106
  if (expressions.length) {
75
107
  properties.push(types.objectProperty(types.identifier("variables"), types.objectExpression(expressions.map((expression, i)=>types.objectProperty(types.identifier(`_${i}`), expression)))));
76
108
  }
77
- path.replaceWith(types.callExpression(types.identifier(name), [
78
- types.objectExpression(properties)
79
- ]));
109
+ path.replaceWith(types.objectExpression(properties));
80
110
  }
81
111
  }
82
112
  };
@@ -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.62",
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": "6d8712d1c44da9b6efd2b9aa2409ab1d9de6f91c"
51
58
  }