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

Offer a way to import files relative to src/ #581

Closed
gaelollivier opened this issue Sep 5, 2016 · 11 comments
Closed

Offer a way to import files relative to src/ #581

gaelollivier opened this issue Sep 5, 2016 · 11 comments

Comments

@gaelollivier
Copy link
Contributor

gaelollivier commented Sep 5, 2016

Not really sure about this, but I find it quite annoying to have imports that look like:

import mock from "../../../testUtils/myMock"

Relative paths are good for resources that will always live next to the calling file but when referring to shared helpers/modules I find them quite counter-productive.

It would be possible to configure webpack so we could import files from src/ just like import 'testUtils/myMock or maybe src/testUtils/myMock by adding src to the resolve root.

Would it make sense ? Maybe relative paths are not that bad and I'm missing something ?

@gaearon
Copy link
Contributor

gaearon commented Sep 5, 2016

We already allow this if you explicitly set NODE_PATH environment variable. So, NODE_PATH=./src npm test should work.

We won't diverge from Node resolution mechanism by default because there are very confusing corner cases. Like having a directory with the same name as a dependency. People have been burned hard over this in the past.

You have two other options too. You can publish these helpers as a (private) module. Or you can create src/setupTests.js to perform initialization (if you only need to set up some globals). It will be picked up by default, as documented in the usage guide.

I hope this helps!

@gaearon gaearon closed this as completed Sep 5, 2016
@gaelollivier
Copy link
Contributor Author

Think it's a bit heavy to publish a module but I get the problem with conflicting names. It wouldn't be an issue if we would only catch names that start with src/ but I agree that diverging from default Node resolution mechanism doesn't feel right.

No clear solution for this then :(

@gaearon
Copy link
Contributor

gaearon commented Sep 5, 2016

Is setting NODE_PATH or setting up a global in setupTests.js not working for you?

@gaelollivier
Copy link
Contributor Author

It's working for tests but not for re-usable modules/helpers.

The directory structure of a project can become quite deep, let's say a file ./routes/posts/create/FormContainer.js uses ./entities/posts.js, I would prefer write import { ... } from "src/entities/posts" rather than ../../../entities/posts. Not just because it breaks if I move FormContainer.js but also because it's more readable and easier to retrieve imo.

@gaearon
Copy link
Contributor

gaearon commented Sep 5, 2016

I don't understand. Setting NODE_PATH will give you precisely that. Have you tried it?

@gaearon
Copy link
Contributor

gaearon commented Sep 5, 2016

  1. Put NODE_PATH=./src before every script command in package.json
  2. Now you can write from 'entities/posts' assuming entities exists inside src.

@gaelollivier
Copy link
Contributor Author

Indeed, that works, thanks!

I just didn't like adding NODE_PATH=./src before every command but it's a tradeoff ¯\_(ツ)_/¯

@gaearon
Copy link
Contributor

gaearon commented Sep 5, 2016

Yeah. If you have suggestions on how to make this better while preserving the opt-in nature, please file an issue with your proposal. Maybe we could have a top-level directory called packages whose subfolders are implicitly resolved.

@Sodj
Copy link

Sodj commented Mar 8, 2018

@gaearon on windows NODE_PATH.src/ before script doesn't work
any other solutions?

@bikeshedder
Copy link

I quite like the idea how Vue solved it in vue-cli: They configured webpack to replace @/ by the source directory using resolve.alias. I quite like the idea and typing import "@/base/components/FooBar" doesn't look too bad. @ on its own should never be a valid scope and should be pretty future proof.

How about adopting that idea to create-react-app as well?

@codialsoftware
Copy link

using .env file also does the trick and it's even better - no need to modify package.json

@lock lock bot locked and limited conversation to collaborators Jan 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants