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.

Resolve path alias in declaration files (`.d.ts`)

See original GitHub issue

When applying a path alias to a typescript project, the path is resolved in the emitted javascript files, but not in the declaration files.

The TypeScript team have explicitly stated that path resolution is not something that will be included in the compiler (ever) and is the domain of utilities like rollup/webpack.

Versions

  • typescript: 3.7.4
  • rollup: 1.27.14
  • rollup-plugin-typescript2: 0.25.3

In the following project I have aliased ~/* to ./src/*. Allowing for imports using absolute paths from the base directory.

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

npm install && make
cat dist/a/index.d.ts | grep "~"

cat should print nothing, however it currently prints

import { B } from '~/b';

This illustrates that we are not applying path resolution to the declaration files.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:35
  • Comments:24 (3 by maintainers)

github_iconTop GitHub Comments

47reactions
alshdavidcommented, Feb 27, 2020

Yeah, I am achieving this using ttsc as well. I was unable to get the transformers working directly with rollup-plugin-typescript2 transformers options, so I opted to use ttsc

My rollup config looks like:

import typescript from 'rollup-plugin-typescript2'

export default {
  input: `src/index.ts`,
  preserveModules: true,
  output: {
    format: 'esm',
    dir: './dist'
  },
  external: [],
  watch: {
    include: 'src/**',
  },
  plugins: [
    typescript({ 
      typescript: require('ttypescript'),
      tsconfigDefaults: {
        compilerOptions: {
          plugins: [
            { "transform": "typescript-transform-paths" },
            { "transform": "typescript-transform-paths", "afterDeclarations": true }
          ]
        }
      }
    }),
  ],
}
8reactions
luanorlandicommented, Nov 11, 2020

Same issue with ttypescript and typescript-transofrm-paths, it didn’t work :disappointed. When running build:

[!] (plugin rpt2) TypeError: Cannot read property 'text' of undefined

But using @zerollup/ts-transform-paths and ttypescript it works 🎉

There is an awesome setup here: https://www.npmjs.com/package/@zerollup/ts-transform-paths#setup-for-rollup-plugin-typescript2

rollup.config.js

import ttypescript from 'ttypescript'
import tsPlugin from 'rollup-plugin-typescript2'
 
export default {
    input: 'src/lib.ts',
    output: [{ file : 'dist/lib.js', name : 'mylib', format : 'iife', sourcemap : true }],
    plugins: [
        tsPlugin({
            typescript: ttypescript
        })
    ]
}

tsconfig.json

{
    "compilerOptions": {
        "baseUrl": ".",
        "paths": {
            "my-lib/*": ["src/*"]
        },
        "plugins": [
            {
                "transform": "@zerollup/ts-transform-paths",
                "exclude": ["*"]
            }
        ]
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript declaration file created with alias instead of relative ...
The type declaration file contains alias instead of relative path. How to fix this problem? import { bar } from "./utils/bar. ts export...
Read more >
Documentation - Module Resolution - TypeScript
Module resolution is the process the compiler uses to figure out what an import refers to. Consider an import statement like import {...
Read more >
How to configure and resolve path alias with a Typescript Project
Path alias is a way to define an absolute path in your typescript project with a word, path or a character.
Read more >
Typescript – How to solve the problem with unresolved path ...
This error actually points to the transpiled('emitted') JavaScript .js file containing the line with the path alias. The actual cause of the ...
Read more >
Setting up Path Alias in TypeScript and tsc build without error
What it does is to look for your source file (.ts) and find the relative path to each module you set as path...
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