Galooshi / import-js

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

Support for relative paths #105

Closed lencioni closed 8 years ago

lencioni commented 8 years ago

As seen in this code snippet, some projects prefer relative paths for non-node_modules modules:

import cheerio from 'cheerio';
import Sinon from 'sinon';
import ReactWrapper from './ReactWrapper';
import ShallowWrapper from './ShallowWrapper';
import { onPrototype } from './Utils';
import { renderToStaticMarkup } from './react-compat';

It would be nice if ImportJS was able to support this.

trotzig commented 8 years ago

This is something I've been thinking about too. I think it should be fairly simple to implement. We just have to take the path to the current file into account when resolving the import. One thing that worries me though is how do we know when it's okay to do a relative import? Is lookup_path != 'node_modules' okay?

lencioni commented 8 years ago

Yep, I think that's all you need to do.

trotzig commented 8 years ago

How would you feel about changing the lookup_paths to allow something like this:

"lookup_paths": [
  "foo",
  { "path": "bar", "use_relative_paths": true },
  "baz"
]

I'm a little scared that it will end up creating imports that look something like this: import Foo from '../../../../bar/baz/Foo'. Or perhaps this sort of thing is mostly suited for smaller projects, in which case we could just add a global use_relative_paths configuration. node_modules isn't meant to be part of lookup_paths anyway.

lencioni commented 8 years ago

How would you feel about changing the lookup_paths to allow something like this

I feel like people are going to probably want this on a per-project level with the only exception being node_modules (which is already treated specially in import-js). I think the option should likely be at the top level.

We may eventually want to add options per lookup path, but I don't think we should until we actually have a real need for it.

I'm a little scared that it will end up creating imports that look something like this: import Foo from '../../../../bar/baz/Foo'

Yeah, it might create imports looking like that, depending on how your project is structured. But, I think it should be up to the consumer to decide if that is acceptable or not.

trotzig commented 8 years ago

We may eventually want to add options per lookup path, but I don't think we should until we actually have a real need for it.

Agreed

Yeah, it might create imports looking like that, depending on how your project is structured. But, I think it should be up to the consumer to decide if that is acceptable or not.

Yep

trotzig commented 8 years ago

Fixed by fd81fffe06d3e39599347b7fe11004daa78c040f