-
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
RFC: Teach compiler --persist
option
#1846
Conversation
This is ugly as hell, but throwing it up there as an RFC anyway. In my own side-project, I went down the path of forking |
--persist
option--persist
option
// Using eval here to prevent webpack from trying to rewrite the require and | ||
// failing. | ||
const persistQuery = persistModule && | ||
eval(`require(${JSON.stringify(persistModule)})`); |
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.
Ugh.
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.
Isn't there something like require.actual()? Or maybe alias require to something else?
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.
In vanilla node, there is not (that's a Jest thing). I looked on the interwebs, but I couldn't find anything. Moving to webpack 2 would provide us with some more options, I think, but that's obviously a bigger change.
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.
What about aliasing require so webpack doesn't transform it? Or shell out to a separate process?
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.
Good idea. Let me see if aliasing works.
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.
Seems like it does work (at least, build runs without blowing up); webpack lets the line in question through as:
var persistQuery = persistModule && global['require'](persistModule);
in the built output.
I was going to send a PR for this too, definitely something we should add. |
Gonna import this to run Flow tests internally (my local set-up is borked right now). |
Heh, or I could just look at Travis... |
Webpack actually aliases |
I was hopeful that this would work because the built output looks like this:
Note how it hasn't mangled
(Whoops submitted too soon). I just found a few mentions in my Google searches to "dynamic requires" not being enabled by default in v2. (Example). |
Do you need to run the |
Cross-referencing: #1710 (review)
True. My recollection is that we're doing this as an optimization to minimize cold-start time. |
Sample usage: relay-compiler --src ./src --schema schema.graphql --persist scripts/persistQuery.js Where `persistQuery.js` looks like this: ``` function persistQuery(text) { var id = someLogicToPersistQueryText(text); return Promise.resolve(id); } ``` The alternative in the absence of this option is to copy-paste the entire `RelayCompilerBin` implementation just to override that bit. Note that webpack makes this ghastly — necessitating the use of `eval` — because it wants to rewrite the `require` of the passed in module, which of course fails because it is dynamic. Google search turns up a bunch of results like this one lamenting the situation: https://stackoverflow.com/questions/30575060/require-js-files-dynamically-on-runtime-using-webpack Apparently it's better in webpack 2.
This time actually tested it and confirmed that it works.
Ship it! |
@wincent has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator. |
@unirey No idea. cc @leebyron |
I imported it but it has since grown stale. I need to dust it off and rebase it. A question was raised into whether the change as I had made it is would work everywhere, and I haven't had time to (re-)validate that myself yet. |
@unirey: not to dismiss your point (I'd like to see this option merged, obviously), but note that it's not really creating your own version of Relay: you just need to fork a single file (and in fact, internally at Facebook we do exactly that: we have a custom "compiler" executable that is configured to suit our specific needs). I did this for a side-project of mine, for instance: |
I think it's probably reasonable to expose that, although we've historically been very conservative and minimal in terms of what we include in the public API. The main question is whether this stuff should be exposed at the |
Summary: Fixes #1927 wincent This will export `formatGeneratedModule` as discussed in #1846 (comment). I exported it in `relay-compiler` (as opposed to `graphql-compiler`) since that is where it is currently located. Closes #2054 Reviewed By: kassens Differential Revision: D5690522 Pulled By: wincent fbshipit-source-id: bc7fd24152d1a1f62e28432fe3e7e1d6b7fef158
Hello, everyone. Is this going to be merged actually? That's a nice option I'd like to use instead of forking.. |
I published my own package relay-compiler-plus which supports persisted queries and compiles graphql-js schema as well. Hope that helps for now. |
It would be really great to have support from the core to proceed with this! |
@jstejada any plans for this feature? |
I haven't spoken to the team yet, but my hunch is that if someone wanted to update this diff to make |
@yusinto can u send a PR making |
Yes I will submit a pr soon. Thanks for the suggestion @sibelius |
Done. Here it is. |
To avoid confusion, I’ll close this PR for now in favour of #2354. |
Sample usage:
Where
persistQuery.js
looks like this:The alternative in the absence of this option is to copy-paste the entire
RelayCompilerBin
implementation just to override that bit.Note that webpack makes this ghastly — necessitating the use of
eval
— because it wants to rewrite therequire
of the passed in module, which of course fails because it is dynamic. Google search turns up a bunch of results like this one lamenting the situation:https://stackoverflow.com/questions/30575060/require-js-files-dynamically-on-runtime-using-webpack
Apparently it's better in webpack 2.