Skip to content
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

Compiler option to rewrite import file paths #35351

Closed
alshdavid opened this issue Nov 26, 2019 · 5 comments
Closed

Compiler option to rewrite import file paths #35351

alshdavid opened this issue Nov 26, 2019 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@alshdavid
Copy link

alshdavid commented Nov 26, 2019

Problem

When using paths to apply an alias to an import, the files do not represent the expected target path.

Not rewriting the paths is the correct and expected behaviour from the TypeScript compiler and further rewriting belongs to extra tooling layers, like Webpack.

Webpack can be used to rewrite the paths in the emitted javascript bundles, however this rewrite is not represented in declaration files or maps so its behaviour is incomplete. This means that modules which are expected to have consumable declarations are unable to rely on the generated files.

Proposal

It would be awesome if TypeScript included the ability to alias + rewrite imports which worked on both emitted .js, .d.ts files and source maps.

Perhaps the inclusion of a new compiler option called something similar to rewriteImports is a solution.

Below I am setting ~ as a path which represents an absolute import path from the root of the source directory.

{
  "compilerOptions": {
    "baseUrl": "./",
    "rewriteImports": {
      "~": "."
    }
  }
}

Example

Given the following folder structure:

/a
  index.ts
/b
  index.ts

Imagine that a/index.ts looks like:

import { foo } from '~/b'
console.log(foo)
export type Foo = typeof foo

The output javascript and declaration files would look like:

// a/index.js
import { foo } from '../b'
console.log(foo)
// a/index.d.ts
import { foo } from '../b'
export type Foo = typeof foo
@denis-sokolov
Copy link

Duplicate of #26722.

(Other mentions: #10866, #16640, #18951, #19453)

@alshdavid
Copy link
Author

alshdavid commented Nov 26, 2019

I have been experimenting with webpack and rollup with no success.

I have a minimal example repo, which features a branch where I attempt to solve the problem with webpack:

https://github.com/alshdavid-sandbox/typescript-library

@fatcerberus
Copy link

Not likely to happen:
#31643 (comment)

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Dec 3, 2019
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@tobico
Copy link

tobico commented May 7, 2024

I ran into this issue today while developing a component library using TypeScript and had good results using the package tsc-alias to fix my imports after compilation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

6 participants