babel-plugin-relay 14.1.0 → 16.0.0
Sign up to get free protection for your applications and to get access to all the features.
- package/BabelPluginRelay.js.flow +1 -0
- package/BabelPluginRelay.macro.js.flow +1 -0
- package/babel-plugin-relay.js +2 -2
- package/babel-plugin-relay.min.js +2 -2
- package/compileGraphQLTag.js.flow +19 -11
- package/getValidGraphQLTag.js.flow +1 -0
- package/index.js +1 -1
- package/lib/BabelPluginRelay.js +0 -29
- package/lib/BabelPluginRelay.macro.js +4 -19
- package/lib/compileGraphQLTag.js +15 -60
- package/lib/getValidGraphQLTag.js +0 -23
- package/macro.js +1 -1
- package/package.json +4 -3
package/BabelPluginRelay.js.flow
CHANGED
package/babel-plugin-relay.js
CHANGED
@@ -1,4 +1,4 @@
|
|
1
1
|
/**
|
2
|
-
* Relay
|
2
|
+
* Relay v16.0.0
|
3
3
|
*/
|
4
|
-
module.exports=function(e){var r={};function n(t){if(r[t])return r[t].exports;var i=r[t]={i:t,l:!1,exports:{}};return e[t].call(i.exports,i,i.exports,n),i.l=!0,i.exports}return n.m=e,n.c=r,n.d=function(e,r,t){n.o(e,r)||Object.defineProperty(e,r,{enumerable:!0,get:t})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,r){if(1&r&&(e=n(e)),8&r)return e;if(4&r&&"object"==typeof e&&e&&e.__esModule)return e;var t=Object.create(null);if(n.r(t),Object.defineProperty(t,"default",{enumerable:!0,value:e}),2&r&&"string"!=typeof e)for(var i in e)n.d(t,i,function(r){return e[r]}.bind(null,i));return t},n.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(r,"a",r),r},n.o=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)},n.p="",n(n.s=1)}([function(e,r){e.exports=require("graphql")},function(e,r,n){"use strict";var t,i=(0,n(2).default)(n(3)),o=n(4),a=n(7),
|
4
|
+
module.exports=function(e){var r={};function n(t){if(r[t])return r[t].exports;var i=r[t]={i:t,l:!1,exports:{}};return e[t].call(i.exports,i,i.exports,n),i.l=!0,i.exports}return n.m=e,n.c=r,n.d=function(e,r,t){n.o(e,r)||Object.defineProperty(e,r,{enumerable:!0,get:t})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,r){if(1&r&&(e=n(e)),8&r)return e;if(4&r&&"object"==typeof e&&e&&e.__esModule)return e;var t=Object.create(null);if(n.r(t),Object.defineProperty(t,"default",{enumerable:!0,value:e}),2&r&&"string"!=typeof e)for(var i in e)n.d(t,i,function(r){return e[r]}.bind(null,i));return t},n.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(r,"a",r),r},n.o=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)},n.p="",n(n.s=1)}([function(e,r){e.exports=require("graphql")},function(e,r,n){"use strict";var t,i=(0,n(2).default)(n(3)),o=n(4),a=n(7),l=n(8),s=l("relay",{searchPlaces:["relay.config.js","relay.config.json","package.json"],loaders:{".json":l.loadJson,".js":l.loadJs,noExt:l.loadYaml}}).searchSync();s&&(t=s.config),e.exports=function(e){var r=e.types;if(!r)throw new Error('BabelPluginRelay: Expected plugin context to include "types", but got:'+String(e));var n={TaggedTemplateExpression:function(e,n){var t=a(e);t&&o(r,e,n,t)}};return{visitor:{Program:function(e,r){e.traverse(n,(0,i.default)((0,i.default)({},r),{},{opts:(0,i.default)((0,i.default)({},t),r.opts)}))}}}}},function(e,r){e.exports=require("@babel/runtime/helpers/interopRequireDefault")},function(e,r){e.exports=require("@babel/runtime/helpers/objectSpread2")},function(e,r,n){"use strict";var t=n(5),i=n(0).print,o=n(6),a=o.dirname,l=o.join,s=o.relative,u=o.resolve;e.exports=function(e,r,n,o){var d,c,p,f,g,v,m;if(1!==o.definitions.length)throw new Error("BabelPluginRelay: Expected exactly one definition per graphql tag.");var x=o.definitions[0];if("FragmentDefinition"!==x.kind&&"OperationDefinition"!==x.kind)throw new Error("BabelPluginRelay: Expected a fragment, mutation, query, or subscription, got `"+x.kind+"`.");var h=null!==(d=null===(c=n.opts)||void 0===c?void 0:c.eagerEsModules)&&void 0!==d&&d,b="haste"===(null===(p=n.opts)||void 0===p?void 0:p.jsModuleFormat),y=null===(f=n.opts)||void 0===f?void 0:f.isDevVariableName;return function(e,r,n,o,d){var c=o.name&&o.name.value;if(!c)throw new Error("GraphQL operations and fragments must contain names");var p=c+".graphql",f=d.isHasteMode?p:d.artifactDirectory?function(e,r,n){if(null==e.file)throw new Error("Babel state is missing expected file name");var t=e.file.opts.filename,i=s(a(t),u(r));return(0!==i.length&&i.startsWith(".")?"":"./")+l(i,n)}(r,d.artifactDirectory,p):"./__generated__/"+p,g=t.createHash("md5").update(i(o),"utf8").digest("hex"),v=n.scope;for(;v.parent;)v=v.parent;var m=v.generateUidIdentifier(c),x=e.MemberExpression(e.cloneNode(m),e.Identifier("hash")),h=function(e,r,n){return e.callExpression(e.memberExpression(e.identifier("console"),e.identifier("error")),[e.stringLiteral("The definition of '".concat(r,"' appears to have changed. Run ")+"`"+n+"` to update the generated files to receive the expected data.")])}(e,c,d.buildCommand),b=e.LogicalExpression("&&",e.cloneNode(x),e.LogicalExpression("&&",e.BinaryExpression("!==",x,e.StringLiteral(g)),h));if(d.eagerEsModules){var y=e.ImportDeclaration([e.ImportDefaultSpecifier(m)],e.StringLiteral(f));n.findParent((function(e){return e.isProgram()})).unshiftContainer("body",y);var E,q=e.SequenceExpression([b,e.cloneNode(m)]);E=null!=d.isDevVariable?e.ConditionalExpression(e.Identifier(d.isDevVariable),q,e.cloneNode(m)):d.isDevelopment?q:e.cloneNode(m),n.replaceWith(E)}else{v.push({id:e.cloneNode(m)});var D,w=e.CallExpression(e.Identifier("require"),[e.StringLiteral(f)]),j=e.AssignmentExpression("=",e.cloneNode(m),w),N=e.SequenceExpression([j,b,e.cloneNode(m)]);D=null!=d.isDevVariable?e.ConditionalExpression(e.Identifier(d.isDevVariable),N,j):d.isDevelopment?N:j;var S=e.UnaryExpression("void",e.NumericLiteral(0));n.replaceWith(e.ConditionalExpression(e.BinaryExpression("!==",e.cloneNode(m),S),e.cloneNode(m),e.cloneNode(D)))}}(e,n,r,x,{artifactDirectory:null===(g=n.opts)||void 0===g?void 0:g.artifactDirectory,eagerEsModules:h,buildCommand:null!==(v=null===(m=n.opts)||void 0===m?void 0:m.codegenCommand)&&void 0!==v?v:"relay-compiler",isDevelopment:"production"!==(process.env.BABEL_ENV||"development"),isHasteMode:b,isDevVariable:y})}},function(e,r){e.exports=require("crypto")},function(e,r){e.exports=require("path")},function(e,r,n){"use strict";var t=n(0);e.exports=function(e){if(!e.get("tag").isIdentifier({name:"graphql"}))return null;var r=e.node.quasi.quasis;if(1!==r.length)throw new Error("BabelPluginRelay: Substitutions are not allowed in graphql fragments. Included fragments should be referenced as `...MyModule_propName`.");var n=r[0].value.raw,i=t.parse(n);if(0===i.definitions.length)throw new Error("BabelPluginRelay: Unexpected empty graphql tag.");return i}},function(e,r){e.exports=require("cosmiconfig")}]);
|
@@ -1,9 +1,9 @@
|
|
1
1
|
/**
|
2
|
-
* Relay
|
2
|
+
* Relay v16.0.0
|
3
3
|
*
|
4
4
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
5
5
|
*
|
6
6
|
* This source code is licensed under the MIT license found in the
|
7
7
|
* LICENSE file in the root directory of this source tree.
|
8
8
|
*/
|
9
|
-
module.exports=function(e){var r={};function n(t){if(r[t])return r[t].exports;var i=r[t]={i:t,l:!1,exports:{}};return e[t].call(i.exports,i,i.exports,n),i.l=!0,i.exports}return n.m=e,n.c=r,n.d=function(e,r,t){n.o(e,r)||Object.defineProperty(e,r,{enumerable:!0,get:t})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,r){if(1&r&&(e=n(e)),8&r)return e;if(4&r&&"object"==typeof e&&e&&e.__esModule)return e;var t=Object.create(null);if(n.r(t),Object.defineProperty(t,"default",{enumerable:!0,value:e}),2&r&&"string"!=typeof e)for(var i in e)n.d(t,i,function(r){return e[r]}.bind(null,i));return t},n.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(r,"a",r),r},n.o=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)},n.p="",n(n.s=1)}([function(e,r){e.exports=require("graphql")},function(e,r,n){"use strict";var t,i=(0,n(2).default)(n(3)),o=n(4),a=n(7),
|
9
|
+
module.exports=function(e){var r={};function n(t){if(r[t])return r[t].exports;var i=r[t]={i:t,l:!1,exports:{}};return e[t].call(i.exports,i,i.exports,n),i.l=!0,i.exports}return n.m=e,n.c=r,n.d=function(e,r,t){n.o(e,r)||Object.defineProperty(e,r,{enumerable:!0,get:t})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,r){if(1&r&&(e=n(e)),8&r)return e;if(4&r&&"object"==typeof e&&e&&e.__esModule)return e;var t=Object.create(null);if(n.r(t),Object.defineProperty(t,"default",{enumerable:!0,value:e}),2&r&&"string"!=typeof e)for(var i in e)n.d(t,i,function(r){return e[r]}.bind(null,i));return t},n.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(r,"a",r),r},n.o=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)},n.p="",n(n.s=1)}([function(e,r){e.exports=require("graphql")},function(e,r,n){"use strict";var t,i=(0,n(2).default)(n(3)),o=n(4),a=n(7),l=n(8),s=l("relay",{searchPlaces:["relay.config.js","relay.config.json","package.json"],loaders:{".json":l.loadJson,".js":l.loadJs,noExt:l.loadYaml}}).searchSync();s&&(t=s.config),e.exports=function(e){var r=e.types;if(!r)throw new Error('BabelPluginRelay: Expected plugin context to include "types", but got:'+String(e));var n={TaggedTemplateExpression:function(e,n){var t=a(e);t&&o(r,e,n,t)}};return{visitor:{Program:function(e,r){e.traverse(n,(0,i.default)((0,i.default)({},r),{},{opts:(0,i.default)((0,i.default)({},t),r.opts)}))}}}}},function(e,r){e.exports=require("@babel/runtime/helpers/interopRequireDefault")},function(e,r){e.exports=require("@babel/runtime/helpers/objectSpread2")},function(e,r,n){"use strict";var t=n(5),i=n(0).print,o=n(6),a=o.dirname,l=o.join,s=o.relative,u=o.resolve;e.exports=function(e,r,n,o){var d,c,p,f,g,v,m;if(1!==o.definitions.length)throw new Error("BabelPluginRelay: Expected exactly one definition per graphql tag.");var x=o.definitions[0];if("FragmentDefinition"!==x.kind&&"OperationDefinition"!==x.kind)throw new Error("BabelPluginRelay: Expected a fragment, mutation, query, or subscription, got `"+x.kind+"`.");var h=null!==(d=null===(c=n.opts)||void 0===c?void 0:c.eagerEsModules)&&void 0!==d&&d,b="haste"===(null===(p=n.opts)||void 0===p?void 0:p.jsModuleFormat),y=null===(f=n.opts)||void 0===f?void 0:f.isDevVariableName;return function(e,r,n,o,d){var c=o.name&&o.name.value;if(!c)throw new Error("GraphQL operations and fragments must contain names");var p=c+".graphql",f=d.isHasteMode?p:d.artifactDirectory?function(e,r,n){if(null==e.file)throw new Error("Babel state is missing expected file name");var t=e.file.opts.filename,i=s(a(t),u(r));return(0!==i.length&&i.startsWith(".")?"":"./")+l(i,n)}(r,d.artifactDirectory,p):"./__generated__/"+p,g=t.createHash("md5").update(i(o),"utf8").digest("hex"),v=n.scope;for(;v.parent;)v=v.parent;var m=v.generateUidIdentifier(c),x=e.MemberExpression(e.cloneNode(m),e.Identifier("hash")),h=function(e,r,n){return e.callExpression(e.memberExpression(e.identifier("console"),e.identifier("error")),[e.stringLiteral("The definition of '".concat(r,"' appears to have changed. Run ")+"`"+n+"` to update the generated files to receive the expected data.")])}(e,c,d.buildCommand),b=e.LogicalExpression("&&",e.cloneNode(x),e.LogicalExpression("&&",e.BinaryExpression("!==",x,e.StringLiteral(g)),h));if(d.eagerEsModules){var y=e.ImportDeclaration([e.ImportDefaultSpecifier(m)],e.StringLiteral(f));n.findParent((function(e){return e.isProgram()})).unshiftContainer("body",y);var E,q=e.SequenceExpression([b,e.cloneNode(m)]);E=null!=d.isDevVariable?e.ConditionalExpression(e.Identifier(d.isDevVariable),q,e.cloneNode(m)):d.isDevelopment?q:e.cloneNode(m),n.replaceWith(E)}else{v.push({id:e.cloneNode(m)});var D,w=e.CallExpression(e.Identifier("require"),[e.StringLiteral(f)]),j=e.AssignmentExpression("=",e.cloneNode(m),w),N=e.SequenceExpression([j,b,e.cloneNode(m)]);D=null!=d.isDevVariable?e.ConditionalExpression(e.Identifier(d.isDevVariable),N,j):d.isDevelopment?N:j;var S=e.UnaryExpression("void",e.NumericLiteral(0));n.replaceWith(e.ConditionalExpression(e.BinaryExpression("!==",e.cloneNode(m),S),e.cloneNode(m),e.cloneNode(D)))}}(e,n,r,x,{artifactDirectory:null===(g=n.opts)||void 0===g?void 0:g.artifactDirectory,eagerEsModules:h,buildCommand:null!==(v=null===(m=n.opts)||void 0===m?void 0:m.codegenCommand)&&void 0!==v?v:"relay-compiler",isDevelopment:"production"!==(process.env.BABEL_ENV||"production"),isHasteMode:b,isDevVariable:y})}},function(e,r){e.exports=require("crypto")},function(e,r){e.exports=require("path")},function(e,r,n){"use strict";var t=n(0);e.exports=function(e){if(!e.get("tag").isIdentifier({name:"graphql"}))return null;var r=e.node.quasi.quasis;if(1!==r.length)throw new Error("BabelPluginRelay: Substitutions are not allowed in graphql fragments. Included fragments should be referenced as `...MyModule_propName`.");var n=r[0].value.raw,i=t.parse(n);if(0===i.definitions.length)throw new Error("BabelPluginRelay: Unexpected empty graphql tag.");return i}},function(e,r){e.exports=require("cosmiconfig")}]);
|
@@ -6,6 +6,7 @@
|
|
6
6
|
*
|
7
7
|
* @flow
|
8
8
|
* @format
|
9
|
+
* @oncall relay
|
9
10
|
*/
|
10
11
|
|
11
12
|
'use strict';
|
@@ -124,11 +125,11 @@ function createNode(
|
|
124
125
|
|
125
126
|
const id = topScope.generateUidIdentifier(definitionName);
|
126
127
|
|
127
|
-
const expHash = t.MemberExpression(id, t.Identifier('hash'));
|
128
|
+
const expHash = t.MemberExpression(t.cloneNode(id), t.Identifier('hash'));
|
128
129
|
const expWarn = warnNeedsRebuild(t, definitionName, options.buildCommand);
|
129
130
|
const expWarnIfOutdated = t.LogicalExpression(
|
130
131
|
'&&',
|
131
|
-
expHash,
|
132
|
+
t.cloneNode(expHash),
|
132
133
|
t.LogicalExpression(
|
133
134
|
'&&',
|
134
135
|
t.BinaryExpression('!==', expHash, t.StringLiteral(hash)),
|
@@ -144,34 +145,41 @@ function createNode(
|
|
144
145
|
const program = path.findParent(parent => parent.isProgram());
|
145
146
|
program.unshiftContainer('body', importDeclaration);
|
146
147
|
|
147
|
-
const expAssignAndCheck = t.SequenceExpression([
|
148
|
+
const expAssignAndCheck = t.SequenceExpression([
|
149
|
+
expWarnIfOutdated,
|
150
|
+
t.cloneNode(id),
|
151
|
+
]);
|
148
152
|
|
149
153
|
let expAssign;
|
150
154
|
if (options.isDevVariable != null) {
|
151
155
|
expAssign = t.ConditionalExpression(
|
152
156
|
t.Identifier(options.isDevVariable),
|
153
157
|
expAssignAndCheck,
|
154
|
-
id,
|
158
|
+
t.cloneNode(id),
|
155
159
|
);
|
156
160
|
} else if (options.isDevelopment) {
|
157
161
|
expAssign = expAssignAndCheck;
|
158
162
|
} else {
|
159
|
-
expAssign = id;
|
163
|
+
expAssign = t.cloneNode(id);
|
160
164
|
}
|
161
165
|
|
162
166
|
path.replaceWith(expAssign);
|
163
167
|
} else {
|
164
|
-
topScope.push({id});
|
168
|
+
topScope.push({id: t.cloneNode(id)});
|
165
169
|
|
166
170
|
const requireGraphQLModule = t.CallExpression(t.Identifier('require'), [
|
167
171
|
t.StringLiteral(requiredPath),
|
168
172
|
]);
|
169
173
|
|
170
|
-
const expAssignProd = t.AssignmentExpression(
|
174
|
+
const expAssignProd = t.AssignmentExpression(
|
175
|
+
'=',
|
176
|
+
t.cloneNode(id),
|
177
|
+
requireGraphQLModule,
|
178
|
+
);
|
171
179
|
const expAssignAndCheck = t.SequenceExpression([
|
172
180
|
expAssignProd,
|
173
181
|
expWarnIfOutdated,
|
174
|
-
id,
|
182
|
+
t.cloneNode(id),
|
175
183
|
]);
|
176
184
|
|
177
185
|
let expAssign;
|
@@ -190,9 +198,9 @@ function createNode(
|
|
190
198
|
const expVoid0 = t.UnaryExpression('void', t.NumericLiteral(0));
|
191
199
|
path.replaceWith(
|
192
200
|
t.ConditionalExpression(
|
193
|
-
t.BinaryExpression('!==', id, expVoid0),
|
194
|
-
id,
|
195
|
-
expAssign,
|
201
|
+
t.BinaryExpression('!==', t.cloneNode(id), expVoid0),
|
202
|
+
t.cloneNode(id),
|
203
|
+
t.cloneNode(expAssign),
|
196
204
|
),
|
197
205
|
);
|
198
206
|
}
|
package/index.js
CHANGED
package/lib/BabelPluginRelay.js
CHANGED
@@ -1,24 +1,10 @@
|
|
1
|
-
/**
|
2
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
3
|
-
*
|
4
|
-
* This source code is licensed under the MIT license found in the
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
6
|
-
*
|
7
|
-
*
|
8
|
-
* @format
|
9
|
-
*/
|
10
1
|
'use strict';
|
11
2
|
|
12
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
|
13
|
-
|
14
4
|
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
15
|
-
|
16
5
|
var compileGraphQLTag = require('./compileGraphQLTag');
|
17
|
-
|
18
6
|
var getValidGraphQLTag = require('./getValidGraphQLTag');
|
19
|
-
|
20
7
|
var cosmiconfig = require('cosmiconfig');
|
21
|
-
|
22
8
|
var configExplorer = cosmiconfig('relay', {
|
23
9
|
searchPlaces: ['relay.config.js', 'relay.config.json', 'package.json'],
|
24
10
|
loaders: {
|
@@ -29,32 +15,17 @@ var configExplorer = cosmiconfig('relay', {
|
|
29
15
|
});
|
30
16
|
var RelayConfig;
|
31
17
|
var result = configExplorer.searchSync();
|
32
|
-
|
33
18
|
if (result) {
|
34
19
|
RelayConfig = result.config;
|
35
20
|
}
|
36
|
-
|
37
|
-
/**
|
38
|
-
* Using babel-plugin-relay with only the modern runtime?
|
39
|
-
*
|
40
|
-
* {
|
41
|
-
* plugins: [
|
42
|
-
* "relay"
|
43
|
-
* ]
|
44
|
-
* }
|
45
|
-
*/
|
46
21
|
module.exports = function BabelPluginRelay(context) {
|
47
22
|
var t = context.types;
|
48
|
-
|
49
23
|
if (!t) {
|
50
24
|
throw new Error('BabelPluginRelay: Expected plugin context to include "types", but got:' + String(context));
|
51
25
|
}
|
52
|
-
|
53
26
|
var visitor = {
|
54
27
|
TaggedTemplateExpression: function TaggedTemplateExpression(path, state) {
|
55
|
-
// Convert graphql`` literals
|
56
28
|
var ast = getValidGraphQLTag(path);
|
57
|
-
|
58
29
|
if (ast) {
|
59
30
|
compileGraphQLTag(t, path, state, ast);
|
60
31
|
return;
|
@@ -1,34 +1,20 @@
|
|
1
|
-
/**
|
2
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
3
|
-
*
|
4
|
-
* This source code is licensed under the MIT license found in the
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
6
|
-
*
|
7
|
-
*
|
8
|
-
* @format
|
9
|
-
*/
|
10
1
|
'use strict';
|
11
2
|
|
12
3
|
var compileGraphQLTag = require('./compileGraphQLTag');
|
13
|
-
|
14
4
|
var getValidGraphQLTag = require('./getValidGraphQLTag');
|
15
|
-
|
16
5
|
var _require = require('babel-plugin-macros'),
|
17
|
-
|
18
|
-
|
6
|
+
createMacro = _require.createMacro;
|
19
7
|
var configName = 'relay';
|
20
|
-
|
21
8
|
function BabelPluginRelayMacro(_ref) {
|
22
9
|
var references = _ref.references,
|
23
|
-
|
24
|
-
|
25
|
-
|
10
|
+
state = _ref.state,
|
11
|
+
babel = _ref.babel,
|
12
|
+
config = _ref.config;
|
26
13
|
var t = babel.types;
|
27
14
|
Object.keys(references).forEach(function (referenceKey) {
|
28
15
|
references[referenceKey].forEach(function (reference) {
|
29
16
|
var path = reference.parentPath;
|
30
17
|
var ast = getValidGraphQLTag(path);
|
31
|
-
|
32
18
|
if (ast) {
|
33
19
|
compileGraphQLTag(t, path, Object.assign(state, config ? {
|
34
20
|
opts: config
|
@@ -37,7 +23,6 @@ function BabelPluginRelayMacro(_ref) {
|
|
37
23
|
});
|
38
24
|
});
|
39
25
|
}
|
40
|
-
|
41
26
|
module.exports = createMacro(BabelPluginRelayMacro, {
|
42
27
|
configName: configName
|
43
28
|
});
|
package/lib/compileGraphQLTag.js
CHANGED
@@ -1,50 +1,28 @@
|
|
1
|
-
/**
|
2
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
3
|
-
*
|
4
|
-
* This source code is licensed under the MIT license found in the
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
6
|
-
*
|
7
|
-
*
|
8
|
-
* @format
|
9
|
-
*/
|
10
1
|
'use strict';
|
11
2
|
|
12
3
|
var crypto = require('crypto');
|
13
|
-
|
14
4
|
var _require = require('graphql'),
|
15
|
-
|
16
|
-
|
5
|
+
print = _require.print;
|
17
6
|
var _require2 = require('path'),
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
7
|
+
dirname = _require2.dirname,
|
8
|
+
joinPath = _require2.join,
|
9
|
+
relativePath = _require2.relative,
|
10
|
+
resolvePath = _require2.resolve;
|
23
11
|
var GENERATED = './__generated__/';
|
24
|
-
/**
|
25
|
-
* Given a graphql`` tagged template literal, replace it with the appropriate
|
26
|
-
* runtime artifact.
|
27
|
-
*/
|
28
|
-
|
29
12
|
function compileGraphQLTag(t, path, state, ast) {
|
30
13
|
var _state$opts$eagerEsMo, _state$opts, _state$opts2, _state$opts3, _state$opts4, _state$opts$codegenCo, _state$opts5;
|
31
|
-
|
32
14
|
if (ast.definitions.length !== 1) {
|
33
15
|
throw new Error('BabelPluginRelay: Expected exactly one definition per graphql tag.');
|
34
16
|
}
|
35
|
-
|
36
17
|
var definition = ast.definitions[0];
|
37
|
-
|
38
18
|
if (definition.kind !== 'FragmentDefinition' && definition.kind !== 'OperationDefinition') {
|
39
19
|
throw new Error('BabelPluginRelay: Expected a fragment, mutation, query, or ' + 'subscription, got `' + definition.kind + '`.');
|
40
20
|
}
|
41
|
-
|
42
21
|
var eagerEsModules = (_state$opts$eagerEsMo = (_state$opts = state.opts) === null || _state$opts === void 0 ? void 0 : _state$opts.eagerEsModules) !== null && _state$opts$eagerEsMo !== void 0 ? _state$opts$eagerEsMo : false;
|
43
22
|
var isHasteMode = ((_state$opts2 = state.opts) === null || _state$opts2 === void 0 ? void 0 : _state$opts2.jsModuleFormat) === 'haste';
|
44
23
|
var isDevVariable = (_state$opts3 = state.opts) === null || _state$opts3 === void 0 ? void 0 : _state$opts3.isDevVariableName;
|
45
24
|
var artifactDirectory = (_state$opts4 = state.opts) === null || _state$opts4 === void 0 ? void 0 : _state$opts4.artifactDirectory;
|
46
|
-
var buildCommand = (_state$opts$codegenCo = (_state$opts5 = state.opts) === null || _state$opts5 === void 0 ? void 0 : _state$opts5.codegenCommand) !== null && _state$opts$codegenCo !== void 0 ? _state$opts$codegenCo : 'relay-compiler';
|
47
|
-
|
25
|
+
var buildCommand = (_state$opts$codegenCo = (_state$opts5 = state.opts) === null || _state$opts5 === void 0 ? void 0 : _state$opts5.codegenCommand) !== null && _state$opts$codegenCo !== void 0 ? _state$opts$codegenCo : 'relay-compiler';
|
48
26
|
var isDevelopment = (process.env.BABEL_ENV || process.env.NODE_ENV) !== 'production';
|
49
27
|
return createNode(t, state, path, definition, {
|
50
28
|
artifactDirectory: artifactDirectory,
|
@@ -55,64 +33,46 @@ function compileGraphQLTag(t, path, state, ast) {
|
|
55
33
|
isDevVariable: isDevVariable
|
56
34
|
});
|
57
35
|
}
|
58
|
-
/**
|
59
|
-
* The Relay compiler generates separate modules that contain the compiled code.
|
60
|
-
* Here we generate:
|
61
|
-
* - a memoized `require` call for that generated code
|
62
|
-
* - for development mode, runtime validation that the artifacts are up to date
|
63
|
-
*/
|
64
|
-
|
65
|
-
|
66
36
|
function createNode(t, state, path, graphqlDefinition, options) {
|
67
37
|
var definitionName = graphqlDefinition.name && graphqlDefinition.name.value;
|
68
|
-
|
69
38
|
if (!definitionName) {
|
70
39
|
throw new Error('GraphQL operations and fragments must contain names');
|
71
40
|
}
|
72
|
-
|
73
41
|
var requiredFile = definitionName + '.graphql';
|
74
42
|
var requiredPath = options.isHasteMode ? requiredFile : options.artifactDirectory ? getRelativeImportPath(state, options.artifactDirectory, requiredFile) : GENERATED + requiredFile;
|
75
43
|
var hash = crypto.createHash('md5').update(print(graphqlDefinition), 'utf8').digest('hex');
|
76
44
|
var topScope = path.scope;
|
77
|
-
|
78
45
|
while (topScope.parent) {
|
79
46
|
topScope = topScope.parent;
|
80
47
|
}
|
81
|
-
|
82
48
|
var id = topScope.generateUidIdentifier(definitionName);
|
83
|
-
var expHash = t.MemberExpression(id, t.Identifier('hash'));
|
49
|
+
var expHash = t.MemberExpression(t.cloneNode(id), t.Identifier('hash'));
|
84
50
|
var expWarn = warnNeedsRebuild(t, definitionName, options.buildCommand);
|
85
|
-
var expWarnIfOutdated = t.LogicalExpression('&&', expHash, t.LogicalExpression('&&', t.BinaryExpression('!==', expHash, t.StringLiteral(hash)), expWarn));
|
86
|
-
|
51
|
+
var expWarnIfOutdated = t.LogicalExpression('&&', t.cloneNode(expHash), t.LogicalExpression('&&', t.BinaryExpression('!==', expHash, t.StringLiteral(hash)), expWarn));
|
87
52
|
if (options.eagerEsModules) {
|
88
53
|
var importDeclaration = t.ImportDeclaration([t.ImportDefaultSpecifier(id)], t.StringLiteral(requiredPath));
|
89
54
|
var program = path.findParent(function (parent) {
|
90
55
|
return parent.isProgram();
|
91
56
|
});
|
92
57
|
program.unshiftContainer('body', importDeclaration);
|
93
|
-
var expAssignAndCheck = t.SequenceExpression([expWarnIfOutdated, id]);
|
58
|
+
var expAssignAndCheck = t.SequenceExpression([expWarnIfOutdated, t.cloneNode(id)]);
|
94
59
|
var expAssign;
|
95
|
-
|
96
60
|
if (options.isDevVariable != null) {
|
97
|
-
expAssign = t.ConditionalExpression(t.Identifier(options.isDevVariable), expAssignAndCheck, id);
|
61
|
+
expAssign = t.ConditionalExpression(t.Identifier(options.isDevVariable), expAssignAndCheck, t.cloneNode(id));
|
98
62
|
} else if (options.isDevelopment) {
|
99
63
|
expAssign = expAssignAndCheck;
|
100
64
|
} else {
|
101
|
-
expAssign = id;
|
65
|
+
expAssign = t.cloneNode(id);
|
102
66
|
}
|
103
|
-
|
104
67
|
path.replaceWith(expAssign);
|
105
68
|
} else {
|
106
69
|
topScope.push({
|
107
|
-
id: id
|
70
|
+
id: t.cloneNode(id)
|
108
71
|
});
|
109
72
|
var requireGraphQLModule = t.CallExpression(t.Identifier('require'), [t.StringLiteral(requiredPath)]);
|
110
|
-
var expAssignProd = t.AssignmentExpression('=', id, requireGraphQLModule);
|
111
|
-
|
112
|
-
var _expAssignAndCheck = t.SequenceExpression([expAssignProd, expWarnIfOutdated, id]);
|
113
|
-
|
73
|
+
var expAssignProd = t.AssignmentExpression('=', t.cloneNode(id), requireGraphQLModule);
|
74
|
+
var _expAssignAndCheck = t.SequenceExpression([expAssignProd, expWarnIfOutdated, t.cloneNode(id)]);
|
114
75
|
var _expAssign;
|
115
|
-
|
116
76
|
if (options.isDevVariable != null) {
|
117
77
|
_expAssign = t.ConditionalExpression(t.Identifier(options.isDevVariable), _expAssignAndCheck, expAssignProd);
|
118
78
|
} else if (options.isDevelopment) {
|
@@ -120,25 +80,20 @@ function createNode(t, state, path, graphqlDefinition, options) {
|
|
120
80
|
} else {
|
121
81
|
_expAssign = expAssignProd;
|
122
82
|
}
|
123
|
-
|
124
83
|
var expVoid0 = t.UnaryExpression('void', t.NumericLiteral(0));
|
125
|
-
path.replaceWith(t.ConditionalExpression(t.BinaryExpression('!==', id, expVoid0), id, _expAssign));
|
84
|
+
path.replaceWith(t.ConditionalExpression(t.BinaryExpression('!==', t.cloneNode(id), expVoid0), t.cloneNode(id), t.cloneNode(_expAssign)));
|
126
85
|
}
|
127
86
|
}
|
128
|
-
|
129
87
|
function warnNeedsRebuild(t, definitionName, buildCommand) {
|
130
88
|
return t.callExpression(t.memberExpression(t.identifier('console'), t.identifier('error')), [t.stringLiteral("The definition of '".concat(definitionName, "' appears to have changed. Run ") + '`' + buildCommand + '` to update the generated files to receive the expected data.')]);
|
131
89
|
}
|
132
|
-
|
133
90
|
function getRelativeImportPath(state, artifactDirectory, fileToRequire) {
|
134
91
|
if (state.file == null) {
|
135
92
|
throw new Error('Babel state is missing expected file name');
|
136
93
|
}
|
137
|
-
|
138
94
|
var filename = state.file.opts.filename;
|
139
95
|
var relative = relativePath(dirname(filename), resolvePath(artifactDirectory));
|
140
96
|
var relativeReference = relative.length === 0 || !relative.startsWith('.') ? './' : '';
|
141
97
|
return relativeReference + joinPath(relative, fileToRequire);
|
142
98
|
}
|
143
|
-
|
144
99
|
module.exports = compileGraphQLTag;
|
@@ -1,45 +1,22 @@
|
|
1
|
-
/**
|
2
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
3
|
-
*
|
4
|
-
* This source code is licensed under the MIT license found in the
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
6
|
-
*
|
7
|
-
*
|
8
|
-
* @format
|
9
|
-
*/
|
10
1
|
'use strict';
|
11
2
|
|
12
3
|
var GraphQL = require('graphql');
|
13
|
-
/**
|
14
|
-
* Given a babel AST path to a tagged template literal, return an AST if it is
|
15
|
-
* a graphql literal being used in a valid way.
|
16
|
-
* If it is some other type of template literal then return nothing.
|
17
|
-
*/
|
18
|
-
|
19
|
-
|
20
4
|
function getValidGraphQLTag(path) {
|
21
5
|
var tag = path.get('tag');
|
22
|
-
|
23
6
|
if (!tag.isIdentifier({
|
24
7
|
name: 'graphql'
|
25
8
|
})) {
|
26
9
|
return null;
|
27
10
|
}
|
28
|
-
|
29
11
|
var quasis = path.node.quasi.quasis;
|
30
|
-
|
31
12
|
if (quasis.length !== 1) {
|
32
13
|
throw new Error('BabelPluginRelay: Substitutions are not allowed in graphql fragments. ' + 'Included fragments should be referenced as `...MyModule_propName`.');
|
33
14
|
}
|
34
|
-
|
35
15
|
var text = quasis[0].value.raw;
|
36
16
|
var ast = GraphQL.parse(text);
|
37
|
-
|
38
17
|
if (ast.definitions.length === 0) {
|
39
18
|
throw new Error('BabelPluginRelay: Unexpected empty graphql tag.');
|
40
19
|
}
|
41
|
-
|
42
20
|
return ast;
|
43
21
|
}
|
44
|
-
|
45
22
|
module.exports = getValidGraphQLTag;
|
package/macro.js
CHANGED
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "babel-plugin-relay",
|
3
3
|
"description": "A Babel Plugin for use with Relay applications.",
|
4
|
-
"version": "
|
4
|
+
"version": "16.0.0",
|
5
5
|
"keywords": [
|
6
6
|
"graphql",
|
7
7
|
"relay",
|
@@ -22,7 +22,8 @@
|
|
22
22
|
"graphql": "15.3.0"
|
23
23
|
},
|
24
24
|
"devDependencies": {
|
25
|
-
"@babel/core": "^7.
|
26
|
-
"prettier": "
|
25
|
+
"@babel/core": "^7.20.0",
|
26
|
+
"prettier": "2.8.8",
|
27
|
+
"prettier-plugin-hermes-parser": "0.17.0"
|
27
28
|
}
|
28
29
|
}
|