question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for import path rewriting

See original GitHub issue

I use paths in tsconfig.json and tsc doesn’t transform import paths automatically.

Basically, just need a way to intercept import statements (using the programmatic API) so I can change where they are pointing.

Alternatively, this package could respect paths in tsconfig.json and do the mapping for me, which could be opt-in if that’s a concern.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
timocovcommented, Nov 22, 2021

@igorpupkinable Sorry for late reply here.

Remove paths from packages/fe-components/tsconfig.json

The reason why this step makes the tool is working is that there are 2 “levels” of linking packages:

  • fs symlinks, which are generated by yarn monorepo I guess
  • TS paths, compiler-only “links”

It looks like they are applying in opposite order, i.e. paths are first, then fs symlinks. By removing paths you let compiler to use fs symlinks and that’s why it is working.

The original issue is the tool relies on fs path rather that import query (which obviously might be different across the project).

Relying on paths is kind of tricky, because if you’re using paths it doesn’t mean that this folder (or a file) should be treated as an “external”. You could add one of your folders to the paths just to decrease level of exiting while writing a path in import statement ('../../../../../../../file' vs '~root/file').

Moreover, I’m not sure, but most likely ts compiler resolves a module’s name internally before accessing FS api (you said that enabling disable-symlinks-following doesn’t work, but all this flag does is preventing accessing FS to resolve a module path). Probably it should be handled in resolveModuleNames to replace the file path with its request instead (but it might be tricky since it should be a path and ts could rely on it internally).

Let me know if you’ll have any thoughts on it.

0reactions
igorpupkinablecommented, Oct 21, 2021

Just faced this issue in my monorepo. Having "paths": { "@xxx/ui-core": ["../ui-core/src"], } entry in tsconfig.json treats @xxx/ui-core as internal package thereby inlining its types even if importedLibraries: ['@xxx/ui-core'] is set.

DTS is bundled properly if I remove "paths": { "@xxx/ui-core": ["../ui-core/src"], } from tsconfig.json. Unfortunately --disable-symlinks-following does not help with TS paths.

Does anyone have a solution for this issue?

Reproducible example: https://github.com/igorpupkinable/dts-bundle-generator-issues-50 Steps to reproduce:

  1. yarn install
  2. yarn check:types
  3. cd packages/ui-core
  4. yarn build:types
  5. cd …/fe-components
  6. yarn build:types <= Error: lib/index.d.ts(10,35): error TS2304: Cannot find name 'ThisShouldNotBeInlined'.
  7. Remove paths from packages/fe-components/tsconfig.json
  8. yarn build:types <= Success. ThisShouldNotBeInlined is imported as expected.

@timocov please let me know if I can provide more information or where to start investigation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

dropbox/ts-transform-import-path-rewrite - GitHub
This is a TypeScript AST Transformer that allows you to rewrite import path in output JS & d.ts files accordingly. The primary use...
Read more >
import-path-rewrite - npm search
Babel plugin to dynamically rewrite the path in require() and ES6 import ... Rewrite TypeScript import paths to ES Modules import paths.
Read more >
Rewriting import paths? - Google Groups
I'm currently in the process of importing some dependencies in to a local revision control server. That means I'll be needing to rewrite...
Read more >
How to automatically rewrite relative import paths to absolute?
To be clear, the imports work as it is. I just need to rewrite all imports in the existing codebase automatically. – Henning...
Read more >
"module": "node16" should support extension rewriting - Reddit
Well, the ESM specification requires imports to specify the full path, and it's my understanding Node will enforce this. So if TypeScript ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found