-
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
Add basic external transformation support to relay-compiler #1710
Conversation
Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla - and if you have received this in error or have any questions, please drop us a line at cla@fb.com. Thanks! If you are contributing on behalf of someone else (eg your employer): the individual CLA is not sufficient - use https://developers.facebook.com/opensource/cla?type=company instead. Contact cla@fb.com if you have any questions. |
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks! |
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks! |
Very interesting! Could you ensure lint passes? |
@leebyron it fails on a global variable which linter doesn't know ( |
While I've had very good success so far just running relay-compiler over my .ts and .tsx files, one thing relay-compiler cannot parse is a |
I'm starting work on simplifying the graphql extraction to no longer fully parse which should also make it work for ts! \o/ |
Any updates here? This is super-interesting. |
let moduleImpl: TransformFactory | ||
try { | ||
// $FlowFixMe flow doesn't know about __non_webpack_require__ | ||
moduleImpl = (__non_webpack_require__(moduleName): TransformFactory) |
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.
cc @wincent - could we use this to require the persist module?
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.
Probably. I need to re-test my PR. If it doesn't work can try this "one weird trick".
Sorry for the delay here. My plan to pull out the GraphQL tags using a simplified state machine parser didn't work out because I wanted to avoid false positives such as a graphql`` tag appearing inside a string or other edge cases. I'll send a commit soon that enables parsing of pure .graphql files using a second parser config instead of |
mocha has an option called after that, node will consult the right transpiler when it resolve a module. for all registered module files like .js/.jsx/.ts/.tsx files node will call the corresponding transform method before putting into its module system. so from mocha's point of view, everything is plain es5. i think its a good solution. dont call their |
@kassens (or anyone) have you figured out a work-around in the meantime. Without the ability to run |
@voxmatt I am currently working on this very problem. My workaround was to move the graphql call to a .js file and then import that in my component.ts. You have to set |
Rebased this branch and fixed the linter issues https://github.com/s-panferov/relay/pull/1 |
@jdolle @voxmatt Could you explain to me the complication you're trying to fix? We have this simple build config set up under npm build and it works well with Relay, but there may be an optimization we're lacking.
Our project is still fairly simple, are there aspects of more complicated projects you're seeing where this setup breaks? In our tsconfig.json we use these to make sure graphql tags are properly picked up by Babel:
|
When using webpack-dev-server, there is no dist folder to run the relay-compiler on. We need either (a) some way to run the compiler against ts files without failing (this is preferable and seems pretty close to doable since it's just getting hung up on superfluous syntax) or (b) run the compiler in a step that webpack runs. Previously, with relay-classic, this worked because relay relied on the babel plugin alone and babel is a loader in our webpack config; so, the babel step was handled in the dev-server setup. |
@voxmatt Very much appreciate the explanation - clipped for when we're building out the web version of our app. |
Hey @wincent, could you please look at this issue? We should make a decision as to whether this fits into the relay-compiler or not |
Also - this PR no longer merges cleanly. @s-panferov would you mind rebasing? |
I think it makes sense for something like this to be in "relay-compiler". Even if we don't go all the way to having a general pluggable transform mechanism like this, we know the concrete TypeScript use-case would be valuable to solve. For me the main question is which of these is most desirable:
My gut sense that the second path (the one implemented in this PR) feels like a good approach. We don't yet have a good story for how you can swap pieces in and out of the compiler (short of making your own module which involved a bit of copy-pasta), and adding a |
In our case I decided to go with a very simple stopgap solution and start experimenting with Babel 7, which has a TS parser, asap. |
Should this feature be called something other than |
I ported this patch to relay 1.3.0, get it here: miracle2k@eda4f6b |
@alloy Do you have an update on using Babel 7? We use TypeScript with Relay Classic and this issue is preventing us from upgrading to Modern. |
@jimkyndemeyer Oh yes! I had ported our React Native app to use Haul, which is webpack based, and was able to use Babel 7 without The main issue I encountered was that the TS and Flow plugins for Babel can’t be active at the same time, so I had to strip all The reason I haven’t brought it to completion yet is that we decided to switch back to the default RN packager (Metro) and alas I haven’t yet been able to get Babel 7 to work with that setup, but I didn’t spend much time on that yet either. If you use webpack, this shouldn’t be an issue for you and you can largely follow what I did in that linked PR. Addendum: the Babel 7 TS plugin does not support namespaces and const enums, so if you require those you’re out of luck. |
Any news on this? Would be great if we got proper TS support in the Relay compiler. I've written a simple Typescript transformer which replaces the babel relay plugin. You can use it if you want to use the TS compiler instead of Babel 7. |
Hi all .. any news on this? many thanks |
As part of work I’m finishing up over the week to add TS type info to artefacts, I’m also working on a very simple plugin API, but in fact my work will also already support TS parsing by using Babel 7. |
For people subscribed to this PR, I just pushed a new PR that adds plugin support and a link to a TS plugin. #2293 |
Wow, I don’t know how I could have unassigned @wincent 👻 🤷♂️ |
Summary: Closes #2073. Closes #1710. In preparation for #2073 and [as requested by kassens](http://twitter.com/kassens/status/946811879204245504), this adds language plugin support to relay-compiler. The whole plugin aspect is really just a matter of consolidating all language specific APIs in the [`RelayLanguagePluginInterface.js` file](https://github.com/alloy/relay/blob/d17bfdb18fe45bae531b13487ba5b9efa1c96171/packages/relay-compiler/language/RelayLanguagePluginInterface.js) and moving all of the pre-existing JS/Flow related code around to conform to that plugin interface. i.e. nothing should have changed in the existing JS/Flow handling. The one actual additions are: * making relay-runtime able to [load artifacts that are ES6 modules](https://github.com/facebook/relay/pull/2293/files#diff-2c18fc91f11394e307550103afff9722R56) (with default exports) * the ability to store all artifacts in a single directory (`artifactDirectory`), such that fragment references can easily be imported from other artifacts without the need for the Haste module system. i.e. all imports assume the imported module is a sibling of the requesting module. This should also improve the experience of JS/Flow users. With this plugin infrastructure in place, kastermester and I have been able to build a TypeScript plugin https://github.com/kastermester/relay-compiler-language-typescript. (You can find example artifacts [here](https://github.com/kastermester/relay-compiler-language-typescript/tree/master/example/ts/__generated__).) ---- Things that are left to do: - [x] Document all of the types in the plugin interface - [x] Remove version changes from `package.json` files - [x] Rebase on latest upstream master - [x] Verify that before and after JS/Flow artefacts are identical But these are not critical to being able to start reviewing and providing feedback. Pull Request resolved: #2293 Reviewed By: kassens Differential Revision: D6962199 Pulled By: jstejada fbshipit-source-id: b927a0c02eac914bce6d46ce25d90433fc0bb98f
Hello, thanks for the great library. We are using TypeScript for our codebase and it will be very useful to enable custom pre-transformations for a source code before parsing, so we can compile our typescript modules without complex configuration. The PR adds such support.
It is a very basic implementation for discussion purposes.
Proposed usage:
You can find example transformer implementation here: https://github.com/s-panferov/relay-compiler-typescript/tree/master