Skip to content

Commit f74387d

Browse files
apalmfacebook-github-bot
authored andcommitted
Support babel-plugin-macros (#2171)
Summary: This PR adds support for [babel-plugin-macros](https://github.com/kentcdodds/babel-plugin-macros). Usage: ```js import graphql from 'babel-plugin-relay/macro'; // Or: // const graphql = require('babel-plugin-relay/macro'); ``` `graphql` is exported from both the `react-relay` and `relay-runtime` packages, but as I created this PR mostly in the interest of possible Create React App integration, I only added an export to `react-relay`. This PR currently only adds support for Relay Modern. I'm not sure if Compat and/or Classic should be supported? I currently only have a single basic test. I certainly could copy over the fixtures in `__tests__/fixtures-modern` and change the imports, although I'm not sure how beneficial that would be, as most of the logic is shared with `babel-plugin-relay`. Closes #1968 Related: facebook/create-react-app#2730 (comment) /cc kentcdodds Pull Request resolved: #2171 Reviewed By: jstejada Differential Revision: D6831205 Pulled By: kassens fbshipit-source-id: 704a239b45974359cc1dcecacd85efb4aeddeef5
1 parent 7d43533 commit f74387d

8 files changed

+182
-1
lines changed

docs/Introduction-InstallationAndSetup.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ Babel's [documentation on this topic](https://babeljs.io/docs/plugins/#pluginpre
3535

3636
See the [Migration Setup](./migration-setup.html) guide if upgrading an existing Relay app.
3737

38+
Alternatively, instead of using `babel-plugin-relay`, you can use Relay with [babel-plugin-macros](https://github.com/kentcdodds/babel-plugin-macros). After installing `babel-plugin-macros` and adding it to your Babel config:
39+
40+
```javascript
41+
const graphql = require('babel-plugin-relay/macro');
42+
```
43+
3844
## Set up relay-compiler
3945

4046
Relay's ahead-of-time compilation requires the [Relay Compiler](./graphql-in-relay.html#relay-compiler.html), which you can install via `yarn` or `npm`:

gulpfile.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const babelOptions = require('./scripts/getBabelOptions')({
1818
'@babel/parser': '@babel/parser',
1919
'@babel/types': '@babel/types',
2020
'babel-core': 'babel-core',
21+
'babel-plugin-macros': 'babel-plugin-macros',
2122
'babel-generator': 'babel-generator',
2223
'babel-generator/lib/printer': 'babel-generator/lib/printer',
2324
'babel-polyfill': 'babel-polyfill',
@@ -153,6 +154,7 @@ const builds = [
153154
package: 'babel-plugin-relay',
154155
exports: {
155156
index: 'BabelPluginRelay.js',
157+
macro: 'BabelPluginRelay.macro.js',
156158
},
157159
bundles: [
158160
{

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
"babel-cli": "6.24.1",
2424
"babel-core": "6.25.0",
2525
"babel-eslint": "9.0.0-beta.2",
26+
"babel-plugin-macros": "^2.0.0",
27+
"babel-plugin-tester": "^5.0.0",
2628
"babel-plugin-transform-async-to-generator": "6.24.1",
2729
"babel-plugin-transform-es2015-modules-commonjs": "6.24.1",
2830
"babel-plugin-transform-flow-strip-types": "6.22.0",
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Copyright (c) 2013-present, Facebook, Inc.
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+
* @flow
8+
* @format
9+
*/
10+
11+
'use strict';
12+
13+
const compileGraphQLTag = require('./compileGraphQLTag');
14+
const getValidGraphQLTag = require('./getValidGraphQLTag');
15+
16+
const {createMacro} = require('babel-plugin-macros');
17+
18+
function BabelPluginRelayMacro({references, state, babel}) {
19+
const {types: t} = babel;
20+
Object.keys(references).forEach(referenceKey => {
21+
references[referenceKey].forEach(reference => {
22+
const path = reference.parentPath;
23+
const ast = getValidGraphQLTag(path);
24+
if (ast) {
25+
compileGraphQLTag(t, path, state, ast);
26+
}
27+
});
28+
});
29+
}
30+
31+
module.exports = createMacro(BabelPluginRelayMacro);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Copyright (c) 2013-present, Facebook, Inc.
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+
* @format
8+
* @emails oncall+relay
9+
*/
10+
11+
'use strict';
12+
13+
require('configureForRelayOSS');
14+
15+
const pluginTester = require('babel-plugin-tester');
16+
const plugin = require('babel-plugin-macros');
17+
18+
pluginTester({
19+
plugin,
20+
snapshot: true,
21+
title: 'BabelPluginRelayMacro',
22+
babelOptions: {filename: __filename, parserOpts: {plugins: ['jsx']}},
23+
tests: {
24+
works: `
25+
'use strict';
26+
27+
const {graphql} = require('../BabelPluginRelay.macro');
28+
const ProfilePic = require('ProfilePic');
29+
30+
const ViewerQuery = graphql\`
31+
query ViewerQuery($id: ID!, $scale: Float = 1.5) {
32+
node(id: $id) {
33+
... on User {
34+
id
35+
...ProfilePic_user
36+
}
37+
}
38+
}
39+
\`;
40+
`,
41+
},
42+
});
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`BabelPluginRelayMacro works: works 1`] = `
4+
"
5+
'use strict';
6+
7+
const {graphql} = require('../BabelPluginRelay.macro');
8+
const ProfilePic = require('ProfilePic');
9+
10+
const ViewerQuery = graphql\`
11+
query ViewerQuery($id: ID!, $scale: Float = 1.5) {
12+
node(id: $id) {
13+
... on User {
14+
id
15+
...ProfilePic_user
16+
}
17+
}
18+
}
19+
\`;
20+
21+
↓ ↓ ↓ ↓ ↓ ↓
22+
23+
'use strict';
24+
25+
const ProfilePic = require('ProfilePic');
26+
27+
const ViewerQuery = function () {
28+
const node = require('./__generated__/ViewerQuery.graphql');
29+
30+
if (node.hash && node.hash !== 'b046a97b7823510c05083ebb114377f4') {
31+
console.error('The definition of \\\\'ViewerQuery\\\\' appears to have changed. Run \`relay-compiler\` to update the generated files to receive the expected data.');
32+
}
33+
34+
return require('./__generated__/ViewerQuery.graphql');
35+
};
36+
"
37+
`;

packages/babel-plugin-relay/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"bugs": "https://github.com/facebook/relay/issues",
1414
"repository": "facebook/relay",
1515
"dependencies": {
16+
"babel-plugin-macros": "^2.0.0",
1617
"babel-runtime": "^6.23.0",
1718
"babel-types": "^6.24.1"
1819
},

yarn.lock

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,12 @@ babel-plugin-jest-hoist@^23.0.1:
671671
version "23.0.1"
672672
resolved "https://registry.yarnpkg.com/babel-plugin-jest-hoist/-/babel-plugin-jest-hoist-23.0.1.tgz#eaa11c964563aea9c21becef2bdf7853f7f3c148"
673673

674+
babel-plugin-macros@^2.0.0:
675+
version "2.4.0"
676+
resolved "https://registry.yarnpkg.com/babel-plugin-macros/-/babel-plugin-macros-2.4.0.tgz#6c5f9836e1f6c0a9743b3bab4af29f73e437e544"
677+
dependencies:
678+
cosmiconfig "^5.0.5"
679+
674680
babel-plugin-syntax-async-functions@^6.8.0:
675681
version "6.13.0"
676682
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-async-functions/-/babel-plugin-syntax-async-functions-6.13.0.tgz#cad9cad1191b5ad634bf30ae0872391e0647be95"
@@ -695,6 +701,16 @@ babel-plugin-syntax-trailing-function-commas@^6.8.0:
695701
version "6.22.0"
696702
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-trailing-function-commas/-/babel-plugin-syntax-trailing-function-commas-6.22.0.tgz#ba0360937f8d06e40180a43fe0d5616fff532cf3"
697703

704+
babel-plugin-tester@^5.0.0:
705+
version "5.0.0"
706+
resolved "https://registry.yarnpkg.com/babel-plugin-tester/-/babel-plugin-tester-5.0.0.tgz#d3387860311cbd8353746d3a8aaba7ad2446e470"
707+
dependencies:
708+
common-tags "^1.4.0"
709+
invariant "^2.2.2"
710+
lodash.merge "^4.6.0"
711+
path-exists "^3.0.0"
712+
strip-indent "^2.0.0"
713+
698714
699715
version "6.24.1"
700716
resolved "https://registry.yarnpkg.com/babel-plugin-transform-async-to-generator/-/babel-plugin-transform-async-to-generator-6.24.1.tgz#6536e378aff6cb1d5517ac0e40eb3e9fc8d08761"
@@ -1358,6 +1374,12 @@ commander@^2.11.0, commander@^2.8.1:
13581374
version "2.15.1"
13591375
resolved "https://registry.yarnpkg.com/commander/-/commander-2.15.1.tgz#df46e867d0fc2aec66a34662b406a9ccafff5b0f"
13601376

1377+
common-tags@^1.4.0:
1378+
version "1.6.0"
1379+
resolved "https://registry.yarnpkg.com/common-tags/-/common-tags-1.6.0.tgz#788e4bcc582f16993e5b2c92f76b1ccb80731537"
1380+
dependencies:
1381+
babel-runtime "^6.26.0"
1382+
13611383
compare-versions@^3.1.0:
13621384
version "3.3.0"
13631385
resolved "https://registry.yarnpkg.com/compare-versions/-/compare-versions-3.3.0.tgz#af93ea705a96943f622ab309578b9b90586f39c3"
@@ -1423,6 +1445,14 @@ [email protected], core-util-is@~1.0.0:
14231445
version "1.0.2"
14241446
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"
14251447

1448+
cosmiconfig@^5.0.5:
1449+
version "5.0.5"
1450+
resolved "https://registry.yarnpkg.com/cosmiconfig/-/cosmiconfig-5.0.5.tgz#a809e3c2306891ce17ab70359dc8bdf661fe2cd0"
1451+
dependencies:
1452+
is-directory "^0.3.1"
1453+
js-yaml "^3.9.0"
1454+
parse-json "^4.0.0"
1455+
14261456
create-react-class@^15.6.0:
14271457
version "15.6.3"
14281458
resolved "https://registry.yarnpkg.com/create-react-class/-/create-react-class-15.6.3.tgz#2d73237fb3f970ae6ebe011a9e66f46dbca80036"
@@ -1713,7 +1743,7 @@ errno@^0.1.3:
17131743
dependencies:
17141744
prr "~1.0.1"
17151745

1716-
error-ex@^1.2.0:
1746+
error-ex@^1.2.0, error-ex@^1.3.1:
17171747
version "1.3.1"
17181748
resolved "https://registry.yarnpkg.com/error-ex/-/error-ex-1.3.1.tgz#f855a86ce61adc4e8621c3cda21e7a7612c3a8dc"
17191749
dependencies:
@@ -2945,6 +2975,10 @@ is-descriptor@^1.0.0, is-descriptor@^1.0.2:
29452975
is-data-descriptor "^1.0.0"
29462976
kind-of "^6.0.2"
29472977

2978+
is-directory@^0.3.1:
2979+
version "0.3.1"
2980+
resolved "https://registry.yarnpkg.com/is-directory/-/is-directory-0.3.1.tgz#61339b6f2475fc772fd9c9d83f5c8575dc154ae1"
2981+
29482982
is-dotfile@^1.0.0:
29492983
version "1.0.3"
29502984
resolved "https://registry.yarnpkg.com/is-dotfile/-/is-dotfile-1.0.3.tgz#a6a2f32ffd2dfb04f5ca25ecd0f6b83cf798a1e1"
@@ -3507,6 +3541,13 @@ js-yaml@^3.11.0, js-yaml@^3.7.0:
35073541
argparse "^1.0.7"
35083542
esprima "^4.0.0"
35093543

3544+
js-yaml@^3.9.0:
3545+
version "3.10.0"
3546+
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.10.0.tgz#2e78441646bd4682e963f22b6e92823c309c62dc"
3547+
dependencies:
3548+
argparse "^1.0.7"
3549+
esprima "^4.0.0"
3550+
35103551
jsbn@~0.1.0:
35113552
version "0.1.1"
35123553
resolved "https://registry.yarnpkg.com/jsbn/-/jsbn-0.1.1.tgz#a5e654c2e5a2deb5f201d96cefbca80c0ef2f513"
@@ -3550,6 +3591,10 @@ jsesc@^2.5.1:
35503591
version "2.5.1"
35513592
resolved "https://registry.yarnpkg.com/jsesc/-/jsesc-2.5.1.tgz#e421a2a8e20d6b0819df28908f782526b96dd1fe"
35523593

3594+
json-parse-better-errors@^1.0.1:
3595+
version "1.0.2"
3596+
resolved "https://registry.yarnpkg.com/json-parse-better-errors/-/json-parse-better-errors-1.0.2.tgz#bb867cfb3450e69107c131d1c514bab3dc8bcaa9"
3597+
35533598
json-schema-traverse@^0.3.0:
35543599
version "0.3.1"
35553600
resolved "https://registry.yarnpkg.com/json-schema-traverse/-/json-schema-traverse-0.3.1.tgz#349a6d44c53a51de89b40805c5d5e59b417d3340"
@@ -3748,6 +3793,10 @@ lodash.keys@^3.0.0:
37483793
lodash.isarguments "^3.0.0"
37493794
lodash.isarray "^3.0.0"
37503795

3796+
lodash.merge@^4.6.0:
3797+
version "4.6.0"
3798+
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.0.tgz#69884ba144ac33fe699737a6086deffadd0f89c5"
3799+
37513800
lodash.restparam@^3.0.0:
37523801
version "3.6.1"
37533802
resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"
@@ -4394,6 +4443,13 @@ parse-json@^2.2.0:
43944443
dependencies:
43954444
error-ex "^1.2.0"
43964445

4446+
parse-json@^4.0.0:
4447+
version "4.0.0"
4448+
resolved "https://registry.yarnpkg.com/parse-json/-/parse-json-4.0.0.tgz#be35f5425be1f7f6c747184f98a788cb99477ee0"
4449+
dependencies:
4450+
error-ex "^1.3.1"
4451+
json-parse-better-errors "^1.0.1"
4452+
43974453
parse-passwd@^1.0.0:
43984454
version "1.0.0"
43994455
resolved "https://registry.yarnpkg.com/parse-passwd/-/parse-passwd-1.0.0.tgz#6d5b934a456993b23d37f40a382d6f1666a8e5c6"
@@ -5322,6 +5378,10 @@ strip-eof@^1.0.0:
53225378
version "1.0.0"
53235379
resolved "https://registry.yarnpkg.com/strip-eof/-/strip-eof-1.0.0.tgz#bb43ff5598a6eb05d89b59fcd129c983313606bf"
53245380

5381+
strip-indent@^2.0.0:
5382+
version "2.0.0"
5383+
resolved "https://registry.yarnpkg.com/strip-indent/-/strip-indent-2.0.0.tgz#5ef8db295d01e6ed6cbf7aab96998d7822527b68"
5384+
53255385
strip-json-comments@^2.0.1, strip-json-comments@~2.0.1:
53265386
version "2.0.1"
53275387
resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-2.0.1.tgz#3c531942e908c2697c0ec344858c286c7ca0a60a"

0 commit comments

Comments
 (0)