Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
128
rated 0 times [  130] [ 2]  / answers: 1 / hits: 87512  / 9 Years ago, mon, april 20, 2015, 12:00:00

I'm currently playing around with React Native. I'm trying to structure my app, however it's starting to get messy with imports.



--app/
-- /components
-- Loading.js
-- index.ios.js


Now, within my index.ios.js i'm able to simply do:



import Loading from './components/Loading';



However, when I start to create more components, with a deeper directory struture, it starts to get messy:



import Loading from '.../../../../components/Loading';



I understand the preferred solution would be to make private npm modules for things, but that's overkill for a small project.



You could do a global.requireRoot type solution on the browser, but how do I implement this with import?


More From » node.js

 Answers
27

Had the same issue with React.
So i wrote some plugin for babel which make it possible to import the modules from the root perspective - the paths are not shorter - but it's clear what you import.



So instead of:



import 'foo' from '../../../components/foo.js';


You can use:



import 'foo' from '~/components/foo.js';


Here is the Plugin (tested and with a clear README)


[#66989] Saturday, April 18, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sheylap

Total Points: 141
Total Questions: 99
Total Answers: 99

Location: Sint Maarten
Member since Tue, Mar 29, 2022
2 Years ago
;