-
Notifications
You must be signed in to change notification settings - Fork 205
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
Support for React Native #29
Comments
To be honest, I never tried it on react-native because I still never used Native yet.
It looks like the plugin doesn't transform the path correctly. The path points to Do you have by any chance a small project to easily reproduce the issue? |
Hey there, I created a test project: https://github.com/adamterlson/babeltest Thank you for your help! By the way, if you like React and have interest in native app development at all, check out React Native. It's super awesome. :) |
I am very interested in React Native ;-) Just couldn't have the time to really dig into it yet. Thanks for the setup though, I'll try to see what I can do to add the RN support. |
After a quick investigation, it seems the plugin is never used. That's why the path is not updated and RN cannot find the module at I'm still not sure yet how to properly debug the babel config/loader inside react native. |
Yeah, I am not sure how to do that either. Wonder why it's not used... Hmmm. Thanks for looking into it though! |
@adamterlson i don't believe it will work with react native because react-native has its own module aliasing and resolution approach based on their own dependency graph tools see: https://github.com/facebook/fbjs#usage https://github.com/facebook/fbjs/blob/master/scripts/babel/rewrite-modules.js |
@datapimp Well that's a bummer. Good find, thank you for the information! So I take it then that the fbjs babel rewrite-modules comes first before the user-specified babel plugins? Sure would be nice if it were possible to reverse that order.... |
Yep, from my investigation, it comes first so the plugin is not even executed because the path is not correct. I opened a ticket in react-native to see what we could do. facebook/react-native#6118 |
Awesome, thank you very much @tleunen. |
So after reading through the issue thread (facebook/react-native#6118) it seems not part of the game plan to add this before/after hook any time soon, and at any rate, the issue is not with module-alias. They recommend not to use ProvidesModule; however, I needed to move forward so I thought I'd share what I did in case anyone else finds this thread and wants to live dangerously. First, I'm assuming that once fbjs is removed from RN (which is planned and probably why they don't want people using providesModule) then module-alias will work as expected. I did some experimenting and found that providesModule doesn't just take "MyModule" as in the example, you can give it any string, including something that looks like a path that corresponds to its location:
In this way, I can actually require this module from a component in the precise way that module-alias would allow: import * as UserActions from 'app/redux/modules/user'; So, once fbjs goes away, I'll delete all the providesModule lines and it should just keep working with an alias set up for "app". That's the theory, anyway. Thank you @tleunen and @datapimp for your help! |
Is it now possible? facebook/react-native@e6cb02d |
Hmm.. Maybe.. If someone can test it. Otherwise I'll try to find some time to give it a try |
@tleunen, @adamterlson It still does not work. The RN packager somehow interferes and tries to resolve relative to I added this to my
And this to
For comparison: if I add So if we could somehow prevent the RN packager from adding the Sorry that I am so non-technical here. I just do not have sufficient understanding of what is going on here to be more precise. I tested with RN 21. |
Is there any solution we can do? |
It works fine for me with RN 0.24 on Linux. My
And it works with just a regular |
That's awesome! @adamterlson can you confirm it works for you as well? |
Yup! Confirmed! 🎉 |
@adamterlson @thatsmydoing: You used the Did you by any chance also make |
Yes, react-native preset is the only other thing I have in my babelrc. I haven't really tried to use flow, so I can't help there |
@consideRatio Sorry I haven't had much luck using flow for RN. I get all sorts of problems. Sorry I can't help! |
I got this to work with Flow by using this package and then additionally symlinking my |
Previously this module was worked for me with RN 0.25. Had anyone confirmed with RN 0.27? Edit:
Edit: |
Could you try with the previous version to see if it's a new issue with the latest version of the plugin? |
It's work on babel-plugin-module-alias version 1.4 with RN 0.25 (+ React 0.14). |
I don't know what I did exactly, but now I don't get this issue again 🎉 LOL |
@pewh Can you share your versions on
When I try UPDATE: Deleted the |
For @svicalifornia and folk wanting to get this up and running with react-native, running babel-plugin-module-resolver version 2.7.0 and react-native 0.43.0 yarn add babel-plugin-module-resolver --dev Note - this is not "babel-plugin-module-alias", which is capped at version 1.6.0 {
"presets": [
"react-native"
],
"plugins": [
[
"module-resolver",
{
"root": ["./src"],
"alias": {
"components": "./components"
}
}
]
]
} Note -This tool goes under With the above configuration it assumes a directory structure of:
You can then import your custom components anywhere within your react-native application with: import Button from 'components/button'; Note - You may need to clear your cache in some cases:
|
@svicalifornia my .babelrc is quite simple:
|
Still running into this issue when using |
Still having this issue using RN 0.44 + webpack |
@AlanFoster I used your solution and it's great until it fails. Resetting the cache ( Could you explain more about: "Note - You may need to clear your cache in some cases:" |
You might consider using Haul instead of react-native-packager (or metro-bundler as it's called now). It doesn't require resetting cache to work with this plugin. |
Same issue here, I'm on windows... {
"presets": ["react-native"],
"plugins": [
["module-resolver", {
"root": ["./src", "./core"]
}]
]
} |
I think the core of the issue is that when React.xcodeproj automatically launches the Packager, it does so from the directory of the packager. Any sane person is going to launch it from the root of their project, and that is where all of the Here's what I did to sleep better at night:
Please note that your path to launchPackager.command will likely be different. After doing all of this, now my Next step: convince the React-native guys to change their launch point of the packager so that none of this is necessary. |
For me works this one: step 1 step 2 Just take note: |
I cannot get this to work with
Though this is kind of awkward... |
Its not working with "require" fn but works with "import". .babelrc
package.json
Works with import at top: |
@tayfunyasar What if you move your require at the top and use the variable in your component instead? |
Thanks for your answer @tleunen. Its still not working with require. I also tried import it, it worked with it. worked:
|
Still not working with the following configuration on windows and react native 0.47.2 {
"presets": ["react-native"],
"plugins": [
[
"module-alias", {
"root": ["./app"],
"alias": {
"screens": "./screens"
},
"extensions": [".js", ".ios.js", ".android.js"]
}
]
]
} I finally "solved" the problem by adding package.json in the app/screens folder with {name: "screens"} and declaring that folder as a local dependency in my main package.json. |
@emusgrave after following the steps you have mentioned we got this error in xcode. |
I was having the issue where the packager would be looking for
When I updated RN, that
This was my mistake. It was this script that was making the packager pick up aliases from inside
|
Coming way after the battle, but still. I managed to finally make it work by adding
Then starting metro by running |
So if I understand correctly, it is not possible to use this plugin with |
Resetting the cache once did the trick for me. |
(tested with RN 0.52.0 and module-resolver babel plugin) For react-native cli command, the working directory for Babel plugins is a directory For instance, the following babel configuration (check root parameter): // .babelrc
{
"presets": ["react-native"],
"plugins": [
["module-resolver", {
"root": [".", "./src"]
}]
]
} should be: // .babelrc
{
"presets": ["react-native"],
"plugins": [
["module-resolver", {
"root": [".", "./src", "../..", "../../src"]
}]
]
} The original paths can be left as they are, as some IDEs (VB Code + RN extension) have their own Metro builder with properly configured project's working directory and no issues with module-resolver babel plugin. |
Reporting: To react native use the package.json fix, after having run To reactjs it worked: https://github.com/entwicklerstube/babel-plugin-root-import Greetings ✌ |
@emusgrave could you please show, where you place your files? I've placed the two files into /ios/-Directory.
and adjusted the Run-Script like that: I'm not sure, where the $SRCROOT points to. |
I am solve this problem. Just add to metro.config.js attribute package.json.
metro.config.js. Add
tsconfig.json
babel.config.js
App.tsx
|
Hey there. This is not an issue per say, more a cry for help.
Have you by some crazy chance attempted to use this plugin with React Native? It simply just doesn't seem to work. I opened a Stack Overflow issue about this. Any help would be greatly appreciated! If you're at a loss as I am, please feel free to ignore and close this issue.
The text was updated successfully, but these errors were encountered: