Galooshi / import-js

A tool to simplify importing JS modules
MIT License
525 stars 55 forks source link

My import-js can't find a component unless it runs on atom. #392

Open cndlhvn opened 7 years ago

cndlhvn commented 7 years ago

I use node 6.5.0 and import-js 2.2.0

I'm trying to insert component path to React file. This is the my react project structure.

├── .importjs.js
├── package.json
├── public
│   ├── favicon.ico
│   └── index.html
└── src
    ├── App.css
    ├── App.js
    ├── App.test.js
    ├── components
    │   ├── Button.js
    │   └── Input.js
    ├── index.css
    └── lindex.js

I want to insert Button component path to the src/App.js This is the code of Button.js

import React from 'react';

const Button = (props) => <button>ok</button>
Button.propTypes = {}
Button.defaultProps ={}
module.exports = Button;

I run this command in the application root dir. importjs word Button src/App.js

And I caught this message. {"messages":["No JS module to import for Button"],"f... Import-js can't find out the Button component.

Next, I open the src/App.js with Atom editor. I type Cmd+Shift+j two times. There are two messages.

"No JS module to import for Button" "Importd ./components/Button"

atommessage

After that, I run the command again.

importjs word Button src/App.js

I caught this message. {"messages":["Imported ./components/Button"],"fi...

Import-js could find out the Button component. Why did not import-js find the button component before that?

trotzig commented 7 years ago

This is unfortunate. It has to do with the cache being cold. On first use, import-js will kick off a background thread indexing modules in your project structure. So on first use, the cache is empty. On the second try, the cache has been populated.

I'm not happy with how this UX ended up like. Due to some technical decisions made along the way, it's a little tricky to know when the cache is done. If you have ideas for how to solve this, I'd be happy for some input!

The cli tool is somewhat unusable before you've populated the cache. The background thread will kick off as usual when the cache is empty, but due to how the command immediately finishes, the thread will usually be cancelled right away.