构建 TypeScript monorepo 项目的问题
Issues building a TypeScript monorepo project
我有一个相当简单的单声道存储库。它在 GitLab 上可用
here。这使用纱线工作区、TypeScript、Jest,ts-jest
和 ESLint eslint-plugin-import
.
我正在尝试使用 TypeScript 正确构建项目包。以前我只是
在同一目录中将 TypeScript 文件与其 JavaScript 代码一起发布。
我构建项目的尝试在 GitLab 合并请求中可用
here
现在存储库遵循以下布局:
|- example/
| |- index.ts
| |- package.json
| `- tsconfig.json
|- packages/
| |- koas-core/
| | |- src/
| | | `- index.ts
| | |- package.json
| | `- tsconfig.json
| |- koas-status-code/
| | |- src/
| | | `- index.ts
| | |- package.json
| | `- tsconfig.json
| `- more similar workspaces…
|- package.json
|- tsconfig.json
`- more configuration files…
一些包相互依赖。我已经设法获得 Jest 测试和 eslint-plugin-import
使用此设置,但我在构建项目时遇到问题。
根 tsconfig.json
看起来像这样:
{
"compilerOptions": {
"baseUrl": ".",
"composite": true,
"declaration": true,
"module": "commonjs",
"noEmit": true,
"resolveJsonModule": true,
"target": "esnext",
"paths": {
"koas-*": ["packages/koas-*/src"]
}
},
"exclude": ["**/*.test.ts"]
}
工作区 tsconfig.json
个文件如下所示:
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "lib"
}
}
每个工作区都有一个在 package.json
中定义的 prepack
脚本,如下所示:
{
"scripts": {
"prepack": "tsc --noEmit false"
}
}
main
字段指的是lib
。
如果我 运行 yarn workspace koas-status-code prepack
,我得到以下错误:
$ tsc --noEmit false
error TS6059: File 'koas/packages/koas-core/src/SchemaValidationError.ts' is not under 'rootDir' 'koas/packages/koas-status-code'. 'rootDir' is expected to contain all source files.
error TS6059: File 'koas/packages/koas-core/src/SchemaValidationError.ts' is not under 'rootDir' 'koas/packages/koas-status-code/src'. 'rootDir' is expected to contain all source files.
error TS6059: File 'koas/packages/koas-core/src/createDefaultValidator.ts' is not under 'rootDir' 'koas/packages/koas-status-code'. 'rootDir' is expected to contain all source files.
error TS6059: File 'koas/packages/koas-core/src/createDefaultValidator.ts' is not under 'rootDir' 'koas/packages/koas-status-code/src'. 'rootDir' is expected to contain all source files.
error TS6059: File 'koas/packages/koas-core/src/createMatcher.ts' is not under 'rootDir' 'koas/packages/koas-status-code'. 'rootDir' is expected to contain all source files.
error TS6059: File 'koas/packages/koas-core/src/createMatcher.ts' is not under 'rootDir' 'koas/packages/koas-status-code/src'. 'rootDir' is expected to contain all source files.
error TS6059: File 'koas/packages/koas-core/src/index.ts' is not under 'rootDir' 'koas/packages/koas-status-code'. 'rootDir' is expected to contain all source files.
error TS6059: File 'koas/packages/koas-core/src/index.ts' is not under 'rootDir' 'koas/packages/koas-status-code/src'. 'rootDir' is expected to contain all source files.
error TS6307: File 'koas/packages/koas-core/src/SchemaValidationError.ts' is not listed within the file list of project 'koas/packages/koas-status-code/tsconfig.json'. Projects must list all files or use an 'include' pattern.
error TS6307: File 'koas/packages/koas-core/src/createDefaultValidator.ts' is not listed within the file list of project 'koas/packages/koas-status-code/tsconfig.json'. Projects must list all files or use an 'include' pattern.
error TS6307: File 'koas/packages/koas-core/src/createMatcher.ts' is not listed within the file list of project 'koas/packages/koas-status-code/tsconfig.json'. Projects must list all files or use an 'include' pattern.
error TS6307: File 'koas/packages/koas-core/src/index.ts' is not listed within the file list of project 'koas/packages/koas-status-code/tsconfig.json'. Projects must list all files or use an 'include' pattern.
Found 12 errors.
我也试过 tsconfig.json
koas-status-code
:
{
"extends": "../../tsconfig.json",
"include": ["src"],
"references": [{ "path": "../koas-core" }],
"compilerOptions": {
"outDir": "lib"
}
}
这构建了工作区,但仍然出现以下错误:
$ tsc --noEmit false
src/index.ts:1:23 - error TS6305: Output file '/home/remco/Projects/koas/packages/koas-core/lib/src/index.d.ts' has not been built from source file '/home/remco/Projects/koas/packages/koas-core/src/index.ts'.
1 import * as Koas from 'koas-core';
~~~~~~~~~~~
Found 1 error.
我该如何解决这个问题?
我根据 https://github.com/NiGhTTraX/lerna-ts.
找到了这个问题的解决方案
项目现在在项目根目录中包含一个名为 tsconfig.build.ts
的文件。这个文件基本上只是指定编译器选项,测试和构建文件应该从构建过程中排除。
{
"compilerOptions": {
"declaration": true,
"module": "commonjs",
"resolveJsonModule": true,
"target": "esnext"
},
"exclude": ["**/*.test.ts", "**/lib"]
}
根目录中的另一个文件是 tsconfig.json
。该文件用于构建过程之外的类型检查,例如通过 Jest 和 VSCode。该文件扩展了构建配置。它包括通过覆盖扩展 exclude
配置的测试。它还包含 noEmit: true
,因为在开发过程中使用 TypeScript 的进程不应该发出任何东西。它还包含在运行时解析 TypeScript 源文件所需的 baseUrl
和 paths
编译器选项。这是因为包的 package.json
文件中的 main
字段引用了包含输出文件的 lib
。
{
"extends": "./tsconfig.build.json",
"compilerOptions": {
"baseUrl": ".",
"noEmit": true,
"paths": {
"koas-*": ["packages/koas-*/src"]
}
},
"exclude": ["**/lib"]
}
每个工作区都包含一个 tsconfig.build.json
文件。这是必需的,因为它要求 src
和 outDir
选项需要相对于 tsconfig 文件。那是用来构建项目的。重要的是此文件 而不是 命名为 tsconfig.json
.
{
"extends": "../../tsconfig.build.json",
"include": ["src"],
"compilerOptions": {
"outDir": "lib"
}
}
每个工作区还包含一个 tsconfig.json
。这可用于覆盖用于类型检查的编译器选项,例如,如果一个存储库将使用 dom
类型。这可以省略。
{
"extends": "../../tsconfig.json"
}
每个工作区在 package.json
中都有以下 scripts
部分。这会导致在构建包时编译 TypeScript。
// …
"scripts": {
"prepack": "tsc --project tsconfig.build.json"
}
有两个 CI 作业用于类型检查。一个运行 tsc
以确保类型检查在开发中使用这些类型的工具中仍然有效,另一个确保构建包不会中断。
tsc:
script:
- yarn --frozen-lockfile
- yarn workspaces run tsc
pack:
script:
- yarn --frozen-lockfile
- yarn workspaces run pack
- find packages -name '*.tgz' -exec mv {} ./ +
artifacts:
name: packages
paths:
- '*.tgz'
我有一个相当简单的单声道存储库。它在 GitLab 上可用
here。这使用纱线工作区、TypeScript、Jest,ts-jest
和 ESLint eslint-plugin-import
.
我正在尝试使用 TypeScript 正确构建项目包。以前我只是 在同一目录中将 TypeScript 文件与其 JavaScript 代码一起发布。
我构建项目的尝试在 GitLab 合并请求中可用 here
现在存储库遵循以下布局:
|- example/
| |- index.ts
| |- package.json
| `- tsconfig.json
|- packages/
| |- koas-core/
| | |- src/
| | | `- index.ts
| | |- package.json
| | `- tsconfig.json
| |- koas-status-code/
| | |- src/
| | | `- index.ts
| | |- package.json
| | `- tsconfig.json
| `- more similar workspaces…
|- package.json
|- tsconfig.json
`- more configuration files…
一些包相互依赖。我已经设法获得 Jest 测试和 eslint-plugin-import
使用此设置,但我在构建项目时遇到问题。
根 tsconfig.json
看起来像这样:
{
"compilerOptions": {
"baseUrl": ".",
"composite": true,
"declaration": true,
"module": "commonjs",
"noEmit": true,
"resolveJsonModule": true,
"target": "esnext",
"paths": {
"koas-*": ["packages/koas-*/src"]
}
},
"exclude": ["**/*.test.ts"]
}
工作区 tsconfig.json
个文件如下所示:
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "lib"
}
}
每个工作区都有一个在 package.json
中定义的 prepack
脚本,如下所示:
{
"scripts": {
"prepack": "tsc --noEmit false"
}
}
main
字段指的是lib
。
如果我 运行 yarn workspace koas-status-code prepack
,我得到以下错误:
$ tsc --noEmit false
error TS6059: File 'koas/packages/koas-core/src/SchemaValidationError.ts' is not under 'rootDir' 'koas/packages/koas-status-code'. 'rootDir' is expected to contain all source files.
error TS6059: File 'koas/packages/koas-core/src/SchemaValidationError.ts' is not under 'rootDir' 'koas/packages/koas-status-code/src'. 'rootDir' is expected to contain all source files.
error TS6059: File 'koas/packages/koas-core/src/createDefaultValidator.ts' is not under 'rootDir' 'koas/packages/koas-status-code'. 'rootDir' is expected to contain all source files.
error TS6059: File 'koas/packages/koas-core/src/createDefaultValidator.ts' is not under 'rootDir' 'koas/packages/koas-status-code/src'. 'rootDir' is expected to contain all source files.
error TS6059: File 'koas/packages/koas-core/src/createMatcher.ts' is not under 'rootDir' 'koas/packages/koas-status-code'. 'rootDir' is expected to contain all source files.
error TS6059: File 'koas/packages/koas-core/src/createMatcher.ts' is not under 'rootDir' 'koas/packages/koas-status-code/src'. 'rootDir' is expected to contain all source files.
error TS6059: File 'koas/packages/koas-core/src/index.ts' is not under 'rootDir' 'koas/packages/koas-status-code'. 'rootDir' is expected to contain all source files.
error TS6059: File 'koas/packages/koas-core/src/index.ts' is not under 'rootDir' 'koas/packages/koas-status-code/src'. 'rootDir' is expected to contain all source files.
error TS6307: File 'koas/packages/koas-core/src/SchemaValidationError.ts' is not listed within the file list of project 'koas/packages/koas-status-code/tsconfig.json'. Projects must list all files or use an 'include' pattern.
error TS6307: File 'koas/packages/koas-core/src/createDefaultValidator.ts' is not listed within the file list of project 'koas/packages/koas-status-code/tsconfig.json'. Projects must list all files or use an 'include' pattern.
error TS6307: File 'koas/packages/koas-core/src/createMatcher.ts' is not listed within the file list of project 'koas/packages/koas-status-code/tsconfig.json'. Projects must list all files or use an 'include' pattern.
error TS6307: File 'koas/packages/koas-core/src/index.ts' is not listed within the file list of project 'koas/packages/koas-status-code/tsconfig.json'. Projects must list all files or use an 'include' pattern.
Found 12 errors.
我也试过 tsconfig.json
koas-status-code
:
{
"extends": "../../tsconfig.json",
"include": ["src"],
"references": [{ "path": "../koas-core" }],
"compilerOptions": {
"outDir": "lib"
}
}
这构建了工作区,但仍然出现以下错误:
$ tsc --noEmit false
src/index.ts:1:23 - error TS6305: Output file '/home/remco/Projects/koas/packages/koas-core/lib/src/index.d.ts' has not been built from source file '/home/remco/Projects/koas/packages/koas-core/src/index.ts'.
1 import * as Koas from 'koas-core';
~~~~~~~~~~~
Found 1 error.
我该如何解决这个问题?
我根据 https://github.com/NiGhTTraX/lerna-ts.
找到了这个问题的解决方案项目现在在项目根目录中包含一个名为 tsconfig.build.ts
的文件。这个文件基本上只是指定编译器选项,测试和构建文件应该从构建过程中排除。
{
"compilerOptions": {
"declaration": true,
"module": "commonjs",
"resolveJsonModule": true,
"target": "esnext"
},
"exclude": ["**/*.test.ts", "**/lib"]
}
根目录中的另一个文件是 tsconfig.json
。该文件用于构建过程之外的类型检查,例如通过 Jest 和 VSCode。该文件扩展了构建配置。它包括通过覆盖扩展 exclude
配置的测试。它还包含 noEmit: true
,因为在开发过程中使用 TypeScript 的进程不应该发出任何东西。它还包含在运行时解析 TypeScript 源文件所需的 baseUrl
和 paths
编译器选项。这是因为包的 package.json
文件中的 main
字段引用了包含输出文件的 lib
。
{
"extends": "./tsconfig.build.json",
"compilerOptions": {
"baseUrl": ".",
"noEmit": true,
"paths": {
"koas-*": ["packages/koas-*/src"]
}
},
"exclude": ["**/lib"]
}
每个工作区都包含一个 tsconfig.build.json
文件。这是必需的,因为它要求 src
和 outDir
选项需要相对于 tsconfig 文件。那是用来构建项目的。重要的是此文件 而不是 命名为 tsconfig.json
.
{
"extends": "../../tsconfig.build.json",
"include": ["src"],
"compilerOptions": {
"outDir": "lib"
}
}
每个工作区还包含一个 tsconfig.json
。这可用于覆盖用于类型检查的编译器选项,例如,如果一个存储库将使用 dom
类型。这可以省略。
{
"extends": "../../tsconfig.json"
}
每个工作区在 package.json
中都有以下 scripts
部分。这会导致在构建包时编译 TypeScript。
// …
"scripts": {
"prepack": "tsc --project tsconfig.build.json"
}
有两个 CI 作业用于类型检查。一个运行 tsc
以确保类型检查在开发中使用这些类型的工具中仍然有效,另一个确保构建包不会中断。
tsc:
script:
- yarn --frozen-lockfile
- yarn workspaces run tsc
pack:
script:
- yarn --frozen-lockfile
- yarn workspaces run pack
- find packages -name '*.tgz' -exec mv {} ./ +
artifacts:
name: packages
paths:
- '*.tgz'