From 948b24f0352e65c8bf65d57884258f36b4162e50 Mon Sep 17 00:00:00 2001 From: Andrew Palm Date: Thu, 26 Oct 2017 20:21:28 -0400 Subject: [PATCH 1/6] [WIP] Support babel-macros --- docs/modern/BabelPluginRelay.md | 8 ++++ gulpfile.js | 3 +- package.json | 1 + .../BabelPluginRelayMacro.js | 31 +++++++++++++ .../__tests__/BabelPluginRelayMacro-test.js | 45 +++++++++++++++++++ .../BabelPluginRelayMacro-test.js.snap | 12 +++++ .../modern/ReactRelayGraphQL.macro.js | 1 + yarn.lock | 33 +++++++++++++- 8 files changed, 131 insertions(+), 3 deletions(-) create mode 100644 packages/babel-plugin-relay/BabelPluginRelayMacro.js create mode 100644 packages/babel-plugin-relay/__tests__/BabelPluginRelayMacro-test.js create mode 100644 packages/babel-plugin-relay/__tests__/__snapshots__/BabelPluginRelayMacro-test.js.snap create mode 100644 packages/react-relay/modern/ReactRelayGraphQL.macro.js diff --git a/docs/modern/BabelPluginRelay.md b/docs/modern/BabelPluginRelay.md index 24bbfcf6a511d..829d37af3576c 100644 --- a/docs/modern/BabelPluginRelay.md +++ b/docs/modern/BabelPluginRelay.md @@ -100,3 +100,11 @@ When compiling code for production deployment, the plugin can be configured to i ] } ``` + +### Alternatives + +Instead of using `babel-plugin-relay`, you can use Relay with [babel-macros](https://github.com/kentcdodds/babel-macros). After installing `babel-macros` and adding it to your Babel config: + +```javascript +const {graphql} = require('react-relay/macro'); +``` diff --git a/gulpfile.js b/gulpfile.js index a8cd3b9fe98b2..3a8ffb45c284f 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -162,7 +162,8 @@ const builds = [ exports: { classic: 'ReactRelayClassicExports.js', compat: 'ReactRelayCompatPublic.js', - index: 'ReactRelayPublic.js' + index: 'ReactRelayPublic.js', + macro: 'ReactRelayGraphQL.macro.js' }, bundles: [ { diff --git a/package.json b/package.json index e12447272cba6..fba331729a8e0 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "babel-core": "6.25.0", "babel-eslint": "7.2.3", "babel-generator": "^6.26.0", + "babel-macros": "1.2.0", "babel-plugin-transform-async-to-generator": "6.24.1", "babel-plugin-transform-es2015-modules-commonjs": "6.24.1", "babel-plugin-transform-flow-strip-types": "6.22.0", diff --git a/packages/babel-plugin-relay/BabelPluginRelayMacro.js b/packages/babel-plugin-relay/BabelPluginRelayMacro.js new file mode 100644 index 0000000000000..3d0d36ba7cad0 --- /dev/null +++ b/packages/babel-plugin-relay/BabelPluginRelayMacro.js @@ -0,0 +1,31 @@ +/** + * Copyright (c) 2013-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @providesModule BabelPluginRelayMacro + * @flow + * @format + */ + +'use strict'; + +const {createMacro} = require('babel-macros'); +const compileGraphQLTag = require('./compileGraphQLTag'); +const getValidGraphQLTag = require('./getValidGraphQLTag'); + +function BabelPluginRelayMacro({references, state, babel}) { + const {types: t} = babel; + Object.keys(references).forEach(referenceKey => { + references[referenceKey].forEach(reference => { + const path = reference.parentPath; + const ast = getValidGraphQLTag(path); + if (ast) { + compileGraphQLTag(t, path, state, ast); + } + }); + }); +} + +module.exports = createMacro(BabelPluginRelayMacro); diff --git a/packages/babel-plugin-relay/__tests__/BabelPluginRelayMacro-test.js b/packages/babel-plugin-relay/__tests__/BabelPluginRelayMacro-test.js new file mode 100644 index 0000000000000..e1a2a2cda15ba --- /dev/null +++ b/packages/babel-plugin-relay/__tests__/BabelPluginRelayMacro-test.js @@ -0,0 +1,45 @@ +/** + * Copyright (c) 2013-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @format + * @emails oncall+relay + */ + +'use strict'; + +require('configureForRelayOSS'); + +const babel = require('babel-core'); + +describe('BabelPluginRelayMacro', () => { + test('works', () => { + const basic = ` + 'use strict'; + + const {graphql} = require('../../react-relay/modern/ReactRelayGraphQL.macro'); + const CompatProfilePic = require('CompatProfilePic'); + + const CompatViewerQuery = graphql\` + query CompatViewerQuery($id: ID!, $scale: Float = 1.5) { + node(id: $id) { + ... on User { + id + ...CompatProfilePic_user + } + } + } + \`; + `; + const {code} = babel.transform(basic, { + plugins: ['babel-macros'], + filename: __filename, + compact: false, + parserOpts: {plugins: ['jsx']}, + babelrc: false, + }); + expect(code).toMatchSnapshot(); + }); +}); diff --git a/packages/babel-plugin-relay/__tests__/__snapshots__/BabelPluginRelayMacro-test.js.snap b/packages/babel-plugin-relay/__tests__/__snapshots__/BabelPluginRelayMacro-test.js.snap new file mode 100644 index 0000000000000..b9c3cc1b28bf6 --- /dev/null +++ b/packages/babel-plugin-relay/__tests__/__snapshots__/BabelPluginRelayMacro-test.js.snap @@ -0,0 +1,12 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`BabelPluginRelayMacro works 1`] = ` +" +'use strict'; + +const CompatProfilePic = require('CompatProfilePic'); + +const CompatViewerQuery = function () { + return require('./__generated__/CompatViewerQuery.graphql'); +};" +`; diff --git a/packages/react-relay/modern/ReactRelayGraphQL.macro.js b/packages/react-relay/modern/ReactRelayGraphQL.macro.js new file mode 100644 index 0000000000000..0f78fcb5fa481 --- /dev/null +++ b/packages/react-relay/modern/ReactRelayGraphQL.macro.js @@ -0,0 +1 @@ +module.exports = require('../../babel-plugin-relay/BabelPluginRelayMacro'); \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 5b9e2ae115085..c8bbbef7ce7f7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -460,6 +460,12 @@ babel-jest@^21.0.2: babel-plugin-istanbul "^4.0.0" babel-preset-jest "^21.0.2" +babel-macros@1.2.0: + version "1.2.0" + resolved "https://registry.yarnpkg.com/babel-macros/-/babel-macros-1.2.0.tgz#39e47ed6d286d4a98f1948d8bab45dac17e4e2d4" + dependencies: + cosmiconfig "3.1.0" + babel-messages@^6.23.0: version "6.23.0" resolved "https://registry.yarnpkg.com/babel-messages/-/babel-messages-6.23.0.tgz#f3cdf4703858035b2a2951c6ec5edf6c62f2630e" @@ -1221,6 +1227,15 @@ core-util-is@1.0.2, core-util-is@~1.0.0: version "1.0.2" resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7" +cosmiconfig@3.1.0: + version "3.1.0" + resolved "https://registry.yarnpkg.com/cosmiconfig/-/cosmiconfig-3.1.0.tgz#640a94bf9847f321800403cd273af60665c73397" + dependencies: + is-directory "^0.3.1" + js-yaml "^3.9.0" + parse-json "^3.0.0" + require-from-string "^2.0.1" + create-react-class@^15.6.0: version "15.6.0" resolved "https://registry.yarnpkg.com/create-react-class/-/create-react-class-15.6.0.tgz#ab448497c26566e1e29413e883207d57cfe7bed4" @@ -1481,7 +1496,7 @@ errno@^0.1.3, errno@^0.1.4: dependencies: prr "~0.0.0" -error-ex@^1.2.0: +error-ex@^1.2.0, error-ex@^1.3.1: version "1.3.1" resolved "https://registry.yarnpkg.com/error-ex/-/error-ex-1.3.1.tgz#f855a86ce61adc4e8621c3cda21e7a7612c3a8dc" dependencies: @@ -2653,6 +2668,10 @@ is-descriptor@^1.0.0: is-data-descriptor "^0.1.4" kind-of "^5.0.0" +is-directory@^0.3.1: + version "0.3.1" + resolved "https://registry.yarnpkg.com/is-directory/-/is-directory-0.3.1.tgz#61339b6f2475fc772fd9c9d83f5c8575dc154ae1" + is-dotfile@^1.0.0: version "1.0.3" resolved "https://registry.yarnpkg.com/is-dotfile/-/is-dotfile-1.0.3.tgz#a6a2f32ffd2dfb04f5ca25ecd0f6b83cf798a1e1" @@ -3136,7 +3155,7 @@ js-tokens@^3.0.0, js-tokens@^3.0.2: version "3.0.2" resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.2.tgz#9866df395102130e38f7f996bceb65443209c25b" -js-yaml@^3.7.0, js-yaml@^3.9.1: +js-yaml@^3.7.0, js-yaml@^3.9.0, js-yaml@^3.9.1: version "3.10.0" resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.10.0.tgz#2e78441646bd4682e963f22b6e92823c309c62dc" dependencies: @@ -3955,6 +3974,12 @@ parse-json@^2.2.0: dependencies: error-ex "^1.2.0" +parse-json@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/parse-json/-/parse-json-3.0.0.tgz#fa6f47b18e23826ead32f263e744d0e1e847fb13" + dependencies: + error-ex "^1.3.1" + parse-passwd@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/parse-passwd/-/parse-passwd-1.0.0.tgz#6d5b934a456993b23d37f40a382d6f1666a8e5c6" @@ -4350,6 +4375,10 @@ require-directory@^2.1.1: version "2.1.1" resolved "https://registry.yarnpkg.com/require-directory/-/require-directory-2.1.1.tgz#8c64ad5fd30dab1c976e2344ffe7f792a6a6df42" +require-from-string@^2.0.1: + version "2.0.1" + resolved "https://registry.yarnpkg.com/require-from-string/-/require-from-string-2.0.1.tgz#c545233e9d7da6616e9d59adfb39fc9f588676ff" + require-main-filename@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/require-main-filename/-/require-main-filename-1.0.1.tgz#97f717b69d48784f5f526a6c5aa8ffdda055a4d1" From 9bb9f2b669c50e634bdd2838bbb9979dc22179dc Mon Sep 17 00:00:00 2001 From: Andrew Palm Date: Tue, 26 Dec 2017 10:06:00 -0500 Subject: [PATCH 2/6] babel-macros -> babel-plugin-macros --- package.json | 2 +- packages/babel-plugin-relay/BabelPluginRelayMacro.js | 2 +- .../__tests__/BabelPluginRelayMacro-test.js | 2 +- yarn.lock | 12 ++++++------ 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 95390c08662d9..7ad36fc2cb822 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ "babel-core": "6.25.0", "babel-eslint": "7.2.3", "babel-generator": "^6.26.0", - "babel-macros": "1.2.0", + "babel-plugin-macros": "2.0.0", "babel-plugin-transform-async-to-generator": "6.24.1", "babel-plugin-transform-es2015-modules-commonjs": "6.24.1", "babel-plugin-transform-flow-strip-types": "6.22.0", diff --git a/packages/babel-plugin-relay/BabelPluginRelayMacro.js b/packages/babel-plugin-relay/BabelPluginRelayMacro.js index 3d0d36ba7cad0..70b696e4524cd 100644 --- a/packages/babel-plugin-relay/BabelPluginRelayMacro.js +++ b/packages/babel-plugin-relay/BabelPluginRelayMacro.js @@ -11,7 +11,7 @@ 'use strict'; -const {createMacro} = require('babel-macros'); +const {createMacro} = require('babel-plugin-macros'); const compileGraphQLTag = require('./compileGraphQLTag'); const getValidGraphQLTag = require('./getValidGraphQLTag'); diff --git a/packages/babel-plugin-relay/__tests__/BabelPluginRelayMacro-test.js b/packages/babel-plugin-relay/__tests__/BabelPluginRelayMacro-test.js index e1a2a2cda15ba..3934b888f9575 100644 --- a/packages/babel-plugin-relay/__tests__/BabelPluginRelayMacro-test.js +++ b/packages/babel-plugin-relay/__tests__/BabelPluginRelayMacro-test.js @@ -34,7 +34,7 @@ describe('BabelPluginRelayMacro', () => { \`; `; const {code} = babel.transform(basic, { - plugins: ['babel-macros'], + plugins: ['babel-plugin-macros'], filename: __filename, compact: false, parserOpts: {plugins: ['jsx']}, diff --git a/yarn.lock b/yarn.lock index 094650be160ee..6a7ad5968ed20 100644 --- a/yarn.lock +++ b/yarn.lock @@ -489,12 +489,6 @@ babel-jest@^22.0.3: babel-plugin-istanbul "^4.1.5" babel-preset-jest "^22.0.3" -babel-macros@1.2.0: - version "1.2.0" - resolved "https://registry.yarnpkg.com/babel-macros/-/babel-macros-1.2.0.tgz#39e47ed6d286d4a98f1948d8bab45dac17e4e2d4" - dependencies: - cosmiconfig "3.1.0" - babel-messages@^6.23.0: version "6.23.0" resolved "https://registry.yarnpkg.com/babel-messages/-/babel-messages-6.23.0.tgz#f3cdf4703858035b2a2951c6ec5edf6c62f2630e" @@ -519,6 +513,12 @@ babel-plugin-jest-hoist@^22.0.3: version "22.0.3" resolved "https://registry.yarnpkg.com/babel-plugin-jest-hoist/-/babel-plugin-jest-hoist-22.0.3.tgz#62cde5fe962fd41ae89c119f481ca5cd7dd48bb4" +babel-plugin-macros@2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/babel-plugin-macros/-/babel-plugin-macros-2.0.0.tgz#fd3aee135f7dec0b82898b7c8f1aed6fa75f9af9" + dependencies: + cosmiconfig "3.1.0" + babel-plugin-syntax-async-functions@^6.8.0: version "6.13.0" resolved "https://registry.yarnpkg.com/babel-plugin-syntax-async-functions/-/babel-plugin-syntax-async-functions-6.13.0.tgz#cad9cad1191b5ad634bf30ae0872391e0647be95" From 072b937d4b08d7473c40864b04274b9b9177dd7f Mon Sep 17 00:00:00 2001 From: Andrew Palm Date: Tue, 26 Dec 2017 10:06:34 -0500 Subject: [PATCH 3/6] Update snapshot --- .../__snapshots__/BabelPluginRelayMacro-test.js.snap | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/babel-plugin-relay/__tests__/__snapshots__/BabelPluginRelayMacro-test.js.snap b/packages/babel-plugin-relay/__tests__/__snapshots__/BabelPluginRelayMacro-test.js.snap index b9c3cc1b28bf6..63974488d56f4 100644 --- a/packages/babel-plugin-relay/__tests__/__snapshots__/BabelPluginRelayMacro-test.js.snap +++ b/packages/babel-plugin-relay/__tests__/__snapshots__/BabelPluginRelayMacro-test.js.snap @@ -7,6 +7,12 @@ exports[`BabelPluginRelayMacro works 1`] = ` const CompatProfilePic = require('CompatProfilePic'); const CompatViewerQuery = function () { + const node = require('./__generated__/CompatViewerQuery.graphql'); + + if (node.hash && node.hash !== '42a62b166f34144936e64b3c65e42c4d') { + console.error('The definition of \\\\'CompatViewerQuery\\\\' appears to have changed. Run \`relay-compiler\` to update the generated files to receive the expected data.'); + } + return require('./__generated__/CompatViewerQuery.graphql'); };" `; From 76e4a386e9c6c3a2c3472b40a4d7c54fa263ca52 Mon Sep 17 00:00:00 2001 From: Andrew Palm Date: Tue, 26 Dec 2017 10:08:32 -0500 Subject: [PATCH 4/6] Add comment --- packages/react-relay/modern/ReactRelayGraphQL.macro.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/react-relay/modern/ReactRelayGraphQL.macro.js b/packages/react-relay/modern/ReactRelayGraphQL.macro.js index 0f78fcb5fa481..48a4b34dda53b 100644 --- a/packages/react-relay/modern/ReactRelayGraphQL.macro.js +++ b/packages/react-relay/modern/ReactRelayGraphQL.macro.js @@ -1 +1,5 @@ +// This module is named the way it is because babel-plugin-macros requires +// the source string to match the regex /[./]macro(\.js)?$/, and +// this module is imported in the test suite. + module.exports = require('../../babel-plugin-relay/BabelPluginRelayMacro'); \ No newline at end of file From acf85faaf127116d851bc601f23e68a5cb2f9157 Mon Sep 17 00:00:00 2001 From: Andrew Palm Date: Tue, 26 Dec 2017 10:31:34 -0500 Subject: [PATCH 5/6] Use babel-plugin-tester --- package.json | 1 + .../__tests__/BabelPluginRelayMacro-test.js | 25 ++++++++----------- .../BabelPluginRelayMacro-test.js.snap | 23 +++++++++++++++-- yarn.lock | 24 ++++++++++++++++++ 4 files changed, 57 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index 7ad36fc2cb822..eebb45ff0ca3b 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "babel-eslint": "7.2.3", "babel-generator": "^6.26.0", "babel-plugin-macros": "2.0.0", + "babel-plugin-tester": "^5.0.0", "babel-plugin-transform-async-to-generator": "6.24.1", "babel-plugin-transform-es2015-modules-commonjs": "6.24.1", "babel-plugin-transform-flow-strip-types": "6.22.0", diff --git a/packages/babel-plugin-relay/__tests__/BabelPluginRelayMacro-test.js b/packages/babel-plugin-relay/__tests__/BabelPluginRelayMacro-test.js index 3934b888f9575..c5b43a7cb248c 100644 --- a/packages/babel-plugin-relay/__tests__/BabelPluginRelayMacro-test.js +++ b/packages/babel-plugin-relay/__tests__/BabelPluginRelayMacro-test.js @@ -12,11 +12,16 @@ require('configureForRelayOSS'); -const babel = require('babel-core'); +const pluginTester = require('babel-plugin-tester'); +const plugin = require('babel-plugin-macros'); -describe('BabelPluginRelayMacro', () => { - test('works', () => { - const basic = ` +pluginTester({ + plugin, + snapshot: true, + title: 'BabelPluginRelayMacro', + babelOptions: {filename: __filename, parserOpts: {plugins: ['jsx']}}, + tests: { + works: ` 'use strict'; const {graphql} = require('../../react-relay/modern/ReactRelayGraphQL.macro'); @@ -32,14 +37,6 @@ describe('BabelPluginRelayMacro', () => { } } \`; - `; - const {code} = babel.transform(basic, { - plugins: ['babel-plugin-macros'], - filename: __filename, - compact: false, - parserOpts: {plugins: ['jsx']}, - babelrc: false, - }); - expect(code).toMatchSnapshot(); - }); + `, + }, }); diff --git a/packages/babel-plugin-relay/__tests__/__snapshots__/BabelPluginRelayMacro-test.js.snap b/packages/babel-plugin-relay/__tests__/__snapshots__/BabelPluginRelayMacro-test.js.snap index 63974488d56f4..9c55df0ecbef5 100644 --- a/packages/babel-plugin-relay/__tests__/__snapshots__/BabelPluginRelayMacro-test.js.snap +++ b/packages/babel-plugin-relay/__tests__/__snapshots__/BabelPluginRelayMacro-test.js.snap @@ -1,9 +1,27 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`BabelPluginRelayMacro works 1`] = ` +exports[`BabelPluginRelayMacro works: works 1`] = ` " 'use strict'; +const {graphql} = require('../../react-relay/modern/ReactRelayGraphQL.macro'); +const CompatProfilePic = require('CompatProfilePic'); + +const CompatViewerQuery = graphql\` + query CompatViewerQuery($id: ID!, $scale: Float = 1.5) { + node(id: $id) { + ... on User { + id + ...CompatProfilePic_user + } + } + } +\`; + + ↓ ↓ ↓ ↓ ↓ ↓ + +'use strict'; + const CompatProfilePic = require('CompatProfilePic'); const CompatViewerQuery = function () { @@ -14,5 +32,6 @@ const CompatViewerQuery = function () { } return require('./__generated__/CompatViewerQuery.graphql'); -};" +}; +" `; diff --git a/yarn.lock b/yarn.lock index 6a7ad5968ed20..fe63010ae32cf 100644 --- a/yarn.lock +++ b/yarn.lock @@ -543,6 +543,16 @@ babel-plugin-syntax-trailing-function-commas@^6.5.0, babel-plugin-syntax-trailin version "6.22.0" resolved "https://registry.yarnpkg.com/babel-plugin-syntax-trailing-function-commas/-/babel-plugin-syntax-trailing-function-commas-6.22.0.tgz#ba0360937f8d06e40180a43fe0d5616fff532cf3" +babel-plugin-tester@^5.0.0: + version "5.0.0" + resolved "https://registry.yarnpkg.com/babel-plugin-tester/-/babel-plugin-tester-5.0.0.tgz#d3387860311cbd8353746d3a8aaba7ad2446e470" + dependencies: + common-tags "^1.4.0" + invariant "^2.2.2" + lodash.merge "^4.6.0" + path-exists "^3.0.0" + strip-indent "^2.0.0" + babel-plugin-transform-async-to-generator@6.24.1: version "6.24.1" resolved "https://registry.yarnpkg.com/babel-plugin-transform-async-to-generator/-/babel-plugin-transform-async-to-generator-6.24.1.tgz#6536e378aff6cb1d5517ac0e40eb3e9fc8d08761" @@ -1213,6 +1223,12 @@ commander@^2.8.1: version "2.11.0" resolved "https://registry.yarnpkg.com/commander/-/commander-2.11.0.tgz#157152fd1e7a6c8d98a5b715cf376df928004563" +common-tags@^1.4.0: + version "1.6.0" + resolved "https://registry.yarnpkg.com/common-tags/-/common-tags-1.6.0.tgz#788e4bcc582f16993e5b2c92f76b1ccb80731537" + dependencies: + babel-runtime "^6.26.0" + component-emitter@^1.2.1: version "1.2.1" resolved "https://registry.yarnpkg.com/component-emitter/-/component-emitter-1.2.1.tgz#137918d6d78283f7df7a6b7c5a63e140e69425e6" @@ -3591,6 +3607,10 @@ lodash.mapvalues@^4.4.0: version "4.6.0" resolved "https://registry.yarnpkg.com/lodash.mapvalues/-/lodash.mapvalues-4.6.0.tgz#1bafa5005de9dd6f4f26668c30ca37230cc9689c" +lodash.merge@^4.6.0: + version "4.6.0" + resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.0.tgz#69884ba144ac33fe699737a6086deffadd0f89c5" + lodash.restparam@^3.0.0: version "3.6.1" resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805" @@ -5101,6 +5121,10 @@ strip-eof@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/strip-eof/-/strip-eof-1.0.0.tgz#bb43ff5598a6eb05d89b59fcd129c983313606bf" +strip-indent@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/strip-indent/-/strip-indent-2.0.0.tgz#5ef8db295d01e6ed6cbf7aab96998d7822527b68" + strip-json-comments@~2.0.1: version "2.0.1" resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-2.0.1.tgz#3c531942e908c2697c0ec344858c286c7ca0a60a" From d7321f9e0e6f704ae71fa817a7dec91accf7bdda Mon Sep 17 00:00:00 2001 From: Jan Kassens Date: Mon, 6 Aug 2018 08:16:02 -0700 Subject: [PATCH 6/6] Move macro export to babel-plugin-relay --- gulpfile.js | 3 +- package.json | 2 +- ...elayMacro.js => BabelPluginRelay.macro.js} | 1 - ...test.js => BabelPluginRelay.macro-test.js} | 10 +++--- .../BabelPluginRelayMacro-test.js.snap | 22 ++++++------- packages/babel-plugin-relay/package.json | 1 + .../modern/ReactRelayGraphQL.macro.js | 5 --- yarn.lock | 32 +++++++++---------- 8 files changed, 36 insertions(+), 40 deletions(-) rename packages/babel-plugin-relay/{BabelPluginRelayMacro.js => BabelPluginRelay.macro.js} (95%) rename packages/babel-plugin-relay/__tests__/{BabelPluginRelayMacro-test.js => BabelPluginRelay.macro-test.js} (70%) delete mode 100644 packages/react-relay/modern/ReactRelayGraphQL.macro.js diff --git a/gulpfile.js b/gulpfile.js index 7b4ff5cfd554f..6053159c23d70 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -18,6 +18,7 @@ const babelOptions = require('./scripts/getBabelOptions')({ '@babel/parser': '@babel/parser', '@babel/types': '@babel/types', 'babel-core': 'babel-core', + 'babel-plugin-macros': 'babel-plugin-macros', 'babel-generator': 'babel-generator', 'babel-generator/lib/printer': 'babel-generator/lib/printer', 'babel-polyfill': 'babel-polyfill', @@ -153,6 +154,7 @@ const builds = [ package: 'babel-plugin-relay', exports: { index: 'BabelPluginRelay.js', + macro: 'BabelPluginRelay.macro.js', }, bundles: [ { @@ -170,7 +172,6 @@ const builds = [ classic: 'ReactRelayClassicExports.js', compat: 'ReactRelayCompatPublic.js', index: 'ReactRelayPublic.js', - macro: 'ReactRelayGraphQL.macro.js' }, bundles: [ { diff --git a/package.json b/package.json index b7dc4b9c96dc0..49825d8269a1d 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "babel-cli": "6.24.1", "babel-core": "6.25.0", "babel-eslint": "9.0.0-beta.2", - "babel-plugin-macros": "2.0.0", + "babel-plugin-macros": "^2.0.0", "babel-plugin-tester": "^5.0.0", "babel-plugin-transform-async-to-generator": "6.24.1", "babel-plugin-transform-es2015-modules-commonjs": "6.24.1", diff --git a/packages/babel-plugin-relay/BabelPluginRelayMacro.js b/packages/babel-plugin-relay/BabelPluginRelay.macro.js similarity index 95% rename from packages/babel-plugin-relay/BabelPluginRelayMacro.js rename to packages/babel-plugin-relay/BabelPluginRelay.macro.js index 70b696e4524cd..8349ee3214201 100644 --- a/packages/babel-plugin-relay/BabelPluginRelayMacro.js +++ b/packages/babel-plugin-relay/BabelPluginRelay.macro.js @@ -4,7 +4,6 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @providesModule BabelPluginRelayMacro * @flow * @format */ diff --git a/packages/babel-plugin-relay/__tests__/BabelPluginRelayMacro-test.js b/packages/babel-plugin-relay/__tests__/BabelPluginRelay.macro-test.js similarity index 70% rename from packages/babel-plugin-relay/__tests__/BabelPluginRelayMacro-test.js rename to packages/babel-plugin-relay/__tests__/BabelPluginRelay.macro-test.js index c5b43a7cb248c..8ba0790f8a4fe 100644 --- a/packages/babel-plugin-relay/__tests__/BabelPluginRelayMacro-test.js +++ b/packages/babel-plugin-relay/__tests__/BabelPluginRelay.macro-test.js @@ -24,15 +24,15 @@ pluginTester({ works: ` 'use strict'; - const {graphql} = require('../../react-relay/modern/ReactRelayGraphQL.macro'); - const CompatProfilePic = require('CompatProfilePic'); + const {graphql} = require('../BabelPluginRelay.macro'); + const ProfilePic = require('ProfilePic'); - const CompatViewerQuery = graphql\` - query CompatViewerQuery($id: ID!, $scale: Float = 1.5) { + const ViewerQuery = graphql\` + query ViewerQuery($id: ID!, $scale: Float = 1.5) { node(id: $id) { ... on User { id - ...CompatProfilePic_user + ...ProfilePic_user } } } diff --git a/packages/babel-plugin-relay/__tests__/__snapshots__/BabelPluginRelayMacro-test.js.snap b/packages/babel-plugin-relay/__tests__/__snapshots__/BabelPluginRelayMacro-test.js.snap index 9c55df0ecbef5..d6a0e40805ed2 100644 --- a/packages/babel-plugin-relay/__tests__/__snapshots__/BabelPluginRelayMacro-test.js.snap +++ b/packages/babel-plugin-relay/__tests__/__snapshots__/BabelPluginRelayMacro-test.js.snap @@ -4,15 +4,15 @@ exports[`BabelPluginRelayMacro works: works 1`] = ` " 'use strict'; -const {graphql} = require('../../react-relay/modern/ReactRelayGraphQL.macro'); -const CompatProfilePic = require('CompatProfilePic'); +const {graphql} = require('../BabelPluginRelay.macro'); +const ProfilePic = require('ProfilePic'); -const CompatViewerQuery = graphql\` - query CompatViewerQuery($id: ID!, $scale: Float = 1.5) { +const ViewerQuery = graphql\` + query ViewerQuery($id: ID!, $scale: Float = 1.5) { node(id: $id) { ... on User { id - ...CompatProfilePic_user + ...ProfilePic_user } } } @@ -22,16 +22,16 @@ const CompatViewerQuery = graphql\` 'use strict'; -const CompatProfilePic = require('CompatProfilePic'); +const ProfilePic = require('ProfilePic'); -const CompatViewerQuery = function () { - const node = require('./__generated__/CompatViewerQuery.graphql'); +const ViewerQuery = function () { + const node = require('./__generated__/ViewerQuery.graphql'); - if (node.hash && node.hash !== '42a62b166f34144936e64b3c65e42c4d') { - console.error('The definition of \\\\'CompatViewerQuery\\\\' appears to have changed. Run \`relay-compiler\` to update the generated files to receive the expected data.'); + if (node.hash && node.hash !== 'b046a97b7823510c05083ebb114377f4') { + console.error('The definition of \\\\'ViewerQuery\\\\' appears to have changed. Run \`relay-compiler\` to update the generated files to receive the expected data.'); } - return require('./__generated__/CompatViewerQuery.graphql'); + return require('./__generated__/ViewerQuery.graphql'); }; " `; diff --git a/packages/babel-plugin-relay/package.json b/packages/babel-plugin-relay/package.json index b08399b55d29c..8a9d75c62a851 100644 --- a/packages/babel-plugin-relay/package.json +++ b/packages/babel-plugin-relay/package.json @@ -13,6 +13,7 @@ "bugs": "https://github.com/facebook/relay/issues", "repository": "facebook/relay", "dependencies": { + "babel-plugin-macros": "^2.0.0", "babel-runtime": "^6.23.0", "babel-types": "^6.24.1" }, diff --git a/packages/react-relay/modern/ReactRelayGraphQL.macro.js b/packages/react-relay/modern/ReactRelayGraphQL.macro.js deleted file mode 100644 index 48a4b34dda53b..0000000000000 --- a/packages/react-relay/modern/ReactRelayGraphQL.macro.js +++ /dev/null @@ -1,5 +0,0 @@ -// This module is named the way it is because babel-plugin-macros requires -// the source string to match the regex /[./]macro(\.js)?$/, and -// this module is imported in the test suite. - -module.exports = require('../../babel-plugin-relay/BabelPluginRelayMacro'); \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 0e2e20884b096..2c7469bfa1368 100644 --- a/yarn.lock +++ b/yarn.lock @@ -671,11 +671,11 @@ babel-plugin-jest-hoist@^23.0.1: version "23.0.1" resolved "https://registry.yarnpkg.com/babel-plugin-jest-hoist/-/babel-plugin-jest-hoist-23.0.1.tgz#eaa11c964563aea9c21becef2bdf7853f7f3c148" -babel-plugin-macros@2.0.0: - version "2.0.0" - resolved "https://registry.yarnpkg.com/babel-plugin-macros/-/babel-plugin-macros-2.0.0.tgz#fd3aee135f7dec0b82898b7c8f1aed6fa75f9af9" +babel-plugin-macros@^2.0.0: + version "2.4.0" + resolved "https://registry.yarnpkg.com/babel-plugin-macros/-/babel-plugin-macros-2.4.0.tgz#6c5f9836e1f6c0a9743b3bab4af29f73e437e544" dependencies: - cosmiconfig "3.1.0" + cosmiconfig "^5.0.5" babel-plugin-syntax-async-functions@^6.8.0: version "6.13.0" @@ -1445,14 +1445,13 @@ core-util-is@1.0.2, core-util-is@~1.0.0: version "1.0.2" resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7" -cosmiconfig@3.1.0: - version "3.1.0" - resolved "https://registry.yarnpkg.com/cosmiconfig/-/cosmiconfig-3.1.0.tgz#640a94bf9847f321800403cd273af60665c73397" +cosmiconfig@^5.0.5: + version "5.0.5" + resolved "https://registry.yarnpkg.com/cosmiconfig/-/cosmiconfig-5.0.5.tgz#a809e3c2306891ce17ab70359dc8bdf661fe2cd0" dependencies: is-directory "^0.3.1" js-yaml "^3.9.0" - parse-json "^3.0.0" - require-from-string "^2.0.1" + parse-json "^4.0.0" create-react-class@^15.6.0: version "15.6.3" @@ -3592,6 +3591,10 @@ jsesc@^2.5.1: version "2.5.1" resolved "https://registry.yarnpkg.com/jsesc/-/jsesc-2.5.1.tgz#e421a2a8e20d6b0819df28908f782526b96dd1fe" +json-parse-better-errors@^1.0.1: + version "1.0.2" + resolved "https://registry.yarnpkg.com/json-parse-better-errors/-/json-parse-better-errors-1.0.2.tgz#bb867cfb3450e69107c131d1c514bab3dc8bcaa9" + json-schema-traverse@^0.3.0: version "0.3.1" resolved "https://registry.yarnpkg.com/json-schema-traverse/-/json-schema-traverse-0.3.1.tgz#349a6d44c53a51de89b40805c5d5e59b417d3340" @@ -4440,11 +4443,12 @@ parse-json@^2.2.0: dependencies: error-ex "^1.2.0" -parse-json@^3.0.0: - version "3.0.0" - resolved "https://registry.yarnpkg.com/parse-json/-/parse-json-3.0.0.tgz#fa6f47b18e23826ead32f263e744d0e1e847fb13" +parse-json@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/parse-json/-/parse-json-4.0.0.tgz#be35f5425be1f7f6c747184f98a788cb99477ee0" dependencies: error-ex "^1.3.1" + json-parse-better-errors "^1.0.1" parse-passwd@^1.0.0: version "1.0.0" @@ -4905,10 +4909,6 @@ require-directory@^2.1.1: version "2.1.1" resolved "https://registry.yarnpkg.com/require-directory/-/require-directory-2.1.1.tgz#8c64ad5fd30dab1c976e2344ffe7f792a6a6df42" -require-from-string@^2.0.1: - version "2.0.1" - resolved "https://registry.yarnpkg.com/require-from-string/-/require-from-string-2.0.1.tgz#c545233e9d7da6616e9d59adfb39fc9f588676ff" - require-main-filename@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/require-main-filename/-/require-main-filename-1.0.1.tgz#97f717b69d48784f5f526a6c5aa8ffdda055a4d1"