如何使用TypeScript设置Lerna monorepo [英] How to set up Lerna monorepo with TypeScript

查看:910
本文介绍了如何使用TypeScript设置Lerna monorepo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在package.json中有一个core库,其中包含以下内容:

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

该库将TypeScript代码构建到dist/文件夹中以进行分发.源代码位于src/中.

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

我正在使用Lerna和monorepos,并且试图获取另一个程序包/模块来按原样加载TypeScript代码:

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'

但是,这不起作用. IntelliJ和TSLint都抱怨缺少模块.如果我将package.json中的main字段的值更改为src/index.ts,则它可以工作.

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.

我不想在开发过程中一直将TS代码编译为dist,因为这很痛苦.

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

很明显,我也不能将main字段更改为src/index.ts,因为它应该引用在node/浏览器中按原样工作的普通JavaScript.

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.

package.json中是否可以使用IntelliJ和TSLint都可以使用的TypeScript专用字段? 那将是理想的.

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.

我唯一能想到的解决方案是将main字段指向TS代码,并通过将main字段换回dist/cjs/index.js来更改构建过程以更改打包的NPM模块的内容.分配.我想避免这种情况.

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.

推荐答案

我在根目录tsconfig.json中对此进行了解析:

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

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

然后将其添加到每个程序包自己的tsconfig.json中:

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

{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "rootDir": "src"
  }
}

这篇关于如何使用TypeScript设置Lerna monorepo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆