I am porting some Node-based packages to React Native using ReactNativify (instead of the more 'hacky' rn-nodeify method) to browserify / shim Node API object in the dependency tree. I am developing in VSCode.
With ReactNativify you use babel-plugin-rewrite-require
in .babelrc
(or by using javascript in a rn-cli.config.js
+ transformer.js
):
{
...
"plugins": [
["rewrite-require", {
"aliases": {
"constants": "constants-browserify",
"crypto": "react-native-crypto",
"dns": "node-libs-browser/mock/dns",
"domain": "domain-browser",
"stream": "stream-browserify",
"_stream_duplex": "readable-stream/duplex",
"_stream_passthrough": "readable-stream/passthrough",
"_stream_readable": "readable-stream/readable",
"_stream_transform": "readable-stream/transform",
"_stream_writable": "readable-stream/writable",
... etcetera
}
}]
...
}
(see a full example here)
This works rather well, and I get nice code completion / IntelliSense in VSCode, etc.
Now I want to use Node's Stream in a MyStream.js
like so:
import { Duplex } from 'stream'
class MyStream extends Duplex {
// ... implementation
}
The first time around this seemed to work. I got code completion, ability to 'Go to type definition`. All seemed well.
I like type safety and 'intellisense-to-the-max' so decided to convert the project / code to typescript. No problem here.
But when changing MyStream.js
to MyStream.ts
the IDE complained that package 'stream' was not found.
I tried various things without success, then decided to convert back to javascript, but now the - originally working - stream import was not finding the code anymore. Changing back to Typescript, now there is no error-checking at all anymore. In short, it all feels very hacky and unstable.
My questions:
- Does someone have working examples of using Node's 'stream' with React Native?
- Are there special considerations for getting code completions to work properly in VSCode (both regular ES6 + Typescript)?
- Can I use either ReactNativify or rn-nodeify in Typescript the same way as with regular js?
[Update: One thing solved. Restarting VSCode will lead it to do code validation again]