Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ES6 import from root

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?

like image 233
Alias Avatar asked Apr 20 '15 17:04

Alias


People also ask

What is import path?

A third default finder searches an import path for modules. The import path is a list of locations that may name file system paths or zip files.

How do you write an import path?

As I have described in another article, an import path can be written as: import * as myImportModule from './myExports'; Here './myExports' is a module name, and it is written with a relative path. A module name can also be written with an absolute path, but we must configure our project to recognize it.

How can I conditionally import an ES6 module?

To conditionally import an ES6 module with JavaScript, we can use the import function. const myModule = await import(moduleName); in an async function to call import with the moduleName string to import the module named moduleName . And then we get the module returned by the promise returned by import with await .

What is ES6 import and export in JavaScript?

ES6 | Import and Export. The ES6 is a JavaScript standard. With the help of ES6, we can create modules in JavaScript. In a module, there can be classes, functions, variables, and objects as well. To make all these available in another file, we can use export and import.

How to create modules in ES6?

The ES6 is a JavaScript standard. With the help of ES6, we can create modules in JavaScript. In a module, there can be classes, functions, variables, and objects as well. To make all these available in another file, we can use export and import. The export and import are the keywords used for exporting and importing one or more members in a module.

Does NodeJS support ES6 import?

NodeJS does not support ES6 import directly, by default. If you try to use ‘import’ for importing modules it will throw an error. Good news is that NodeJS has started supporting ES6 import on an experimental basis.

Should you use relative paths for imports in ES2015?

The ES2015 module system is probably familiar to you by now. If you’ve used it, you might have faced the familiar relative path inclusion problem. Using relative paths for imports makes your code base very hard to maintain, not to mention the hassle required to figure out where the inclusion is relative to the current path.


1 Answers

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)

like image 186
Michael J. Zoidl Avatar answered Sep 22 '22 03:09

Michael J. Zoidl