【问题标题】:Typescript project setup with shared package带有共享包的打字稿项目设置
【发布时间】:2020-01-16 03:43:24
【问题描述】:

在开始之前我有几个前提:

  1. 我想要一个共享包,它将包含在其他三个包中,
  2. 打字稿,
  3. 只有一个节点模块,
  4. 多平台使用(Windows / Linux),
  5. 使用共享包中的依赖项,
  6. (实时重新加载会很好),
  7. 不要失去以后发布共享包的能力(其他三个包是网络服务器,但现在正在大力开发中),
  8. 通用脚本build:webbuild:apibuild:admin来自全局包。

我梦想的文件夹结构是:

/
/node_modules/***

/packages/shared/package.json // Shared dependencies like typeorm, lodash etc..
/packages/shared/src/index.ts
/packages/shared/src/models/User.ts

/packages/web/package.json
/packages/web/tsconfig.json
/packages/web/src/index.ts // first express server (using shared code)

/packages/api/package.json
/packages/api/tsconfig.json
/packages/api/src/index.ts
...

/package.json
/tsconfig.common.json

互联网上有很多解决方案 - Lerna、typescript 项目参考、npm / yarn 链接……但我找不到合适的解决方案……

例如,这个https://github.com/Quramy/lerna-yarn-workspaces-example 很酷,但我无法设置运行这样的东西:

// package.json

{
  "scripts": {
     "debug:web": "concurrently \"(cd packages/shared && tsc -w)\" \"(cd packages/web && tsc -w)\" \"(cd packages/web && nodemon --inspect dist/index.js --watch .. /)\""

...

我想在 ../ (包文件夹)中发生更改后不断构建共享包(生成 dist/index.js 和 .ts 文件)并在 nodemon 中重新启动服务器。

几个小时后,我放弃了重新加载代码的机会,并通过我能找到的“最简单的方式”尝试 - shared 中没有包,但直接通过以下方式包含代码:

// shared/index.ts
export const createConnection = () => ....

// web/index.ts
import { createConnection } from 'shared'

它看起来也不起作用:

// tsconfig.json

{
  "extends": "../../tsconfig.settings.json",
  "compilerOptions": {
    "rootDir": "../",
    "outDir": "dist",
    "baseUrl": "./",
    "paths": {
      "shared":  [ "../shared/src/" ]
    }
  }
}

现在的问题是:

File /shared/src/index.ts is not listed within the file list of the project in tsconfig.json. Projects must list all files or use an 'include' pattern.

下一步是(显然):

{
  "extends": "../../tsconfig.settings.json",
  "include": [
    "./src/**/*.ts",
    "../shared/src/**/*.ts"
  ],
  "compilerOptions": {
    "rootDir": "../",
    "outDir": "dist",
    "baseUrl": "./",
    "paths": {
      "shared":  [ "../shared/src/" ]
    }
  }
}

这行得通!没有错误,但“dist”中也没有文件...... LOL

我很确定,有一些不错的解决方案、示例或者它也不起作用,但我无法找到或构建它。

【问题讨论】:

    标签: node.js typescript npm lerna


    【解决方案1】:

    如果我错了,请纠正我,但您需要一个示例,其中项目被分离为单一存储库中的包,用 Typescript 编写,并且一些包相互依赖。 如果是这样,我创建了这样的项目。我使用了 Typescript 的一项新功能:项目引用。您可以在配置文件中指定其他 tsconfig,并且可以为它们定义依赖项。例如:包 B 依赖包 A。Typescript 将在开始编译 A 之前编译 B 并使用其声明。

    这是 tsconfig 的一个小例子

    
    {
      "extends": "../../tsconfig.json",
      "compilerOptions": {
        "rootDir": "src",
        "outDir": "lib"
      },
      "include": ["src"],
      "exclude": ["lib"],
      "references": [{ "path": "../util" }, { "path": "../crud-request" }]
    }
    

    您可以在这里获取更多信息:https://www.typescriptlang.org/docs/handbook/projectreferences.html

    我以这个项目为例来构建我自己的,也许它对你也有用: https://github.com/nestjsx/crud

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-03
      • 1970-01-01
      • 2017-08-22
      • 2020-10-27
      • 1970-01-01
      • 1970-01-01
      • 2017-03-06
      • 2013-12-24
      相关资源
      最近更新 更多