[Feature Request] React Native Compatibility
See original GitHub issueHello, this is a new feature request!
I’m trying to get this library to work with React Native, and having a few issues. I’d love to work with the team on improving this if allowed so they can keep focusing on other priorities.
After implementing barrels, I discovered a couple of issues caused by a (IMHO very silly) React Native behaviour: if it sees any require('...')
call, it tries to load the module in this require
. It doesn’t matter whether this code is even inside if(false)
or try { ... } catch { ...}
.
Since most of these require()
s call built in Node stuff, and React Native runtime is not Node, it fails.
See https://github.com/facebook/react-native/issues/5079 for reference.
So, it sees stuff from node_support/
and crypto_utils.ts
, and shows an error for loading them, even if I’m not using these.
Each of these 2 can have different ways to handle.
crypto_utils.ts
In crypto_utils.ts
, the line that breaks it is let crypto = require('crypto');
.
We can workaround the issue by doing something like:
const nodeRequire = require;
...
let crypto = nodeRequire('crypto');
This is sweet and easy, and if you approve, I’ll create a quick PR for it.
node_support
The contents of node_support
is meant to be for Node. We can use the same workaround as above, but it’s not clean and quite silly actually.
If you approve, again, it might be a temporary solution.
Another temporary solution is to remove node_support
from the main barrel. This way Node users will have to import from @openid/appauth/built/node_support
(which has its own barrel), but having to import from built
is quite silly.
The reason we have this issue is that TypeScript cannot have a base folder for looking for submodules except the package root - see https://github.com/Microsoft/TypeScript/issues/8305#issuecomment-343003095 as mentioned in https://github.com/openid/AppAuth-JS/pull/34#issuecomment-343003850
we can solve the built
issue by compiling in-place instead of having src
and built
folders. Or, when publishing the package, implement a workflow where we copy the package.json
file to built
folder and publishing from inside that folder.
I can have a spin at this workflow (publishing from built
but I cannot test it as I don’t have publish permissions. So, we either:
- Move ts files to package root and compile in place
- Change
require
calls innnode_support
for now - Remove
nnode_support
from the root barrel for now - If the team has capacity, implement the workflow that publishes from inside
built
Happy to do pull request for any option you choose, or provide better clarification if this issue is a bit overwhelming.
Issue Analytics
- State:
- Created 6 years ago
- Comments:10 (3 by maintainers)
Top GitHub Comments
I wrote an article for scotch.io last week that shows you how to use React Native App Auth to add authentication to a React Native application. Hope this helps! https://scotch.io/tutorials/build-a-react-native-app-and-authenticate-with-oauth-20
@paulbrittain That’s a good question for Formidable, the folks that run React Native App Auth.