#Rewriting import path alias on compilation?

4 messages · Page 1 of 1 (latest)

sharp sluice
#

I have these paths in my tsconfig.json:

{
  "compilerOptions": {
    "lib": [
      "es2020"
    ],
    "module": "NodeNext",
    "experimentalDecorators": true,
    "moduleResolution": "NodeNext",
    "sourceMap": true,
    "target": "es2020",
    "noImplicitAny": false,
    "paths": {
      "@/*": ["./src/*"],
      "src/*": ["./src/*"],
    }
  }
}

The issue is when I run tsc the import paths aren't rewritten, so I end up with this in the generated javascript file:

import ResourceType from '@/enums/ResourceType.js';

How should I configure tsconfig or the 'compilation' process to do path substitution? This is for a server side nodejs app.

lilac quiver
#

@sharp sluice TS doesn't do this.

#

!*:paths-are-not-magic

tight daggerBOT
#
tjjfvi#0
`!t6:paths-are-not-magic`:

The paths and baseUrl compiler options don't cause any remapping of imports paths, they only inform TS of existing mappings, which you'll have to setup with some other tool.

baseUrl is a pretty well-supported option (e.g. using the NODE_PATH environment variable with node or resolve.modules with webpack).
paths can be trickier to setup, (especially with node see this for node), and you may find it to not be worth the effort.