ts-jest
和ESLint与eslint-plugin-import
。我正在尝试使用TypeScript正确构建项目包。以前,我只是将TypeScript文件与它们的JavaScript代码一起发布在同一个目录中。
我的构建项目的尝试在GitLab合并请求中可用这里 现在,仓库遵循以下布局:
|- 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.
我该如何修复这个问题?
../lib/Something
导入,除非您不使用rootDir
和outDir
,并接受.js
和.js.map
文件将随每个.ts
文件一起出现,污染工作区。 - Dan Dascalescu