Skip to content

Suggestion: Transpile path mappings based module resolution imports to relative paths #6710

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

Closed
dsherret opened this issue Jan 29, 2016 · 4 comments
Labels
Needs More Info The issue still hasn't been fully clarified

Comments

@dsherret
Copy link
Contributor

Taking example 2 from the original issue (#5039):

projectRoot
├── folder1
│   ├── file1.ts
│   └── file2.ts
├── generated
│   ├── folder1
│   └── folder2
│       └── file3.ts (Contains: import 'folder1/file2';)
└── tsconfig.json (with "baseUrl": ".")

When doing...

import 'folder1/file2';

...in file3.ts it current compiles to the same thing as above. Would it be possible if we could add a compiler option or some way that would convert this to the relative path? So it would transpile it to:

import './../../folder1/file2';

That way the paths don't have to be managed in two places. I couldn't find this discussed anywhere and didn't want to clutter the original issue so I'm opening this here.

@mhegazy
Copy link
Contributor

mhegazy commented Jan 29, 2016

Can you elaborate on the use case here.

@mhegazy mhegazy added the Needs More Info The issue still hasn't been fully clarified label Jan 29, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Feb 20, 2016

please reopen if you have more information.

@mhegazy mhegazy closed this as completed Feb 20, 2016
@LaurensRietveld
Copy link

LaurensRietveld commented Jul 14, 2016

@mhegazy : to avoid opening another issue with the same topic, I'd like to pitch in.
As far as I see, what @dsherret means is that the import * as something from 'folder1/file2' is transpiled to something like const something = require('folder1/file2') (assuming commonjs).
The transpiled JS won't work without some hacking around, i.e. duplicating the same functionality that typescript already has, but now for rewriting these javascript require statements.

I assumed that this conversion to relative paths was part of these new ts 2.0 features, but just like @dsherret I fail to find relevant documentation for this scenario

ps. Feel free to correct me if I reinterpret you incorrectly @dsherret ;)
pps. How I solved this (hacky): set the env variable NODE_PATH to the js directory (e.g. ./build/src), and set the base url to e.g. ./src. That way both TS and JS are looking in the correct location. This won't work when using the other new TS features such as paths though.

@dsherret
Copy link
Contributor Author

Yes, basically I think it would be extremely nice if the emitter handled emitting the module specifier to a relative path if the module specifier is resolved using baseUrl, path mappings, or rootDirs.

Right now it emits import 'folder1/file2'; to have the same module specifier—'folder1/file2'. That means I have to configure the module loader to know the configuration in tsconfig.json... in this case "baseUrl": ".".

So current problems:

  1. I have to configure "baseUrl": "." in TypeScript AND in my module loader configuration.
  2. I have to hope that whatever module loader I'm using has support for the feature I use in tsconfig.json (baseUrl, path mappings, rootDirs).

It would be nice if the emitter would handle all this. That would mean that I would only need to keep this configuration in tsconfig.json.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Needs More Info The issue still hasn't been fully clarified
Projects
None yet
Development

No branches or pull requests

3 participants