Expanding paths defined in tsconfig in to compiled javascript

Viewed 119

I am writing a Node library in typescript and have enabled paths in my tsconfig.json as shwon below.

When I compile my code using tsc (using npm run tscompile), the typescript compiles, and I see the corresponding JS in my ./dist/ folder. But the javascript still has the paths in it, e.g. const logger_1 = __importDefault(require("@logging/logger"));. If I try to run the javascript, it fails since it doesn't understand the paths.

What / where am I missing to expand the paths in the javscript?

For my Jest tests, I am using a module name mapper to expand the paths for the tests, and that works, but what about my mainline compiled code?

tsconfig.json:

{
  "compileOnSave": true,
    "compilerOptions": {
      "module": "commonjs",
      "target": "ES2020",
      "declaration": true,
      "outDir": "./dist",
      "esModuleInterop": true,
      "rootDir": ".",
      "baseUrl": ".",
      "sourceMap": true,
      "paths": {
        "@logging/*": ["src/logging/*"],
        "@validation/*": ["src/validation/*"],
        "@util/*": ["src/util/*"],
        "@test-helpers/*": ["test/helpers/*"]
     }
    },
    "include": [
      "src/**/*", "test/**/*"
    ]
  }

package.json:

  "scripts": {
    "test": "jest --coverage",
    "lint": "eslint ./src/",
    "lint-tests": "eslint ./test/",
    "lint-all": "eslint ./src/ && eslint ./test/",
    "tscompile": "tsc",
    "lic-report-prod": "license-checker --production --json --out ./report/license/license-prod.json"
  },

`jest.config.ts:

const tsconfig = require("./tsconfig.json");
// use tsconfig-paths-jest tool to automatically map modules based on tsconfig.json paths config
const moduleNameMapper = require("tsconfig-paths-jest")(tsconfig);

module.exports = {
  transform: {
    "^.+\\.ts?$": "ts-jest",
  },
  testEnvironment: "node",
  testRegex: ".*.test.ts$",
  moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
  moduleNameMapper,
};

0 Answers
Related