Skip to content
This repository has been archived by the owner on Apr 21, 2022. It is now read-only.

Transform tsConfig.compilerOptions.paths when building TypeScript #5

Open
stackdumper opened this issue Feb 8, 2019 · 6 comments
Open

Comments

@stackdumper
Copy link

stackdumper commented Feb 8, 2019

Hello. Thank you for your project 👍


TypeScript module resolution shortcuts are not transformed into a plain paths.

tsconfig.json

{
  "compilerOptions": {
    "target": "es2015",
    "types": ["node"],
    "lib": ["esnext"],

    "module": "commonjs",
    "moduleResolution": "node",
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,

    "declaration": true,
    "declarationMap": true,

    "baseUrl": ".",
    "paths": {
      "@/*": ["./src/*"],
      "@test/*": ["./test/*"]
    }
  }
}

source

// src/index.ts
import { createServices } from '@/services'

output

// pkg/dist-src/index.js
import { createServices } from '@/services';

expected output

// pkg/dist-src/index.js
import { createServices } from './services';

possible-solution

tsconfig-paths

@FredKSchott
Copy link
Owner

That's a really interesting feature! I haven't used that personally before, is it supported by TypeScript by default, or is this a feature that only works when tsconfig-paths is loaded? If there are any official TypeScript docs you can send along, that would be great.

Also, do you have an example repo that you can share so that I can get a better feel for it?

@stackdumper
Copy link
Author

This is typescript feature, the caveat is that tsc does not compile aliases to real paths during the build.

Resources

Here's the doc
https://www.typescriptlang.org/docs/handbook/module-resolution.html

Here's the simpler explanation
https://stackoverflow.com/questions/43281741/how-to-use-paths-in-tsconfig-json

Here's the corresponding TypeScript issues
microsoft/TypeScript#16640
microsoft/TypeScript#10866

Existing solutions in other tools

Solution?

tsconfig-paths is only able to alias paths in runtime (node --register tsconfig-paths/register index.js). This looks weird and might cause unintended side effects.

Ideally, paths should be transpiled during the build. However, I was not able to find existing solution that would do this. Looks like this is archivable only by using module builder like Webpack or Rollup.

@stackdumper
Copy link
Author

And here's the reproduction
https://github.com/stackdumper/pika-pack-tsconfig-paths-repro

@stackdumper stackdumper changed the title @pika/plugin-ts-standard-pkg ignores tsconfig paths Transform tsConfig.compilerOptions.paths when building TypeScript Feb 9, 2019
@thesems
Copy link

thesems commented May 13, 2019

I run into the same issue! Basically, it makes it impossible to use it in a project that gets included somewhere.

Would be cool if we got this working.

@kcarra
Copy link

kcarra commented Aug 3, 2019

To get around this issue I'm just running ts-node -r tsconfig-paths/register server.js instead of node server.js and it works as expected with the paths not being mapped. This requires ts-node to be installed globally for this to work.

@TechnicalRhino
Copy link

Just for help https://dev.to/larswaechter/path-aliases-with-typescript-in-nodejs-4353

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants