AsaAyers / js-hyperclick

A hyperclick provider that lets you jump to where variables are defined.
MIT License
127 stars 42 forks source link

Make this work with meteor? #46

Closed thebarty closed 7 years ago

thebarty commented 7 years ago

Hi guys,

I really love the idea of this package, but can not make it work with meteor. :-(

My meteor-directory-structure looks like:

./  // root
/src/* // sources
/src/imports/modules/*  // modules

So an import might look like

import { Permissions } from '/imports/modules/permissions/lib/permissions.js'

When I know click on Permissions in the code, I get an "module /imports/modules/permissions/lib/permissions.js not found"-error.

Any ideas how to solve this? It would be such a timesaver to get this to work!!

AsaAyers commented 7 years ago

This project is designed to be node-compatible. This works well because other projects like Webpack also seem to try to remain node-compatible, so everything just works.

The leading slash in /importsindicates you want to import from the root of your drive. If you're on windows this is something like c:\imports, under other OSes it's literally /imports. This is not something I'm going to make a special case for. That's the behavior you get with every tool I've used.

There are two options here:

Relative Imports

The most compatible solution is for you to simply use relative paths on your imports:

import { Permissions } from './imports/modules/permissions/lib/permissions.js'

If you just use relative paths, then your code is compatible with every tool I have ever found that scans imports without any configuration. You get webpack, js-hyperclick, eslint all for free. Try autocomplete-paths, It'll work with relative paths, but it doesn't look like it'll work with your absolute paths you have now or the moduleRoots in the next option.

Drop the leading slash

If you drop the leading slash and have imports/modules/... you can configure moduleRoots in your package.json:

"moduleRoots": [
    "src"
  ],

Any time you import a file that isn't relative and js-hyperclick can't find it, js-hyperclick will do one last round searching your moduleRoots.

rohithb commented 7 years ago

@thebarty if you really want hyperclick to work with absolute path (relative to project root) add the following lines into suggestions.js in resolveModule function. (here: https://github.com/AsaAyers/js-hyperclick/blob/master/lib/suggestions.js#L73)

   if(module[0] === '/'){
        let atomPath = atom.project.relativizePath(textEditor.getPath());
        basedir = atomPath[0]; 
        module = '.' + module;
    } 

Also don't forget to change const basedir to let basedir (https://github.com/AsaAyers/js-hyperclick/blob/master/lib/suggestions.js#L72).

I just made this change and works fine with my meteor imports.

NB: If you make this change hyperclick will no longer follows actual absolute paths (ie., system root relative paths).

AsaAyers commented 7 years ago

I suggest forking the project. If you make that change, you're going to have to remember to come back and make it every time a new version is published.

AsaAyers commented 5 years ago

I just published v1.15 with a new feature I'm calling "Custom Resolvers". It will allow you to add a JS file to your project that can implement your aliases. PR and documentation

See these for your use case

https://github.com/AsaAyers/js-hyperclick/blob/6240dc8dd738371d380a1c875f543887ae5eb65d/spec/fixtures/moduleRoots.js#L8-L9 https://github.com/AsaAyers/js-hyperclick/blob/6240dc8dd738371d380a1c875f543887ae5eb65d/custom-resolver.js#L28-L32