module-alias icon indicating copy to clipboard operation
module-alias copied to clipboard

[Feature request] Add support for extra extension and also patch path.resolve

Open wintercounter opened this issue 6 years ago • 4 comments

These are actually 2 separate features.

  • I want to be able to resolve ts files using require.resolve.
  • I want to be able to resolve my aliases with path.resolve.

wintercounter avatar Aug 22 '19 11:08 wintercounter

Hey there !

Just to get a bit more context:

  • What's your use-case for aliasing Typescript files ? Can you use Typescript's path aliasing instead ? See: https://medium.com/@caludio/how-to-use-module-path-aliases-in-visual-studio-typescript-and-javascript-e7851df8eeaa
  • What's the use-case for path.resolve ? I'm thinking this is a bit too far removed from module-alias, which is about modules, not naked files.

Kehrlann avatar Aug 22 '19 12:08 Kehrlann

  • For the first case when I'm doing require.resolve('@/aliasedpath/moduleDirWithIndexTsFile'), it won't get resolved. I could use https://nodejs.org/api/modules.html#modules_require_extensions but it's deprecated so I thought it would be nice to have such support here instead so I can make sure it won't conflict with any other lib touching the module system.
  • The second case is while require.resolve('alias/file.js') resolves correctly, path.resolve('alias/file.js') is not. For resolving modules it's fine to use require.resolve but for anything else (eg. directories) I should use path.resolve. It would be also a solution for me if a function would be exported by the package to resolve paths, eg: import { resolvePath } from 'module-alias'

wintercounter avatar Aug 22 '19 14:08 wintercounter

module-alias works with compiled JS folder rather than TS sources, so it could resolve only .js from dist (or whatever is destination) folder. If you really need to resolve the .ts source path, you should create a separate alias that would point to src folder rather than dist. So even after it's compiled to JS it would still resolve you .ts file

ilearnio avatar Sep 25 '19 09:09 ilearnio

I'm using TS in runtime with babel-register so I don't have dist folder, only src.

wintercounter avatar Oct 03 '19 05:10 wintercounter