-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support babel-plugin-macros #2171
Changes from 1 commit
948b24f
40e513d
9bb9f2b
072b937
76e4a38
acf85fa
d74c37e
d7321f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -162,7 +162,8 @@ const builds = [ | |
exports: { | ||
classic: 'ReactRelayClassicExports.js', | ||
compat: 'ReactRelayCompatPublic.js', | ||
index: 'ReactRelayPublic.js' | ||
index: 'ReactRelayPublic.js', | ||
macro: 'ReactRelayGraphQL.macro.js' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This outputs |
||
}, | ||
bundles: [ | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
"babel-core": "6.25.0", | ||
"babel-eslint": "7.2.3", | ||
"babel-generator": "^6.26.0", | ||
"babel-macros": "1.2.0", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
"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", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the pointer. I didn't address this yet, as most of the options only pertain to Compat and Classic. Maybe @gaearon can help answer whether Compat and Classic should be supported? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If configuration is an edge case, I'd suggest omitting it for now. That's something that can be added later and I think it's better to not slow down the merge-ability of this PR for something that not many folks will need/use. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we would want to support classic and compat for anything new. The rest of the options are only to allow us to use the same plugin internally at FB and shouldn't be needed in OSS. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have you considered using babel-plugin-tester? It reduces the amount of boilerplate and the snapshot is a bit more insightful. Here's an example with a macro and here are the snapshots. |
||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`BabelPluginRelayMacro works 1`] = ` | ||
" | ||
'use strict'; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unlike in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the import needs to be retained (or replaced with something else) then your macros can get the Program node from the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The import is not needed in Modern (classic pulls some runtime helpers from it). |
||
const CompatProfilePic = require('CompatProfilePic'); | ||
|
||
const CompatViewerQuery = function () { | ||
return require('./__generated__/CompatViewerQuery.graphql'); | ||
};" | ||
`; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('../../babel-plugin-relay/BabelPluginRelayMacro'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This module is named the way it is because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You might want to add that as a comment in the file 👍 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you want to, you could add the babel-macro badge: