3

Problem

[It only infects typescript library creators]

if the code base contains absolute paths (by configuring tsconfig.json and webpack), then the typescript compiler will generate all the d.ts files with the same absolute paths that are useless because my lib consumers can't do a thing with them.


Workarounds

Every library that I saw are doing one of the following:

  1. creating their own single-file.d.ts file manually
  2. using relative path in their code base so the output d.ts files also contain relative paths.

Clearly, both alternatives suck.


Other solution that I thought about

  1. creating a single d.ts file (there will be no imports).

I couldn't find a working library for that.

  1. not sure if it's possible: before running ts-loader, we need to run a magic babel-plugin to transform every absolute path to a relative by looking in the webpack-resolve-module section in webpack.config file.

My current status

I created a library that generates broken d.ts files with absolute paths:

https://github.com/stavalfi/lerna-yarn-workspaces-example/tree/master/packages/x-core

index.ts:

import { z, x } from 'shalom'     // problem
export default function awesomeFn(y: number): x {
  return z(1)
}

export { z, x } from './shalom' . // not a problem

generated index.d.ts:

import { x } from './shalom';     /// GOOD - becuase I used relative
export default function awesomeFn(y: number): x;
export { z, x } from 'shalom';    /// BAD - because I used absolute
//# sourceMappingURL=index.d.ts.map .   

Question

Is there any sort of solution/workaround that is not stop using an absolute path or creating my own single.d.ts file manually?

Stav Alfi
  • 13,139
  • 23
  • 99
  • 171

1 Answers1

3

If any typescript library creator still has this problem, I created a Webpack loader for babel-plugin-module-resolver, which converts absolute paths to relative paths: https://github.com/stavalfi/babel-plugin-module-resolver-loader

It also solved the problem of *.d.ts files with absolute paths by converting them to relative paths.


You will probably want to subscribe to/read this thread for additional workarounds: https://github.com/Microsoft/TypeScript/issues/15479

Stav Alfi
  • 13,139
  • 23
  • 99
  • 171