How to set up Lerna monorepo with TypeScript

泪湿孤枕 提交于 2020-06-08 19:34:11

问题


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.


回答1:


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"
  }
}


来源:https://stackoverflow.com/questions/56515778/how-to-set-up-lerna-monorepo-with-typescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!