Skip to content
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 jsconfig.json #556

Closed
mnieber opened this issue Jun 3, 2020 · 7 comments
Closed

Support for jsconfig.json #556

mnieber opened this issue Jun 3, 2020 · 7 comments

Comments

@mnieber
Copy link

mnieber commented Jun 3, 2020

When I create an application with "create react app" then it places all source under the /src directory. In that case, imports do not have a "src/" prefix. However, importjs will produce import paths that do have the "src/" prefix. Is it possible to fix this? Or am I doing something wrong?

I thought that one option could be to take the value of compilerOptions.baseUrl in the jsconfig.json file into account when determining the import paths.

@coagmano
Copy link
Contributor

I've gotten around it by putting the package.json and node_modules in /src, which doesn't work for create react app...

Should be relatively easy to add to here:
https://github.com/Galooshi/import-js/blob/master/lib/findProjectRoot.js

If you wanted to start a proposal

@trotzig
Copy link
Collaborator

trotzig commented Jun 11, 2020

@mnieber Could you give a more specific example? What's the location of the file you're importing into and what's the location of the file/module you're importing? What did you expect import statements to look like vs what do they end up looking like?

Depending on the scenario here, you could look into using https://github.com/Galooshi/import-js#importstatementformatter.

@mnieber
Copy link
Author

mnieber commented Jun 12, 2020

I've found a solution based on an undocumented feature: the .importjs-root file. Just like package.json this file indicates the root directory for import paths found by importjs. By placing both .importjs-root and .importjs.js in the src directory, I managed to get import paths that do not start with the src/ prefix.
Maybe it's worth documenting .importjs-root?

@mnieber
Copy link
Author

mnieber commented Jun 14, 2020

Unfortunately, my solution (described above) is not adequate because now node_modules is no longer considered.

@JoaquimEsteves
Copy link

I know this is an old issue, but this is a feature I'd quite like to see.

A pet peeve of mine is that JavaScript projects always end up with hundreds of config files; and jsconfig has emerged as a bit of a standard.

I'd like to work on a PR, but I'm not experienced with the codebase, and would appreciate any hints as to where to look.

@mnieber
Copy link
Author

mnieber commented Oct 28, 2021

FYI, I'm now using the vandelay plugin in VSCode to generate the import statements. It doesn't have the "Fix all imports" feature that ImportJS has, which is very useful, but otherwise works really well.

@mikabytes
Copy link
Collaborator

I couldn't reproduce this issue. Perhaps it's been solved?

Here's what I did:

mkdir /tmp/test ; cd /tmp/test
npx create-react-app .
echo "export default 'hello'" > src/b.js
echo "console.log(b)" >> src/App.js
importjs fix src/App.js

ImportJS then went ahead and added this line:

import b from "./b.js";

Notice that it did not include the src in the path.

I'll go ahead and close this now as I can't reproduce the issue. However, if you feel I missed something then please go ahead and reopen it and describe how to reproduce step-by-step. Thanks!

This was referenced Feb 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants