5

I have a core library with the following in package.json:

"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"es2015": "dist/es2015/index.js",
"types": "dist/es2015/index.d.ts",
"typings": "dist/es2015/index.d.ts",

The library builds TypeScript code into dist/ folder for distribution. The source code lies within src/.

I am using Lerna and monorepos, and I'm trying to get another package/module to load the TypeScript code as-is:

import { someTypeScriptStuff } from '@test/core'

However, this does not work. Both IntelliJ and TSLint complain about a missing module. If I change the value of the main field in package.json to src/index.ts, then it works.

I don't want to compile the TS code into dist all the time in development, because it's painful.

Obviously, I can't change the main field to src/index.ts either, because it's supposed to reference ordinary JavaScript that works as-is in node/browsers.

Is there a TypeScript specific field that I could use in package.json that both IntelliJ and TSLint could use instead? That would be ideal.

The only solution I can think of is to literally make the main field point to TS code and change my build process to mutate the contents of the packed NPM module by swapping the main field back to dist/cjs/index.js for distribution. I'd like to avoid that.

Kai Sellgren
  • 27,954
  • 10
  • 75
  • 87

1 Answers1

7

I resolved it with this in the root tsconfig.json:

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

And then I added this to every package's own tsconfig.json:

{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "rootDir": "src"
  }
}
Kai Sellgren
  • 27,954
  • 10
  • 75
  • 87