ianstormtaylor / superstruct

A simple and composable way to validate data in JavaScript (and TypeScript).
https://docs.superstructjs.org
MIT License
7.02k stars 224 forks source link

Rename files to `cjs.js` for compatibility with React Native #1085

Closed steveluscher closed 2 years ago

steveluscher commented 2 years ago

Importing @solana/web3.js in React Native throws the following error.

error: Error: While trying to resolve module `superstruct` from file `/Users/sluscher/mobile-wallet-adapter/js/node_modules/@solana/web3.js/lib/index.browser.cjs.js`, the package `/Users/sluscher/mobile-wallet-adapter/js/node_modules/superstruct/package.json` was successfully found. However, this package itself specifies a `main` module field that could not be resolved (`/Users/sluscher/mobile-wallet-adapter/js/node_modules/superstruct/lib/index.cjs`. Indeed, none of these files exist:

  * /Users/sluscher/mobile-wallet-adapter/js/node_modules/superstruct/lib/index.cjs(.native|.android.js|.native.js|.js|.android.json|.native.json|.json|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx)

You can see that the React Native packager expects a .js extension in addition to .cjs

If superstruct could rename the CommonJS bundles from .cjs to .cjs.js that would make it Just Work™ for React Native folks!

More details on the problem here, if you're interested: https://github.com/facebook/metro/issues/535

ianstormtaylor commented 2 years ago

@steveluscher interesting, I'd be down for a PR if you want to make this change!

steveluscher commented 2 years ago

For sure. I would have done that in the moment, but I had too much mental stack to switch. If I get to it first, cool; if someone reading this wants to take a crack at it, cooler!

steveluscher commented 2 years ago

PR'd.

momolarson commented 2 years ago

Hi everyone, thanks very much for addressing this! I was having this issue using solana/web3.js and stumbled upon this. I downloaded @steveluscher's branch and tested it out locally and it was able to build.

steveluscher commented 2 years ago

Awesome! Let's shipit!

valentinmadrid commented 2 years ago

Hi @steveluscher I'm experiencing the exact same problem: iOS Bundling failed 409ms While trying to resolve module superstruct from file... I have installed your branch yarn add git+https://github.com/steveluscher/superstruct.git#react-native-compatibility but still get the same error. Do you by any chance know if there's some additional configuration to do/link me to your "Getting Started" README ?

Thanks a lot in advance!

inhumantsar commented 2 years ago

@valentinmadrid the package needs to be built, it can't be cloned directly from git. not sure if this is the right way, but i used the same yarn add ... as you, but then also cloned steve's branch to a separate location. in that directory, i ran yarn install && yarn build. this produced a directory named lib. i copied that lib directory to my project's node_modules/superstruct/ directory.

this seems to have worked but i immediately ran into other issues. i don't think they're related though.

steveluscher commented 2 years ago

We'll just have to be patient! Once @ianstormtaylor mashes the merge button on #1086 and does a release, we should all be able to update to that new release from npm.

charIeszhao commented 2 years ago

Hi @ianstormtaylor, we have run into the same issue and really expect a new release of superstruct to address this. Thanks heaps!

AndonMitev commented 2 years ago

Guys any resolution, i'm facing exactly same error:

"react-native": "0.69.6",
"@solana/web3.js": "1.66.1"
steveluscher commented 2 years ago

@AndonMitev: https://twitter.com/steveluscher/status/1565232097044099073

AndonMitev commented 2 years ago

Thanks for this but i'm on "@solana/web3.js": "1.66.1" with react-native: "0.69.6"

steveluscher commented 2 years ago

I know! You’ll have to upgrade to 0.70+ for support going forward. We’ve abandoned the 0.69- line.