Skip to content
This repository has been archived by the owner on Mar 10, 2024. It is now read-only.

Issues with platform-specific React Native files #4

Closed
salockhart opened this issue Jun 3, 2020 · 12 comments
Closed

Issues with platform-specific React Native files #4

salockhart opened this issue Jun 3, 2020 · 12 comments
Labels

Comments

@salockhart
Copy link

Hey!

I was running this on my React Native project, and I noticed that it wasn't able to follow an import such as ./User and resolve it to ./User.ios.tsx and ./User.android.tsx.

I realise this is an edge-case and might be difficult to solve since IIRC these special extensions are handled by Metro. Just wanted to surface this in case anyone else has the same issue. Luckily I have very few platform-specific files so this tool is still super valuable.

Thanks!

@smeijer
Copy link
Owner

smeijer commented Jun 3, 2020

Thanks for letting me know. I'm sure it's fixable.

Do you know of any open source repo that has a similar problem/file structure, so that I can test it on that?

@salockhart
Copy link
Author

Hey @smeijer. I'm not aware of too many good open source examples, so I've created a small dummy repo to test on:

https://github.com/salockhart/RNPlatformSpecificCodeExample

It's super barebones but should suffice. Here's an example of the output:

       summary               unimported v1.0.6
────────────────────────────────────────────────────────────────────────────────────────
       entry file          : index.js

       unresolved imports  : 1
       unused dependencies : 0
       unimported files    : 3


─────┬──────────────────────────────────────────────────────────────────────────────────
     │ 1 unresolved imports
─────┼──────────────────────────────────────────────────────────────────────────────────
   1 │ ./src/Body/Body
─────┴──────────────────────────────────────────────────────────────────────────────────


─────┬──────────────────────────────────────────────────────────────────────────────────
     │ 3 unimported files
─────┼──────────────────────────────────────────────────────────────────────────────────
   1 │ src/Body/Body.android.tsx
   2 │ src/Body/Body.ios.tsx
   3 │ src/Container/Container.tsx
─────┴──────────────────────────────────────────────────────────────────────────────────

@smeijer
Copy link
Owner

smeijer commented Jul 21, 2020

Thanks for the repo. That definitely helps.

I do have a hard time understanding the dependency tree here though. Take the Body example, we can have:

Body.tsx
Body.android.tsx
Boby.ios.tsx
Body.native.tsx

When I have an import x from './Body/Body' statement, does that mean that they are all imported? Or can there still be one unused?

And do I need all of those files? Or can I have any of those at random?

@salockhart
Copy link
Author

The import gets resolved at build time, so import x from './Body/Body' resolves to Boby.ios.tsx if you are targeting iOS, and Boby.android.tsx if you're targeting Android.

I don't think that you would have both Body.tsx and Body.android.tsx/Body.ios.tsx, and I'm not sure what the behaviour would be there.

Body.native.tsx, IIRC, is used to differentiate between native mobile projects and web projects, which might have files like Boby.web.tsx

@smeijer
Copy link
Owner

smeijer commented Jul 21, 2020

so import x from './Body/Body' resolves to Boby.ios.tsx if you are targeting iOS, and Boby.android.tsx if you're targeting Android.

But only if they exist, and if they do not exist, Body.tsx will be imported. Correct?

@salockhart
Copy link
Author

Yes that is correct

@smeijer
Copy link
Owner

smeijer commented Jul 21, 2020

I think I have done it. I'll test it a bit more, and will release it this week.

Thanks for the info!

@smeijer smeijer added the feature label Sep 1, 2020
@geraintwhite
Copy link

This would be really handy to have for RN projects. Is there any update on the progress?

@dkarter
Copy link

dkarter commented May 13, 2021

@smeijer Thank you so much for looking into this! This feature would be super useful for React Native. If you need any help I'm happy to lend a second pair of eyes!

@smeijer
Copy link
Owner

smeijer commented Aug 7, 2021

Sorry, even though I had a working version, it took me a while to commit. Mostly because I didn't really like the solution.

I believe I've now found a better way to support this, and the commit has been pushed. It's way more generic, and adds support for build configs like react native, but also other projects that have multiple build targets.

I'll release this soon. All that's left is add some presets / auto detection so that you don't need to setup complicated unimportedrc config files.

@smeijer smeijer closed this as completed Aug 7, 2021
@smeijer smeijer reopened this Aug 7, 2021
@smeijer
Copy link
Owner

smeijer commented Aug 7, 2021

Wrong button 😅

@smeijer
Copy link
Owner

smeijer commented Aug 8, 2021

Released in v1.18 😎 🚀

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants